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

Added support (without options) for spread arguments with keys #2664

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
28 changes: 26 additions & 2 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const pragmaUtil = require('../util/pragma');
// ------------------------------------------------------------------------------

const defaultOptions = {
checkFragmentShorthand: false
checkFragmentShorthand: false,
allowSpreadKeys: true // TODO: When React separates keys from props this should become false
};

module.exports = {
Expand All @@ -32,6 +33,10 @@ module.exports = {
checkFragmentShorthand: {
type: 'boolean',
default: defaultOptions.checkFragmentShorthand
},
allowSpreadKeys: {
type: 'boolean',
default: defaultOptions.allowSpreadKeys
}
},
additionalProperties: false
Expand All @@ -41,11 +46,30 @@ module.exports = {
create(context) {
const options = Object.assign({}, defaultOptions, context.options[0]);
const checkFragmentShorthand = options.checkFragmentShorthand;
const allowSpreadKeys = options.allowSpreadKeys;
const reactPragma = pragmaUtil.getFromContext(context);
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);

function checkIteratorElement(node) {
if (node.type === 'JSXElement' && !hasProp(node.openingElement.attributes, 'key')) {
if (node.type === 'JSXElement') {
const hasAKeyAttribute = hasProp(node.openingElement.attributes, 'key');

const hasASpreadArgumentWithAKeyProperty = allowSpreadKeys && node.openingElement
&& node.openingElement.attributes
&& node.openingElement.attributes.some(
({argument}) => argument && argument.properties && argument.properties.some(
(property) => property.key.name === 'key'));

const hasAnObjectSpreadArgument = allowSpreadKeys && node.openingElement
&& node.openingElement.attributes
&& node.openingElement.attributes.some(
({argument}) => argument && argument.type === 'Identifier');

const isValidElement = hasAKeyAttribute
|| hasASpreadArgumentWithAKeyProperty
|| hasAnObjectSpreadArgument;
if (isValidElement) return;

context.report({
node,
message: 'Missing "key" prop for element in iterator'
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ ruleTester.run('jsx-key', rule, {
{code: 'fn()'},
{code: '[1, 2, 3].map(function () {})'},
{code: '<App />;'},
{
code: '[1, 2, 3].map(x => <App {...{ key }} />);',
options: [{allowSpreadKeys: true}]
},
{
code: '[1, 2, 3].map(x => <App {...objectWithKey} />);',
options: [{allowSpreadKeys: true}]
},
{code: '[<App key={0} />, <App key={1} />];'},
{code: '[1, 2, 3].map(function(x) { return <App key={x} /> });'},
{code: '[1, 2, 3].map(x => <App key={x} />);'},
Expand Down Expand Up @@ -88,5 +96,14 @@ 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'}]
}, {
code: '[1, 2, 3].map(x => <App {...{ key }} />);',
options: [{allowSpreadKeys: false}],
errors: [{message: 'Missing "key" prop for element in iterator'}]
},
{
code: '[1, 2, 3].map(x => <App {...objectWithKey} />);',
options: [{allowSpreadKeys: false}],
errors: [{message: 'Missing "key" prop for element in iterator'}]
})
});