Skip to content

Commit

Permalink
[Fix] jsx-key: add a failing test case for optional chaining
Browse files Browse the repository at this point in the history
Fixes #2594

Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jonathan Lee <1150974628@qq.com>
  • Loading branch information
JonathanLee-LX and ljharb committed Mar 29, 2020
1 parent da7a045 commit d66f467
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/rules/jsx-key.js
Expand Up @@ -90,8 +90,8 @@ module.exports = {
},

// Array.prototype.map
CallExpression(node) {
if (node.callee && node.callee.type !== 'MemberExpression') {
'CallExpression, OptionalCallExpression'(node) {
if (node.callee && node.callee.type !== 'MemberExpression' && node.callee.type !== 'OptionalMemberExpression') {
return;
}

Expand Down
12 changes: 10 additions & 2 deletions tests/lib/rules/jsx-key.js
Expand Up @@ -50,7 +50,7 @@ ruleTester.run('jsx-key', rule, {
{code: 'foo(() => <></>);', parser: parsers.BABEL_ESLINT},
{code: '<></>;', parser: parsers.BABEL_ESLINT}
],
invalid: [{
invalid: [].concat({
code: '[<App />];',
errors: [{message: 'Missing "key" prop for element in array'}]
}, {
Expand All @@ -69,6 +69,14 @@ ruleTester.run('jsx-key', rule, {
code: '[1, 2 ,3].map(x => { return <App /> });',
errors: [{message: 'Missing "key" prop for element in iterator'}]
}, {
code: '[1, 2, 3]?.map(x => <BabelEslintApp />)',
parser: parsers.BABEL_ESLINT,
errors: [{message: 'Missing "key" prop for element in iterator'}]
}, parsers.TS({
code: '[1, 2, 3]?.map(x => <TypescriptEslintApp />)',
parser: parsers['@TYPESCRIPT_ESLINT'],
errors: [{message: 'Missing "key" prop for element in iterator'}]
}), {
code: '[1, 2, 3].map(x => <>{x}</>);',
parser: parsers.BABEL_ESLINT,
options: [{checkFragmentShorthand: true}],
Expand All @@ -80,5 +88,5 @@ ruleTester.run('jsx-key', rule, {
options: [{checkFragmentShorthand: true}],
settings,
errors: [{message: 'Missing "key" prop for element in array. Shorthand fragment syntax does not support providing keys. Use Act.Frag instead'}]
}]
})
});

0 comments on commit d66f467

Please sign in to comment.