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): handle shorthand fragment syntax #2316

Merged
merged 4 commits into from
Jun 23, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const docsUrl = require('../util/docsUrl');
// Rule Definition
// ------------------------------------------------------------------------------

const defaultOptions = {
checkFragmentShorthand: false
};

module.exports = {
meta: {
docs: {
Expand All @@ -21,16 +25,33 @@ module.exports = {
recommended: true,
url: docsUrl('jsx-key')
},
schema: []
schema: [{
type: 'object',
properties: {
checkFragmentShorthand: {
type: 'boolean',
default: defaultOptions.checkFragmentShorthand
}
},
additionalProperties: false
}]
},

create(context) {
const options = Object.assign({}, defaultOptions, context.options[0]);
const checkFragmentShorthand = options.checkFragmentShorthand;

function checkIteratorElement(node) {
if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) {
context.report({
node,
message: 'Missing "key" prop for element in iterator'
});
} else if (checkFragmentShorthand && node.type === 'JSXFragment') {
context.report({
node,
message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys'
});
}
}

Expand All @@ -52,6 +73,19 @@ module.exports = {
}
},

JSXFragment(node) {
if (!checkFragmentShorthand) {
return;
}

if (node.parent.type === 'ArrayExpression') {
context.report({
node,
message: 'Missing "key" prop for element in array. Shorthand fragment syntax does support providing keys'
});
}
},

// Array.prototype.map
CallExpression(node) {
if (node.callee && node.callee.type !== 'MemberExpression') {
Expand All @@ -66,7 +100,7 @@ module.exports = {
const isFn = fn && fn.type === 'FunctionExpression';
const isArrFn = fn && fn.type === 'ArrowFunctionExpression';

if (isArrFn && fn.body.type === 'JSXElement') {
if (isArrFn && (fn.body.type === 'JSXElement' || fn.body.type === 'JSXFragment')) {
checkIteratorElement(fn.body);
}

Expand Down
16 changes: 15 additions & 1 deletion tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/jsx-key');

const parsers = require('../../helpers/parsers');

const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
Expand All @@ -37,7 +39,9 @@ ruleTester.run('jsx-key', rule, {
{code: '[1, 2, 3].foo(x => <App />);'},
{code: 'var App = () => <div />;'},
{code: '[1, 2, 3].map(function(x) { return; });'},
{code: 'foo(() => <div />);'}
{code: 'foo(() => <div />);'},
{code: 'foo(() => <></>);', parser: parsers.BABEL_ESLINT},
{code: '<></>;', parser: parsers.BABEL_ESLINT}
],
invalid: [{
code: '[<App />];',
Expand All @@ -57,5 +61,15 @@ 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 => <>{x}</>);',
parser: parsers.BABEL_ESLINT,
options: [{checkFragmentShorthand: true}],
errors: [{message: 'Missing "key" prop for element in iterator. Shorthand fragment syntax does support providing keys'}]
}, {
code: '[<></>];',
parser: parsers.BABEL_ESLINT,
options: [{checkFragmentShorthand: true}],
errors: [{message: 'Missing "key" prop for element in array. Shorthand fragment syntax does support providing keys'}]
}]
});