Skip to content

Commit

Permalink
Fix prefer-stateless-function for conditional JSX (fixes #516)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Mar 29, 2016
1 parent e6b28cc commit aba4f96
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/rules/prefer-stateless-function.js
Expand Up @@ -209,7 +209,7 @@ module.exports = Components.detect(function(context, components, utils) {
}
scope = scope.upper;
}
if (!blockNode || !blockNode.key || blockNode.key.name !== 'render' || utils.isReturningJSX(node)) {
if (!blockNode || !blockNode.key || blockNode.key.name !== 'render' || utils.isReturningJSX(node, true)) {
return;
}
markReturnAsInvalid(node);
Expand Down
10 changes: 7 additions & 3 deletions lib/util/Components.js
Expand Up @@ -166,9 +166,10 @@ function componentRule(rule, context) {
* Check if the node is returning JSX
*
* @param {ASTNode} node The AST node being checked (can be a ReturnStatement or an ArrowFunctionExpression).
* @param {Boolean} strict If true, in a ternary condition the node must return JSX in both cases
* @returns {Boolean} True if the node is returning JSX, false if not
*/
isReturningJSX: function(node) {
isReturningJSX: function(node, strict) {
var property;
switch (node.type) {
case 'ReturnStatement':
Expand All @@ -191,6 +192,10 @@ function componentRule(rule, context) {
node[property].type === 'ConditionalExpression' &&
node[property].alternate.type === 'JSXElement'
;
var returnsConditionalJSX =
strict ? (returnsConditionalJSXConsequent && returnsConditionalJSXAlternate) :
(returnsConditionalJSXConsequent || returnsConditionalJSXAlternate);

var returnsJSX =
node[property] &&
node[property].type === 'JSXElement'
Expand All @@ -203,8 +208,7 @@ function componentRule(rule, context) {
;

return Boolean(
returnsConditionalJSXConsequent ||
returnsConditionalJSXAlternate ||
returnsConditionalJSX ||
returnsJSX ||
returnsReactCreateElement
);
Expand Down

0 comments on commit aba4f96

Please sign in to comment.