Skip to content

Commit

Permalink
Merge pull request jsx-eslint#1089 from benstepp/bs-multicomp-false-p…
Browse files Browse the repository at this point in the history
…ositives

Bug fix for false positives with no-multi-comp
  • Loading branch information
ljharb authored Feb 24, 2017
2 parents 8148833 + c038899 commit b646485
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 b646485

Please sign in to comment.