Skip to content

Commit ecadb92

Browse files
yialoljharb
authored andcommitted
[Fix] jsx-key: detect conditional returns
1 parent 422ff33 commit ecadb92

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1010

1111
### Fixed
1212
* [`jsx-no-leaked-render`]: preserve RHS parens for multiline jsx elements while fixing ([#3623][] @akulsr0)
13+
* [`jsx-key`]: detect conditional returns ([#3630][] @yialo)
1314

15+
[#3630]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3630
1416
[#3623]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3623
1517
[#3615]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3615
1618

lib/rules/jsx-key.js

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ module.exports = {
9696
if (node.alternate) {
9797
getReturnStatements(node.alternate, returnStatements);
9898
}
99+
} else if (node.type === 'ReturnStatement') {
100+
returnStatements.push(node);
99101
} else if (Array.isArray(node.body)) {
100102
node.body.forEach((item) => {
101103
if (item.type === 'IfStatement') {

tests/lib/rules/jsx-key.js

+22
Original file line numberDiff line numberDiff line change
@@ -387,5 +387,27 @@ ruleTester.run('jsx-key', rule, {
387387
{ messageId: 'missingIterKey' },
388388
],
389389
},
390+
{
391+
code: `
392+
const TestCase = () => {
393+
const list = [1, 2, 3, 4, 5];
394+
395+
return (
396+
<div>
397+
{list.map(item => {
398+
if (item < 2) return <div>{item}</div>;
399+
else if (item < 5) return <div />;
400+
else return <div />;
401+
})}
402+
</div>
403+
);
404+
};
405+
`,
406+
errors: [
407+
{ messageId: 'missingIterKey' },
408+
{ messageId: 'missingIterKey' },
409+
{ messageId: 'missingIterKey' },
410+
],
411+
},
390412
]),
391413
});

0 commit comments

Comments
 (0)