diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index 825d21f4bb..bdc60a6259 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 @@ -146,7 +147,12 @@ module.exports = { getReturnStatements(node.body) .filter((returnStatement) => returnStatement && returnStatement.argument) .forEach((returnStatement) => { - checkIteratorElement(returnStatement.argument); + const argument = returnStatement.argument; + if (argument.type === 'LogicalExpression' && isJSX(argument.right)) { + checkIteratorElement(argument.right); + } else { + checkIteratorElement(argument); + } }); } } @@ -159,18 +165,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); } } 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' }], + }, ]), });