Skip to content

Commit d66f467

Browse files
[Fix] jsx-key: add a failing test case for optional chaining
Fixes #2594 Co-authored-by: Jordan Harband <[email protected]> Co-authored-by: Jonathan Lee <[email protected]>
1 parent da7a045 commit d66f467

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/rules/jsx-key.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ module.exports = {
9090
},
9191

9292
// Array.prototype.map
93-
CallExpression(node) {
94-
if (node.callee && node.callee.type !== 'MemberExpression') {
93+
'CallExpression, OptionalCallExpression'(node) {
94+
if (node.callee && node.callee.type !== 'MemberExpression' && node.callee.type !== 'OptionalMemberExpression') {
9595
return;
9696
}
9797

tests/lib/rules/jsx-key.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ruleTester.run('jsx-key', rule, {
5050
{code: 'foo(() => <></>);', parser: parsers.BABEL_ESLINT},
5151
{code: '<></>;', parser: parsers.BABEL_ESLINT}
5252
],
53-
invalid: [{
53+
invalid: [].concat({
5454
code: '[<App />];',
5555
errors: [{message: 'Missing "key" prop for element in array'}]
5656
}, {
@@ -69,6 +69,14 @@ ruleTester.run('jsx-key', rule, {
6969
code: '[1, 2 ,3].map(x => { return <App /> });',
7070
errors: [{message: 'Missing "key" prop for element in iterator'}]
7171
}, {
72+
code: '[1, 2, 3]?.map(x => <BabelEslintApp />)',
73+
parser: parsers.BABEL_ESLINT,
74+
errors: [{message: 'Missing "key" prop for element in iterator'}]
75+
}, parsers.TS({
76+
code: '[1, 2, 3]?.map(x => <TypescriptEslintApp />)',
77+
parser: parsers['@TYPESCRIPT_ESLINT'],
78+
errors: [{message: 'Missing "key" prop for element in iterator'}]
79+
}), {
7280
code: '[1, 2, 3].map(x => <>{x}</>);',
7381
parser: parsers.BABEL_ESLINT,
7482
options: [{checkFragmentShorthand: true}],
@@ -80,5 +88,5 @@ ruleTester.run('jsx-key', rule, {
8088
options: [{checkFragmentShorthand: true}],
8189
settings,
8290
errors: [{message: 'Missing "key" prop for element in array. Shorthand fragment syntax does not support providing keys. Use Act.Frag instead'}]
83-
}]
91+
})
8492
});

0 commit comments

Comments
 (0)