Skip to content

Commit 9202a30

Browse files
committed
chore: handle returning logical expressions
1 parent 56e7436 commit 9202a30

File tree

2 files changed

+250
-145
lines changed

2 files changed

+250
-145
lines changed

lib/rules/no-render-return-undefined.js

+29-26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const astUtil = require('../util/ast');
99
const docsUrl = require('../util/docsUrl');
10+
const isFirstLetterCapitalized = require('../util/isFirstLetterCapitalized');
1011
const report = require('../util/report');
1112
const variableUtil = require('../util/variable');
1213

@@ -54,32 +55,34 @@ module.exports = {
5455
return value;
5556
}
5657

57-
if (returnNode.type === 'ConditionalExpression') {
58-
const possibleReturnValue = [getReturnValue(returnNode.consequent), getReturnValue(returnNode.alternate)];
59-
const returnsUndefined = possibleReturnValue.some((val) => val === undefined);
60-
if (returnsUndefined) return undefined;
61-
return possibleReturnValue;
62-
}
63-
64-
if (returnNode.type === 'CallExpression') {
65-
const calleeName = returnNode.callee.name;
66-
const calleeNode = variableUtil.variablesInScope(context).find((item) => item.name === calleeName);
67-
const calleeDefinitionNode = calleeNode && calleeNode.defs && calleeNode.defs[0] && calleeNode.defs[0].node;
68-
const calleeReturnStatement = astUtil.findReturnStatement(calleeDefinitionNode);
69-
const calleeReturnNode = (calleeReturnStatement && calleeReturnStatement.argument)
70-
|| (calleeDefinitionNode.init && calleeDefinitionNode.init.body);
71-
return getReturnValue(calleeReturnNode);
72-
}
73-
74-
if (returnNode.type === 'ArrayExpression') {
75-
return returnNode.elements;
76-
}
77-
78-
if (returnNode.type === 'JSXElement') {
79-
return returnNode;
58+
switch (returnNode.type) {
59+
case 'LogicalExpression': {
60+
return getReturnValue(returnNode.right);
61+
}
62+
case 'ConditionalExpression': {
63+
const possibleReturnValue = [getReturnValue(returnNode.consequent), getReturnValue(returnNode.alternate)];
64+
const returnsUndefined = possibleReturnValue.some((val) => val === undefined);
65+
if (returnsUndefined) return undefined;
66+
return possibleReturnValue;
67+
}
68+
case 'CallExpression': {
69+
const calleeName = returnNode.callee.name;
70+
const calleeNode = variableUtil.variablesInScope(context).find((item) => item.name === calleeName);
71+
const calleeDefinitionNode = calleeNode && calleeNode.defs && calleeNode.defs[0] && calleeNode.defs[0].node;
72+
const calleeReturnStatement = astUtil.findReturnStatement(calleeDefinitionNode);
73+
const calleeReturnNode = (calleeReturnStatement && calleeReturnStatement.argument)
74+
|| (calleeDefinitionNode.init && calleeDefinitionNode.init.body);
75+
return getReturnValue(calleeReturnNode);
76+
}
77+
case 'ArrayExpression': {
78+
return returnNode.elements;
79+
}
80+
case 'JSXElement': {
81+
return returnNode;
82+
}
83+
default:
84+
return returnNode.value;
8085
}
81-
82-
return returnNode.value;
8386
}
8487

8588
const isReturningUndefined = (returnStatement) => {
@@ -102,7 +105,7 @@ module.exports = {
102105
const fnName = (node.id && node.id.name) || node.parent.id.name;
103106

104107
// Considering functions starting with Uppercase letters are react components
105-
const isReactComponent = fnName[0] === fnName[0].toUpperCase();
108+
const isReactComponent = isFirstLetterCapitalized(fnName);
106109
const returnStatement = astUtil.findReturnStatement(node);
107110

108111
if (!isReactComponent) return;

0 commit comments

Comments
 (0)