From f9ee7edace836c71d5fcf1fd76e745102e819611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Alper=20G=C3=B6l?= Date: Fri, 6 Jun 2025 23:56:27 +0300 Subject: [PATCH 1/3] [Refactor] `jsx-key`: use isJSX util function instead of shouldCheckNode --- lib/rules/jsx-key.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index 825d21f4bb..a932ef1a3e 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -13,6 +13,7 @@ const pragmaUtil = require('../util/pragma'); const report = require('../util/report'); const astUtil = require('../util/ast'); const getText = require('../util/eslint').getText; +const isJSX = require('../util/jsx').isJSX; // ------------------------------------------------------------------------------ // Rule Definition @@ -159,18 +160,17 @@ module.exports = { */ function checkArrowFunctionWithJSX(node) { const isArrFn = node && node.type === 'ArrowFunctionExpression'; - const shouldCheckNode = (n) => n && (n.type === 'JSXElement' || n.type === 'JSXFragment'); - if (isArrFn && shouldCheckNode(node.body)) { + if (isArrFn && isJSX(node.body)) { checkIteratorElement(node.body); } if (node.body.type === 'ConditionalExpression') { - if (shouldCheckNode(node.body.consequent)) { + if (isJSX(node.body.consequent)) { checkIteratorElement(node.body.consequent); } - if (shouldCheckNode(node.body.alternate)) { + if (isJSX(node.body.alternate)) { checkIteratorElement(node.body.alternate); } - } else if (node.body.type === 'LogicalExpression' && shouldCheckNode(node.body.right)) { + } else if (node.body.type === 'LogicalExpression' && isJSX(node.body.right)) { checkIteratorElement(node.body.right); } } From 7b2bcabdebd81672e3ee5a28b79981e3d6efc7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Alper=20G=C3=B6l?= Date: Fri, 6 Jun 2025 23:59:32 +0300 Subject: [PATCH 2/3] [Fix] `jsx-key`: detect missing keys in logical expressions --- lib/rules/jsx-key.js | 4 ++++ tests/lib/rules/jsx-key.js | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index a932ef1a3e..e72f070053 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -147,7 +147,11 @@ module.exports = { getReturnStatements(node.body) .filter((returnStatement) => returnStatement && returnStatement.argument) .forEach((returnStatement) => { + const argument = returnStatement.argument; checkIteratorElement(returnStatement.argument); + if (argument.type === 'LogicalExpression' && isJSX(argument.right)) { + checkIteratorElement(argument.right); + } }); } } diff --git a/tests/lib/rules/jsx-key.js b/tests/lib/rules/jsx-key.js index 2c5ba7a5c1..932907a195 100644 --- a/tests/lib/rules/jsx-key.js +++ b/tests/lib/rules/jsx-key.js @@ -205,6 +205,9 @@ ruleTester.run('jsx-key', rule, { `, settings, }, + { code: '[1, 2, 3].map(x => { return x && ; });' }, + { code: '[1, 2, 3].map(x => { return x && y && ; });' }, + { code: '[1, 2, 3].map(x => { return x && foo(); });' }, ]), invalid: parsers.all([ { @@ -424,5 +427,13 @@ ruleTester.run('jsx-key', rule, { options: [{ checkKeyMustBeforeSpread: true }], errors: [{ messageId: 'keyBeforeSpread' }], }, + { + code: '[1, 2, 3].map(x => { return x && ; });', + errors: [{ messageId: 'missingIterKey' }], + }, + { + code: '[1, 2, 3].map(x => { return x || y || ; });', + errors: [{ messageId: 'missingIterKey' }], + }, ]), }); From 1cb9049043634401648776450e87e432542b4202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Alper=20G=C3=B6l?= Date: Sat, 7 Jun 2025 00:16:57 +0300 Subject: [PATCH 3/3] [Fix] `jsx-key`: only check full argument when not LogicalExpression with JSX --- lib/rules/jsx-key.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index e72f070053..bdc60a6259 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -148,9 +148,10 @@ module.exports = { .filter((returnStatement) => returnStatement && returnStatement.argument) .forEach((returnStatement) => { const argument = returnStatement.argument; - checkIteratorElement(returnStatement.argument); if (argument.type === 'LogicalExpression' && isJSX(argument.right)) { checkIteratorElement(argument.right); + } else { + checkIteratorElement(argument); } }); }