Skip to content

Commit

Permalink
Fix stateless components detection when conditionally returning JSX (f…
Browse files Browse the repository at this point in the history
…ixes #486)
  • Loading branch information
yannickcr committed Mar 6, 2016
1 parent 8df4123 commit 70cebe4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function componentRule(rule, context) {
/**
* Check if the node is returning JSX
*
* @param {ASTNode} node The AST node being checked (must be a ReturnStatement).
* @param {ASTNode} node The AST node being checked (can be a ReturnStatement or an ArrowFunctionExpression).
* @returns {Boolean} True if the node is returning JSX, false if not
*/
isReturningJSX: function(node) {
Expand All @@ -181,6 +181,16 @@ function componentRule(rule, context) {
return false;
}

var returnsConditionalJSXConsequent =
node[property] &&
node[property].type === 'ConditionalExpression' &&
node[property].consequent.type === 'JSXElement'
;
var returnsConditionalJSXAlternate =
node[property] &&
node[property].type === 'ConditionalExpression' &&
node[property].alternate.type === 'JSXElement'
;
var returnsJSX =
node[property] &&
node[property].type === 'JSXElement'
Expand All @@ -192,7 +202,12 @@ function componentRule(rule, context) {
node[property].callee.property.name === 'createElement'
;

return Boolean(returnsJSX || returnsReactCreateElement);
return Boolean(
returnsConditionalJSXConsequent ||
returnsConditionalJSXAlternate ||
returnsJSX ||
returnsReactCreateElement
);
},

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,14 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'toggle\' is missing in props validation'
}]
}, {
code: [
'const MyComponent = props => props.test ? <div /> : <span />'
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: '\'test\' is missing in props validation'
}]
}
]
});

0 comments on commit 70cebe4

Please sign in to comment.