From c03889941365c07f55be6f440a0b806b0e9fb4a5 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 --- Review: Use object.assign with sourceType: module in the added test over babel-eslint. --- lib/util/Components.js | 18 +++++++++++++----- tests/lib/rules/no-multi-comp.js | 16 ++++++++++++++++ 2 files changed, 29 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..747538e277 100644 --- a/tests/lib/rules/no-multi-comp.js +++ b/tests/lib/rules/no-multi-comp.js @@ -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, @@ -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: [{