Skip to content

Commit

Permalink
[Fix] jsx-key: detect conditional returns
Browse files Browse the repository at this point in the history
  • Loading branch information
yialo authored and ljharb committed Sep 4, 2023
1 parent 422ff33 commit ecadb92
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

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

[#3630]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3630
[#3623]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3623
[#3615]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3615

Expand Down
2 changes: 2 additions & 0 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ module.exports = {
if (node.alternate) {
getReturnStatements(node.alternate, returnStatements);
}
} else if (node.type === 'ReturnStatement') {
returnStatements.push(node);
} else if (Array.isArray(node.body)) {
node.body.forEach((item) => {
if (item.type === 'IfStatement') {
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,27 @@ ruleTester.run('jsx-key', rule, {
{ messageId: 'missingIterKey' },
],
},
{
code: `
const TestCase = () => {
const list = [1, 2, 3, 4, 5];
return (
<div>
{list.map(item => {
if (item < 2) return <div>{item}</div>;
else if (item < 5) return <div />;
else return <div />;
})}
</div>
);
};
`,
errors: [
{ messageId: 'missingIterKey' },
{ messageId: 'missingIterKey' },
{ messageId: 'missingIterKey' },
],
},
]),
});

0 comments on commit ecadb92

Please sign in to comment.