Skip to content

feat(jsx-key): check for array related method calls with jsx elements/fragments #3938

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

SimonSchick
Copy link

WIP, pending initial feedback, slightly worried this check may be overly broad.

Things to add:

  • Setting to make this behavior opt-in unless we want it to be the default.
  • Optimization of esquery usage? Seems potentially very wasteful.
  • Check methods for exhaustiveness, there (fill, toSpliced) being more exotic candidates

Fixes #3937

@ljharb ljharb marked this pull request as draft July 9, 2025 05:47
@ljharb
Copy link
Member

ljharb commented Jul 9, 2025

I'm not sure it's a good idea to assume any method named "push" or "splice" is happening on an array, unless it's a TS codebase.

@SimonSchick
Copy link
Author

That was kinda my thinking too but I'm unsure how much better we can do without types.

I thought about trying to trace the symbol we are pushing to and see if it is a array declaration but that feels very much like a hack.

On the other hand I'd assume push, unshift and concat are the most commonly used methods for this anti-pattern and these are not exactly common method names in in relation to jsx usage so false positives should be extremely rare.

Question it if it is rare enough to proceed with this vague targetting strategy?

I did some rough searching via https://github.com/search?type=code&q=%28path%3A*.tsx+OR+path%3A*.jsx%29+%2F.%2B%5C.push%5C%28%3C.%2B%5C%29%3B%2F and found nothing that stands out, had to vary the search query a fair bit since github code search seems to limit results to 100 for some reason.

@ljharb
Copy link
Member

ljharb commented Jul 9, 2025

concat exists on a number of things, and i agree false positives will be rare, but nonzero is nonzero.

Adding this change under an option is smart if it has a nonzero risk of false positives, though.

@SimonSchick
Copy link
Author

I'll add an option then, and also fix the failing tests for legacy versions (I really wish those get pruned 😢 ).

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.

[Bug]: react/jsx-key does not detect usage of Array.push etc.
2 participants