Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const pragmaUtil = require('../util/pragma');
const report = require('../util/report');
const astUtil = require('../util/ast');
const getText = require('../util/eslint').getText;
const isJSX = require('../util/jsx').isJSX;

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -146,7 +147,12 @@ module.exports = {
getReturnStatements(node.body)
.filter((returnStatement) => returnStatement && returnStatement.argument)
.forEach((returnStatement) => {
checkIteratorElement(returnStatement.argument);
const argument = returnStatement.argument;
if (argument.type === 'LogicalExpression' && isJSX(argument.right)) {
checkIteratorElement(argument.right);
} else {
checkIteratorElement(argument);
}
});
}
}
Expand All @@ -159,18 +165,17 @@ module.exports = {
*/
function checkArrowFunctionWithJSX(node) {
const isArrFn = node && node.type === 'ArrowFunctionExpression';
const shouldCheckNode = (n) => n && (n.type === 'JSXElement' || n.type === 'JSXFragment');
if (isArrFn && shouldCheckNode(node.body)) {
if (isArrFn && isJSX(node.body)) {
checkIteratorElement(node.body);
}
if (node.body.type === 'ConditionalExpression') {
if (shouldCheckNode(node.body.consequent)) {
if (isJSX(node.body.consequent)) {
checkIteratorElement(node.body.consequent);
}
if (shouldCheckNode(node.body.alternate)) {
if (isJSX(node.body.alternate)) {
checkIteratorElement(node.body.alternate);
}
} else if (node.body.type === 'LogicalExpression' && shouldCheckNode(node.body.right)) {
} else if (node.body.type === 'LogicalExpression' && isJSX(node.body.right)) {
checkIteratorElement(node.body.right);
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ ruleTester.run('jsx-key', rule, {
`,
settings,
},
{ code: '[1, 2, 3].map(x => { return x && <App key={x} />; });' },
{ code: '[1, 2, 3].map(x => { return x && y && <App key={x} />; });' },
{ code: '[1, 2, 3].map(x => { return x && foo(); });' },
]),
invalid: parsers.all([
{
Expand Down Expand Up @@ -424,5 +427,13 @@ ruleTester.run('jsx-key', rule, {
options: [{ checkKeyMustBeforeSpread: true }],
errors: [{ messageId: 'keyBeforeSpread' }],
},
{
code: '[1, 2, 3].map(x => { return x && <App />; });',
errors: [{ messageId: 'missingIterKey' }],
},
{
code: '[1, 2, 3].map(x => { return x || y || <App />; });',
errors: [{ messageId: 'missingIterKey' }],
},
]),
});