Skip to content

Commit aba4f96

Browse files
committed
Fix prefer-stateless-function for conditional JSX (fixes #516)
1 parent e6b28cc commit aba4f96

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/rules/prefer-stateless-function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ module.exports = Components.detect(function(context, components, utils) {
209209
}
210210
scope = scope.upper;
211211
}
212-
if (!blockNode || !blockNode.key || blockNode.key.name !== 'render' || utils.isReturningJSX(node)) {
212+
if (!blockNode || !blockNode.key || blockNode.key.name !== 'render' || utils.isReturningJSX(node, true)) {
213213
return;
214214
}
215215
markReturnAsInvalid(node);

lib/util/Components.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ function componentRule(rule, context) {
166166
* Check if the node is returning JSX
167167
*
168168
* @param {ASTNode} node The AST node being checked (can be a ReturnStatement or an ArrowFunctionExpression).
169+
* @param {Boolean} strict If true, in a ternary condition the node must return JSX in both cases
169170
* @returns {Boolean} True if the node is returning JSX, false if not
170171
*/
171-
isReturningJSX: function(node) {
172+
isReturningJSX: function(node, strict) {
172173
var property;
173174
switch (node.type) {
174175
case 'ReturnStatement':
@@ -191,6 +192,10 @@ function componentRule(rule, context) {
191192
node[property].type === 'ConditionalExpression' &&
192193
node[property].alternate.type === 'JSXElement'
193194
;
195+
var returnsConditionalJSX =
196+
strict ? (returnsConditionalJSXConsequent && returnsConditionalJSXAlternate) :
197+
(returnsConditionalJSXConsequent || returnsConditionalJSXAlternate);
198+
194199
var returnsJSX =
195200
node[property] &&
196201
node[property].type === 'JSXElement'
@@ -203,8 +208,7 @@ function componentRule(rule, context) {
203208
;
204209

205210
return Boolean(
206-
returnsConditionalJSXConsequent ||
207-
returnsConditionalJSXAlternate ||
211+
returnsConditionalJSX ||
208212
returnsJSX ||
209213
returnsReactCreateElement
210214
);

0 commit comments

Comments
 (0)