Skip to content

Commit 7b2bcab

Browse files
committed
[Fix] jsx-key: detect missing keys in logical expressions
1 parent f9ee7ed commit 7b2bcab

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/rules/jsx-key.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ module.exports = {
147147
getReturnStatements(node.body)
148148
.filter((returnStatement) => returnStatement && returnStatement.argument)
149149
.forEach((returnStatement) => {
150+
const argument = returnStatement.argument;
150151
checkIteratorElement(returnStatement.argument);
152+
if (argument.type === 'LogicalExpression' && isJSX(argument.right)) {
153+
checkIteratorElement(argument.right);
154+
}
151155
});
152156
}
153157
}

tests/lib/rules/jsx-key.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ ruleTester.run('jsx-key', rule, {
205205
`,
206206
settings,
207207
},
208+
{ code: '[1, 2, 3].map(x => { return x && <App key={x} />; });' },
209+
{ code: '[1, 2, 3].map(x => { return x && y && <App key={x} />; });' },
210+
{ code: '[1, 2, 3].map(x => { return x && foo(); });' },
208211
]),
209212
invalid: parsers.all([
210213
{
@@ -424,5 +427,13 @@ ruleTester.run('jsx-key', rule, {
424427
options: [{ checkKeyMustBeforeSpread: true }],
425428
errors: [{ messageId: 'keyBeforeSpread' }],
426429
},
430+
{
431+
code: '[1, 2, 3].map(x => { return x && <App />; });',
432+
errors: [{ messageId: 'missingIterKey' }],
433+
},
434+
{
435+
code: '[1, 2, 3].map(x => { return x || y || <App />; });',
436+
errors: [{ messageId: 'missingIterKey' }],
437+
},
427438
]),
428439
});

0 commit comments

Comments
 (0)