diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index 5c3450e7d9..614527c6cc 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -13,6 +13,10 @@ const docsUrl = require('../util/docsUrl'); // Rule Definition // ------------------------------------------------------------------------------ +const defaultOptions = { + checkFragmentShorthand: false +} + module.exports = { meta: { docs: { @@ -21,17 +25,29 @@ module.exports = { recommended: true, url: docsUrl('jsx-key') }, - schema: [] + schema: [{ + type: 'object', + properties: { + checkFragmentShorthand: { + type: 'boolean', + default: defaultOptions.checkFragmentShorthand + } + }, + additionalProperties: false + }] }, create(context) { + const options = Object.assign({}, defaultOptions, context.options[0]) + const checkFragmentShorthand = options.checkFragmentShorthand + function checkIteratorElement(node) { if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) { context.report({ node, message: 'Missing "key" prop for element in iterator' }); - } else if (node.type === 'JSXFragment') { + } else if (checkFragmentShorthand && node.type === 'JSXFragment') { context.report({ node, message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys' @@ -58,6 +74,10 @@ module.exports = { }, JSXFragment(node) { + if (!checkFragmentShorthand) { + return; + } + if (node.parent.type === 'ArrayExpression') { context.report({ node, diff --git a/tests/lib/rules/jsx-key.js b/tests/lib/rules/jsx-key.js index a61ed628d8..fd28fa5dca 100644 --- a/tests/lib/rules/jsx-key.js +++ b/tests/lib/rules/jsx-key.js @@ -64,10 +64,12 @@ ruleTester.run('jsx-key', rule, { }, { code: '[1, 2, 3].map(x => <>{x});', parser: parsers.BABEL_ESLINT, + options: [{checkFragmentShorthand: true}], errors: [{message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys'}] }, { code: '[<>];', parser: parsers.BABEL_ESLINT, + options: [{checkFragmentShorthand: true}], errors: [{message: 'Missing "key" prop for element in array. Shorthand fragment syntax does support providing keys'}] }] });