Skip to content

Commit 0bfb4c4

Browse files
committed
Add detection for stateless components with implicit return
1 parent a98fa94 commit 0bfb4c4

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

lib/util/Components.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,27 @@ function componentRule(rule, context) {
133133
* @returns {Boolean} True if the node is returning JSX, false if not
134134
*/
135135
isReturningJSX: function(node) {
136-
if (node.type !== 'ReturnStatement') {
137-
return false;
136+
var property;
137+
switch (node.type) {
138+
case 'ReturnStatement':
139+
property = 'argument';
140+
break;
141+
case 'ArrowFunctionExpression':
142+
property = 'body';
143+
break;
144+
default:
145+
return false;
138146
}
139147

140148
var returnsJSX =
141-
node.argument &&
142-
node.argument.type === 'JSXElement'
149+
node[property] &&
150+
node[property].type === 'JSXElement'
143151
;
144152
var returnsReactCreateElement =
145-
node.argument &&
146-
node.argument.callee &&
147-
node.argument.callee.property &&
148-
node.argument.callee.property.name === 'createElement'
153+
node[property] &&
154+
node[property].callee &&
155+
node[property].callee.property &&
156+
node[property].callee.property.name === 'createElement'
149157
;
150158

151159
return Boolean(returnsJSX || returnsReactCreateElement);
@@ -339,8 +347,12 @@ function componentRule(rule, context) {
339347
if (!node) {
340348
return;
341349
}
342-
var component = components.get(node);
343-
components.add(node, {confident: component && component.confident || false});
350+
if (node.expression && context.react.isReturningJSX(node)) {
351+
components.add(node, {confident: true});
352+
} else {
353+
var component = components.get(node);
354+
components.add(node, {confident: component && component.confident || false});
355+
}
344356
},
345357

346358
ReturnStatement: function(node) {

tests/lib/rules/prop-types.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,18 @@ ruleTester.run('prop-types', rule, {
14591459
{message: '\'names.map\' is missing in props validation'},
14601460
{message: '\'company\' is missing in props validation'}
14611461
]
1462+
}, {
1463+
code: [
1464+
'const Annotation = props => (',
1465+
' <div>',
1466+
' {props.text}',
1467+
' </div>',
1468+
')'
1469+
].join('\n'),
1470+
parser: 'babel-eslint',
1471+
errors: [
1472+
{message: '\'text\' is missing in props validation'}
1473+
]
14621474
}
14631475
]
14641476
});

0 commit comments

Comments
 (0)