Skip to content

[Fix] jsx-key: detect conditional returns #3630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
@@ -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') {
22 changes: 22 additions & 0 deletions tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
@@ -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' },
],
},
]),
});