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: add check for unique key #2701

Closed

Conversation

jzabala
Copy link
Contributor

@jzabala jzabala commented Jul 7, 2020

Add key uniqueness check in array literal and sibling JSX elements.

Closes #2614

Comment on lines +64 to +66
### `checkUniquePropKey` (default: `true`)

When `false` the validation for unique `key` in array literal and sibling JSX elements is disabled.
Copy link
Member

Choose a reason for hiding this comment

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

to avoid a breaking change, this also needs to default to false, just like checkFragmentShorthand.

Copy link
Member

Choose a reason for hiding this comment

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

(i can see an argument for it not being enabled by default, but i don't think it's worth the risk)

const reactPragma = pragmaUtil.getFromContext(context);
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);
const processedNodes = new Set();
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
const processedNodes = new Set();
const processedNodes = new WeakSet();

if (hasProp(node.openingElement.attributes, 'key')) {
return;
}
function nodeProcessed(node) {
Copy link
Member

Choose a reason for hiding this comment

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

this reads like it's a predicate, but it also has the side effect of marking the node as processed. maybe it needs a better name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you are right. 🤔 What do you think about checkAndMarkNodeProcessed? I am not crazy about this one but at least express better the behavior.

tests/lib/rules/jsx-key.js Show resolved Hide resolved
tests/lib/rules/jsx-key.js Show resolved Hide resolved
@jzabala jzabala force-pushed the feat/jsx-key-check-uniqueness branch from a1538c7 to 6cc9a00 Compare July 13, 2020 01:55
@jzabala jzabala changed the title [New]: : add check for unique key [New]jsx-key: add check for unique key Jul 13, 2020
@ljharb
Copy link
Member

ljharb commented Dec 30, 2020

Please mark this PR as ready for review once it is ready.

@ljharb ljharb marked this pull request as draft December 30, 2020 04:20
@ljharb
Copy link
Member

ljharb commented Feb 25, 2022

This was done in dbc8e8c / f51ec45.

@ljharb ljharb closed this Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Rule idea: jsx-unique-key
2 participants