Skip to content

Commit 14a212d

Browse files
committed
Bug fix for false positives with no-multi-comp
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 #1088
1 parent 8148833 commit 14a212d

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/util/Components.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,25 @@ function componentRule(rule, context) {
237237
* @returns {Boolean} True if React.createElement called
238238
*/
239239
isReactCreateElement: function(node) {
240-
return (
240+
var calledOnReact = (
241241
node &&
242242
node.callee &&
243243
node.callee.object &&
244244
node.callee.object.name === 'React' &&
245245
node.callee.property &&
246246
node.callee.property.name === 'createElement'
247247
);
248+
249+
var calledDirectly = (
250+
node &&
251+
node.callee &&
252+
node.callee.name === 'createElement'
253+
);
254+
255+
if (this.hasDestructuredReactCreateElement()) {
256+
return calledDirectly || calledOnReact;
257+
}
258+
return calledOnReact;
248259
},
249260

250261
/**
@@ -290,10 +301,7 @@ function componentRule(rule, context) {
290301
node[property] &&
291302
node[property].type === 'JSXElement'
292303
;
293-
var returnsReactCreateElement =
294-
this.hasDestructuredReactCreateElement() ||
295-
this.isReactCreateElement(node[property])
296-
;
304+
var returnsReactCreateElement = this.isReactCreateElement(node[property]);
297305

298306
return Boolean(
299307
returnsConditionalJSX ||

tests/lib/rules/no-multi-comp.js

+15
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ ruleTester.run('no-multi-comp', rule, {
8989
options: [{
9090
ignoreStateless: true
9191
}]
92+
}, {
93+
// multiple non-components
94+
code: [
95+
'import React, { createElement } from "react"',
96+
'const helperFoo = () => {',
97+
' return true;',
98+
'};',
99+
'function helperBar() {',
100+
' return false;',
101+
'};',
102+
'function RealComponent() {',
103+
' return createElement("img");',
104+
'};'
105+
].join('\n'),
106+
parser: 'babel-eslint'
92107
}],
93108

94109
invalid: [{

0 commit comments

Comments
 (0)