Skip to content

Commit

Permalink
Add detection for stateless components with implicit return
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Nov 4, 2015
1 parent a98fa94 commit 0bfb4c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,27 @@ function componentRule(rule, context) {
* @returns {Boolean} True if the node is returning JSX, false if not
*/
isReturningJSX: function(node) {
if (node.type !== 'ReturnStatement') {
return false;
var property;
switch (node.type) {
case 'ReturnStatement':
property = 'argument';
break;
case 'ArrowFunctionExpression':
property = 'body';
break;
default:
return false;
}

var returnsJSX =
node.argument &&
node.argument.type === 'JSXElement'
node[property] &&
node[property].type === 'JSXElement'
;
var returnsReactCreateElement =
node.argument &&
node.argument.callee &&
node.argument.callee.property &&
node.argument.callee.property.name === 'createElement'
node[property] &&
node[property].callee &&
node[property].callee.property &&
node[property].callee.property.name === 'createElement'
;

return Boolean(returnsJSX || returnsReactCreateElement);
Expand Down Expand Up @@ -339,8 +347,12 @@ function componentRule(rule, context) {
if (!node) {
return;
}
var component = components.get(node);
components.add(node, {confident: component && component.confident || false});
if (node.expression && context.react.isReturningJSX(node)) {
components.add(node, {confident: true});
} else {
var component = components.get(node);
components.add(node, {confident: component && component.confident || false});
}
},

ReturnStatement: function(node) {
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,18 @@ ruleTester.run('prop-types', rule, {
{message: '\'names.map\' is missing in props validation'},
{message: '\'company\' is missing in props validation'}
]
}, {
code: [
'const Annotation = props => (',
' <div>',
' {props.text}',
' </div>',
')'
].join('\n'),
parser: 'babel-eslint',
errors: [
{message: '\'text\' is missing in props validation'}
]
}
]
});

0 comments on commit 0bfb4c4

Please sign in to comment.