Skip to content

Commit

Permalink
Fix component detection to ignore functions expression without a pare…
Browse files Browse the repository at this point in the history
…nt component
  • Loading branch information
yannickcr committed Aug 22, 2016
1 parent 7ed3b29 commit 70bf28d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/util/Components.js
Expand Up @@ -483,14 +483,16 @@ function componentRule(rule, context) {
},

FunctionExpression: function(node) {
node = utils.getParentComponent();
var component = utils.getParentComponent();
if (
!node ||
(node.parent && node.parent.type === 'JSXExpressionContainer')
!component ||
(component.parent && component.parent.type === 'JSXExpressionContainer')
) {
// Ban the node if we cannot find a parent component
components.add(node, 0);
return;
}
components.add(node, 1);
components.add(component, 1);
},

FunctionDeclaration: function(node) {
Expand All @@ -502,17 +504,19 @@ function componentRule(rule, context) {
},

ArrowFunctionExpression: function(node) {
node = utils.getParentComponent();
var component = utils.getParentComponent();
if (
!node ||
(node.parent && node.parent.type === 'JSXExpressionContainer')
!component ||
(component.parent && component.parent.type === 'JSXExpressionContainer')
) {
// Ban the node if we cannot find a parent component
components.add(node, 0);
return;
}
if (node.expression && utils.isReturningJSX(node)) {
components.add(node, 2);
if (component.expression && utils.isReturningJSX(component)) {
components.add(component, 2);
} else {
components.add(node, 1);
components.add(component, 1);
}
},

Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/display-name.js
Expand Up @@ -358,6 +358,26 @@ ruleTester.run('display-name', rule, {
'module.exports = someDecorator;'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'const element = (',
' <Media query={query} render={() => {',
' renderWasCalled = true',
' return <div/>',
' }}/>',
')'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'const element = (',
' <Media query={query} render={function() {',
' renderWasCalled = true',
' return <div/>',
' }}/>',
')'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit 70bf28d

Please sign in to comment.