Skip to content

Commit 7c1abed

Browse files
committed
Add checkFragmentShorthand option
1 parent ed04c2f commit 7c1abed

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/rules/jsx-key.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const docsUrl = require('../util/docsUrl');
1313
// Rule Definition
1414
// ------------------------------------------------------------------------------
1515

16+
const defaultOptions = {
17+
checkFragmentShorthand: false
18+
}
19+
1620
module.exports = {
1721
meta: {
1822
docs: {
@@ -21,17 +25,29 @@ module.exports = {
2125
recommended: true,
2226
url: docsUrl('jsx-key')
2327
},
24-
schema: []
28+
schema: [{
29+
type: 'object',
30+
properties: {
31+
checkFragmentShorthand: {
32+
type: 'boolean',
33+
default: defaultOptions.checkFragmentShorthand
34+
}
35+
},
36+
additionalProperties: false
37+
}]
2538
},
2639

2740
create(context) {
41+
const options = Object.assign({}, defaultOptions, context.options[0])
42+
const checkFragmentShorthand = options.checkFragmentShorthand
43+
2844
function checkIteratorElement(node) {
2945
if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) {
3046
context.report({
3147
node,
3248
message: 'Missing "key" prop for element in iterator'
3349
});
34-
} else if (node.type === 'JSXFragment') {
50+
} else if (checkFragmentShorthand && node.type === 'JSXFragment') {
3551
context.report({
3652
node,
3753
message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys'
@@ -58,6 +74,10 @@ module.exports = {
5874
},
5975

6076
JSXFragment(node) {
77+
if (!checkFragmentShorthand) {
78+
return;
79+
}
80+
6181
if (node.parent.type === 'ArrayExpression') {
6282
context.report({
6383
node,

tests/lib/rules/jsx-key.js

+2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ ruleTester.run('jsx-key', rule, {
6464
}, {
6565
code: '[1, 2, 3].map(x => <>{x}</>);',
6666
parser: parsers.BABEL_ESLINT,
67+
options: [{checkFragmentShorthand: true}],
6768
errors: [{message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys'}]
6869
}, {
6970
code: '[<></>];',
7071
parser: parsers.BABEL_ESLINT,
72+
options: [{checkFragmentShorthand: true}],
7173
errors: [{message: 'Missing "key" prop for element in array. Shorthand fragment syntax does support providing keys'}]
7274
}]
7375
});

0 commit comments

Comments
 (0)