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

feat(rules): add jsx-props-no-spread-multi #3724

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

SimonSchick
Copy link

@SimonSchick SimonSchick commented Apr 3, 2024

Please see notes in code.

I think this rule has value in catching some niche bugs, I personally only saw this ~3-4 times throughout my career.

Copy link

codecov bot commented Apr 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.76%. Comparing base (e4ecbcf) to head (d366ac0).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3724   +/-   ##
=======================================
  Coverage   97.76%   97.76%           
=======================================
  Files         133      134    +1     
  Lines        9467     9485   +18     
  Branches     3467     3470    +3     
=======================================
+ Hits         9255     9273   +18     
  Misses        212      212           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

configs/recommended.js Outdated Show resolved Hide resolved
Examples of **incorrect** code for this rule:

```jsx
<App {...props} myAttr='1' {...props} />
Copy link
Author

Choose a reason for hiding this comment

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

TODO: Should I add more expression examples eg. function calls?

const hashes = new Set();
for (const spread of spreads) {
// TODO: Deep compare ast function?
const hash = JSON.stringify(spread, propertyFilter);
Copy link
Author

Choose a reason for hiding this comment

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

This is admittedly somewhat of a bandaid.
I used a various of deepEqual before to compare all possible matches (which is also O(n^2)).
This performs pretty well and generally has been generating small 'hashes' however I am worried of scenarios where developers create excessively large/syntactically complex spreads.

In these cases we could perform a simple md5/sha hashing though this may be overkill.

Copy link
Author

Choose a reason for hiding this comment

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

Optimized it a little by only hashing the argument rather than the full node.


type TypeDeclarationBuilder = (annotation: ASTNode, parentName: string, seen: Set<typeof annotation>) => object;
type TypeDeclarationBuilder = (
Copy link
Author

Choose a reason for hiding this comment

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

This was auto-formatted, I assume this is ok?

return {
JSXOpeningElement(node) {
const spreads = node.attributes.filter((attr) => attr.type === 'JSXSpreadAttribute');
if (spreads.length < 2) {
Copy link
Author

Choose a reason for hiding this comment

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

Would be nice to have some stats on how many spreads exists on average per component, though this probably covers ~99.99% of all component instantiations.

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.

Adding a new rule has a cost, and if you’ve only seen it 3-4 times (i never have), i don’t think it’ll be that useful. (Generally it’s better to discuss a new rule in an issue first so it doesn’t feel like work is wasted in the event it’s not merged)

@SimonSchick
Copy link
Author

No hard feelings if it's not merged, I understand it's more of a niche thing, worst case I will port it over to my own plugin :)

@SimonSchick
Copy link
Author

@ljharb do you have any suggestions where I could sample this rule against a larger set of JSX code in the wild? Github search hasn't been particularly helpful due to the lack of ast node search.

I ran this rule again some internal code bases and found 2 more violations which is still not statistically significant but an indicator that this may be more wide-spread than I thought.

@SimonSchick
Copy link
Author

Any interest/thoughts on this? If not I will close this PR eow and move into a separate library.

@ljharb
Copy link
Member

ljharb commented Apr 22, 2024

@SimonSchick we use https://www.npmjs.com/package/eslint-remote-tester to test things weekly, that might be worth looking into?

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.

None yet

2 participants