diff --git a/CHANGELOG.md b/CHANGELOG.md index fff57f229c..8f88b8d4ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index 263ed8243c..c13a00cf83 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -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') { diff --git a/tests/lib/rules/jsx-key.js b/tests/lib/rules/jsx-key.js index 393affcaac..17109e4215 100644 --- a/tests/lib/rules/jsx-key.js +++ b/tests/lib/rules/jsx-key.js @@ -387,5 +387,27 @@ ruleTester.run('jsx-key', rule, { { messageId: 'missingIterKey' }, ], }, + { + code: ` + const TestCase = () => { + const list = [1, 2, 3, 4, 5]; + + return ( +
+ {list.map(item => { + if (item < 2) return
{item}
; + else if (item < 5) return
; + else return
; + })} +
+ ); + }; + `, + errors: [ + { messageId: 'missingIterKey' }, + { messageId: 'missingIterKey' }, + { messageId: 'missingIterKey' }, + ], + }, ]), });