Skip to content

Commit eb872c9

Browse files
committed
Fix JSX return being assigned to the parent function is some cases (fixes #504)
1 parent 8863dfa commit eb872c9

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/util/Components.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,15 @@ function componentRule(rule, context) {
330330
while (scope) {
331331
var node = scope.block;
332332
var isClass = node.type === 'ClassExpression';
333-
var isFunction = /Function/.test(node.type); // Ignore non functions
334-
var isNotMethod = !node.parent || node.parent.type !== 'MethodDefinition'; // Ignore classes methods
335-
var isNotArgument = !node.parent || node.parent.type !== 'CallExpression'; // Ignore arguments (callback, etc.)
336-
// Stop moving up if we reach a class
337-
if (isClass) {
333+
var isFunction = /Function/.test(node.type); // Functions
334+
var isMethod = node.parent && node.parent.type === 'MethodDefinition'; // Classes methods
335+
var isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.)
336+
// Stop moving up if we reach a class or an argument (like a callback)
337+
if (isClass || isArgument) {
338338
return null;
339339
}
340-
// Return the node if it is a function that is not a class method or an argument (like a callback)
341-
if (isFunction && isNotMethod && isNotArgument) {
340+
// Return the node if it is a function that is not a class method
341+
if (isFunction && !isMethod) {
342342
return node;
343343
}
344344
scope = scope.upper;
@@ -534,6 +534,8 @@ function componentRule(rule, context) {
534534
}
535535
node = utils.getParentComponent();
536536
if (!node) {
537+
var scope = context.getScope();
538+
components.add(scope.block, 1);
537539
return;
538540
}
539541
components.add(node, 2);

tests/lib/rules/prop-types.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,15 @@ ruleTester.run('prop-types', rule, {
12711271
'}'
12721272
].join('\n'),
12731273
parserOptions: parserOptions
1274+
}, {
1275+
code: [
1276+
'function Hello({names}) {',
1277+
' return names.map((name) => {',
1278+
' return <div>{name}</div>;',
1279+
' });',
1280+
'}'
1281+
].join('\n'),
1282+
parser: 'babel-eslint'
12741283
}
12751284
],
12761285

0 commit comments

Comments
 (0)