Skip to content

Commit

Permalink
Bug fix for false positives with no-multi-comp
Browse files Browse the repository at this point in the history
Previously, when createElement was destructured from React, it would
cause any function defined in the file to be flagged as a React
Component. This change makes it such that the function must call
createElement to be considered a component.

Fixes jsx-eslint#1088
  • Loading branch information
benstepp committed Feb 23, 2017
1 parent 8148833 commit 14a212d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/util/Components.js
Expand Up @@ -237,14 +237,25 @@ function componentRule(rule, context) {
* @returns {Boolean} True if React.createElement called
*/
isReactCreateElement: function(node) {
return (
var calledOnReact = (
node &&
node.callee &&
node.callee.object &&
node.callee.object.name === 'React' &&
node.callee.property &&
node.callee.property.name === 'createElement'
);

var calledDirectly = (
node &&
node.callee &&
node.callee.name === 'createElement'
);

if (this.hasDestructuredReactCreateElement()) {
return calledDirectly || calledOnReact;
}
return calledOnReact;
},

/**
Expand Down Expand Up @@ -290,10 +301,7 @@ function componentRule(rule, context) {
node[property] &&
node[property].type === 'JSXElement'
;
var returnsReactCreateElement =
this.hasDestructuredReactCreateElement() ||
this.isReactCreateElement(node[property])
;
var returnsReactCreateElement = this.isReactCreateElement(node[property]);

return Boolean(
returnsConditionalJSX ||
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-multi-comp.js
Expand Up @@ -89,6 +89,21 @@ ruleTester.run('no-multi-comp', rule, {
options: [{
ignoreStateless: true
}]
}, {
// multiple non-components
code: [
'import React, { createElement } from "react"',
'const helperFoo = () => {',
' return true;',
'};',
'function helperBar() {',
' return false;',
'};',
'function RealComponent() {',
' return createElement("img");',
'};'
].join('\n'),
parser: 'babel-eslint'
}],

invalid: [{
Expand Down

0 comments on commit 14a212d

Please sign in to comment.