Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] jsx-key: support optional chaining #2610

Merged
merged 1 commit into from May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'}]
ljharb marked this conversation as resolved.
Show resolved Hide resolved
}), {
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'}]
}]
})
});