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

[New]: jsx-key: added checkKeyMustBeforeSpread option for new jsx transform #2835

Merged
merged 1 commit into from Oct 20, 2020

Conversation

morlay
Copy link
Contributor

@morlay morlay commented Oct 19, 2020

@morlay
Copy link
Contributor Author

morlay commented Oct 19, 2020

@ljharb should be default: true ?

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The default can't be true, or that'd be a breaking change.

Comment on lines 72 to 87
let keyAttributeIndex = -1;
let spreadAttributeIndex = -1;

attributes.forEach((attribute, i) => {
if (attribute.type === 'JSXSpreadAttribute') {
spreadAttributeIndex = i;
}

if (attribute.type === 'JSXAttribute') {
if (propName(attribute) === 'key') {
keyAttributeIndex = i;
}
}
});

return spreadAttributeIndex !== -1 && keyAttributeIndex > spreadAttributeIndex;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let keyAttributeIndex = -1;
let spreadAttributeIndex = -1;
attributes.forEach((attribute, i) => {
if (attribute.type === 'JSXSpreadAttribute') {
spreadAttributeIndex = i;
}
if (attribute.type === 'JSXAttribute') {
if (propName(attribute) === 'key') {
keyAttributeIndex = i;
}
}
});
return spreadAttributeIndex !== -1 && keyAttributeIndex > spreadAttributeIndex;
var hasFoundSpread = false;
return attributes.some((attribute) => {
if (attribute.type === 'JSXSpreadAttribute') {
hasFoundSpread = true;
return false;
}
if (attribute.type !== 'JSXAttribute') {
return false;
}
return !hasFoundSpread || propName(attribute) === 'key';
});

what about this alternative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasFoundSpread && propName(attribute) === 'key' may be clear

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, in that case you'd need to negate the overarching .some.

lib/rules/jsx-key.js Outdated Show resolved Hide resolved
tests/lib/rules/jsx-key.js Outdated Show resolved Hide resolved
tests/lib/rules/jsx-key.js Outdated Show resolved Hide resolved
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks great! If there's further tweaking needed for the TS tests in older eslint/node, I'll handle that.

@ljharb ljharb changed the title [feat]: added option checkKeyMustBeforeSpread of react/jsx-key [New]: jsx-key: added checkKeyMustBeforeSpread option for new jsx transform Oct 20, 2020
@ljharb ljharb added the hacktoberfest-accepted If you're interested in a free shirt, this PR counts towards it. label Oct 20, 2020
@ljharb ljharb merged commit 9f0d5c4 into jsx-eslint:master Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement hacktoberfest-accepted If you're interested in a free shirt, this PR counts towards it.
Development

Successfully merging this pull request may close these issues.

Enhancement: added option checkKeyMustBeforeSpread of react/jsx-key
2 participants