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

---

Review: Use object.assign with sourceType: module in the added test over
babel-eslint.
  • Loading branch information
benstepp committed Feb 23, 2017
1 parent 8148833 commit c038899
Show file tree
Hide file tree
Showing 2 changed files with 29 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
16 changes: 16 additions & 0 deletions tests/lib/rules/no-multi-comp.js
Expand Up @@ -10,6 +10,7 @@

var rule = require('../../../lib/rules/no-multi-comp');
var RuleTester = require('eslint').RuleTester;
var assign = require('object.assign');

var parserOptions = {
ecmaVersion: 6,
Expand Down Expand Up @@ -89,6 +90,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'),
parserOptions: assign({sourceType: 'module'}, parserOptions)
}],

invalid: [{
Expand Down

0 comments on commit c038899

Please sign in to comment.