From 14a212df9bd3ff574a6f59086bd8c75a5b9b09f1 Mon Sep 17 00:00:00 2001 From: Benjamin Stepp Date: Thu, 23 Feb 2017 09:58:31 -0600 Subject: [PATCH] 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 --- lib/util/Components.js | 18 +++++++++++++----- tests/lib/rules/no-multi-comp.js | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index 1eacf099dd..688a87dfc8 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -237,7 +237,7 @@ function componentRule(rule, context) { * @returns {Boolean} True if React.createElement called */ isReactCreateElement: function(node) { - return ( + var calledOnReact = ( node && node.callee && node.callee.object && @@ -245,6 +245,17 @@ function componentRule(rule, context) { 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; }, /** @@ -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 || diff --git a/tests/lib/rules/no-multi-comp.js b/tests/lib/rules/no-multi-comp.js index bbf2f32dc6..8f7d2210ec 100644 --- a/tests/lib/rules/no-multi-comp.js +++ b/tests/lib/rules/no-multi-comp.js @@ -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: [{