diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index 25405bb13b..467d39cf79 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -45,7 +45,25 @@ module.exports = { const fragmentPragma = pragmaUtil.getFragmentFromContext(context); function checkIteratorElement(node) { - if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) { + if (node.type === 'JSXElement') { + const hasAKeyAttribute = hasProp(node.openingElement.attributes, 'key'); + + const hasASpreadArgumentWithAKeyProperty = node.openingElement + && node.openingElement.attributes + && node.openingElement.attributes.some( + ({argument}) => argument && argument.properties && argument.properties.some( + (property) => property.key.name === 'key')); + + const hasAnObjectSpreadArgument = node.openingElement + && node.openingElement.attributes + && node.openingElement.attributes.some( + ({argument}) => argument && argument.type === 'Identifier'); + + const isValidElement = hasAKeyAttribute + || hasASpreadArgumentWithAKeyProperty + || hasAnObjectSpreadArgument; + if (isValidElement) return; + context.report({ node, message: 'Missing "key" prop for element in iterator' diff --git a/tests/lib/rules/jsx-key.js b/tests/lib/rules/jsx-key.js index 49632f59e9..50d8d7660c 100644 --- a/tests/lib/rules/jsx-key.js +++ b/tests/lib/rules/jsx-key.js @@ -39,6 +39,8 @@ ruleTester.run('jsx-key', rule, { {code: 'fn()'}, {code: '[1, 2, 3].map(function () {})'}, {code: ';'}, + {code: '[1, 2, 3].map(x => );'}, + {code: '[1, 2, 3].map(x => );'}, {code: '[, ];'}, {code: '[1, 2, 3].map(function(x) { return });'}, {code: '[1, 2, 3].map(x => );'},