Skip to content

Commit 8aec646

Browse files
committed
chore: handle map callbacks
1 parent 639f15f commit 8aec646

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,23 @@ module.exports = {
6666
return possibleReturnValue;
6767
}
6868
case 'CallExpression': {
69+
if (returnNode.callee.type === 'MemberExpression') {
70+
const calleeObjName = returnNode.callee.object.name;
71+
const calleePropertyName = returnNode.callee.property.name;
72+
const calleeObjNode = variables.find((item) => item && item.name === calleeObjName);
73+
const isCalleeObjArray = calleeObjNode.defs[0].node.init.type === 'ArrayExpression';
74+
const isMapCall = isCalleeObjArray && calleePropertyName === 'map';
75+
if (isMapCall) {
76+
const mapCallback = returnNode.arguments[0];
77+
const mapReturnStatement = mapCallback.body.type === 'BlockStatement'
78+
&& astUtil.findReturnStatement(returnNode.arguments[0]);
79+
const mapReturnNode = (mapReturnStatement && mapReturnStatement.argument) || mapCallback.body;
80+
// console.log('DEBUG', mapReturnNode);
81+
return getReturnValue(mapReturnNode);
82+
}
83+
}
6984
const calleeName = returnNode.callee.name;
70-
const calleeNode = variableUtil.variablesInScope(context).find((item) => item.name === calleeName);
85+
const calleeNode = variables.find((item) => item && item.name === calleeName);
7186
const calleeDefinitionNode = calleeNode && calleeNode.defs && calleeNode.defs[0] && calleeNode.defs[0].node;
7287
const calleeReturnStatement = astUtil.findReturnStatement(calleeDefinitionNode);
7388
const calleeReturnNode = (calleeReturnStatement && calleeReturnStatement.argument)
@@ -102,7 +117,7 @@ module.exports = {
102117
};
103118

104119
const handleFunctionalComponents = (node) => {
105-
const fnName = (node.id && node.id.name) || node.parent.id.name;
120+
const fnName = (node.id && node.id.name) || (node.parent.id && node.parent.id.name);
106121

107122
// Considering functions starting with Uppercase letters are react components
108123
const isReactComponent = isFirstLetterCapitalized(fnName);

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

+79
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,42 @@ ruleTester.run('no-render-return-undefined', rule, {
417417
}
418418
`,
419419
},
420+
{
421+
code: `
422+
const data = ['John', 'Ram' , 'Akul'];
423+
function App() {
424+
return data.map(name => <h1>{name}</h1>);
425+
}
426+
`,
427+
},
428+
{
429+
code: `
430+
const data = ['John', 'Ram' , 'Akul'];
431+
function App() {
432+
return data.map(name => {
433+
return <h1>{name}</h1>;
434+
});
435+
}
436+
`,
437+
},
438+
{
439+
code: `
440+
const data = ['John', 'Ram' , 'Akul'];
441+
function App() {
442+
return cond && data.map(name => {
443+
return <h1>{name}</h1>;
444+
});
445+
}
446+
`,
447+
},
448+
{
449+
code: `
450+
const data = ['John', 'Ram' , 'Akul'];
451+
function App() {
452+
return cond ? data.map(name => <h1>{name}</h1>) : null;
453+
}
454+
`,
455+
},
420456
]),
421457

422458
// Invalid Cases
@@ -845,5 +881,48 @@ ruleTester.run('no-render-return-undefined', rule, {
845881
`,
846882
errors: [{ messageId: 'returnsUndefined' }],
847883
},
884+
{
885+
code: `
886+
const data = ['John', 'Ram' , 'Akul'];
887+
function App() {
888+
return cond ? data.map(name => <h1>{name}</h1>) : undefined;
889+
}
890+
`,
891+
errors: [{ messageId: 'returnsUndefined' }],
892+
},
893+
{
894+
code: `
895+
const data = ['John', 'Ram' , 'Akul'];
896+
function App() {
897+
return data.map(name => {
898+
if(cond) return <h1>{name}</h1>;
899+
});
900+
}
901+
`,
902+
errors: [{ messageId: 'returnsUndefined' }],
903+
},
904+
{
905+
code: `
906+
const data = ['John', 'Ram' , 'Akul'];
907+
function App() {
908+
if(cond) {
909+
return data.map(name => <h1>{name}</h1>);
910+
}
911+
}
912+
`,
913+
errors: [{ messageId: 'returnsUndefined' }],
914+
},
915+
{
916+
code: `
917+
let x;
918+
const data = ['John', 'Ram' , 'Akul'];
919+
function App() {
920+
return cond ? data.map(name => {
921+
return <h1>{name}</h1>;
922+
}) : x;
923+
}
924+
`,
925+
errors: [{ messageId: 'returnsUndefined' }],
926+
},
848927
]),
849928
});

0 commit comments

Comments
 (0)