Skip to content

feat(eslint-plugin): [no-misused-spread] add new rule #8509

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

Closed

Conversation

StyleShit
Copy link
Contributor

Closes #748

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @StyleShit!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Feb 19, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 07b69b8
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66f2f3bfacea6e00091a83c5
😎 Deploy Preview https://deploy-preview-8509--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (🟢 up 1 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

nx-cloud bot commented Feb 19, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 07b69b8. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link

codecov bot commented Feb 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.27%. Comparing base (896c731) to head (ab20e73).
Report is 239 commits behind head on main.

Current head ab20e73 differs from pull request most recent head 07b69b8

Please upload reports for the commit 07b69b8 to get more accurate results.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8509      +/-   ##
==========================================
- Coverage   88.73%   87.27%   -1.47%     
==========================================
  Files         426      252     -174     
  Lines       14881    12363    -2518     
  Branches     4327     3899     -428     
==========================================
- Hits        13205    10790    -2415     
+ Misses       1534     1303     -231     
- Partials      142      270     +128     
Flag Coverage Δ
unittest 87.27% <100.00%> (-1.47%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...kages/eslint-plugin/src/rules/no-misused-spread.ts 100.00% <100.00%> (ø)

... and 411 files with indirect coverage changes

@StyleShit
Copy link
Contributor Author

Not sure why the tests are failing, seems to pass locally

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Very clean! I like this a lot. Just a few touchup requests. ✨

Eevee Pokemon happily looking at sparkles, with sparkly eyes

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Feb 23, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Feb 27, 2024
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Progress! 🚀

Sorry, I'm only now realizing the class instance stuff might need an option. I think I've look at this PR too much - we should get someone else from @typescript-eslint/triage-team to look too. 😅

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Mar 5, 2024
@JoshuaKGoldberg JoshuaKGoldberg requested a review from a team March 5, 2024 17:39
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Mar 5, 2024
@auvred auvred added the awaiting response Issues waiting for a reply from the OP or another party label Mar 7, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Sep 10, 2024
@@ -5,7 +5,7 @@
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"lib": ["es2015", "es2017", "esnext"],
"lib": ["es2015", "es2017", "esnext", "DOM"],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this fixes the HTMLElement tests

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like it needs further clarification from #9648

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this caught an existing violation in our codebase:

/home/runner/work/typescript-eslint/typescript-eslint/packages/website/src/theme/NotFound/Content/index.tsx
  16:17  error  Using the spread operator on a string can cause unexpected behavior. Prefer `String.split('')` instead  @typescript-eslint/no-misused-spread

yarn lint on the repository will need to pass.

(posting this comment here because GitHub won't let me post on an unchanged file)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

haha, seems like we have conflicting rules here 😅

image

image

Copy link
Member

Choose a reason for hiding this comment

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

lol. cc @abrahamguo @kirkwaiblinger from #9834 - looks like this just blatantly disagrees with sindresorhus/eslint-plugin-unicorn#1489 -> https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-spread.md.

Maybe we should just not go for strings at all to start? Seems like maybe it's use-case specific?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have any strong opinion for either of the sides, so... 🤷‍♂️

(it's also more of a style thing rather than something that could cause errors, no?)

any objection to removing it @abrahamguo @kirkwaiblinger?

Copy link
Member

Choose a reason for hiding this comment

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

Here are my suggestions.

  1. Never ever use split(""). It doesn't do what you think it does for many scripts.
  2. [...str] is fine by principle but may still be surprising and not have the intended behavior, especially for emojis. In fact TC39 now considers strings being iterable as a mistake and all future built-in APIs that accept iterables will reject strings by default. If you really want to spread strings, either disable this rule or use Iterator.from, or think about alternatives such as Intl.Segmenter.
  3. An option to allow it is good enough. We can consider changing the default to true, but my vote is to be strict by default.

Copy link
Member

Choose a reason for hiding this comment

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

+1 from me to everything Josh has indicated. I think this is a really interesting case of the language going one direction (... iterable strings), lint rules following that direction, then the language realizing the direction being wrong. If other lint rules are asking to use ... on strings then we should file an issue on them saying they should update.

Copy link
Contributor

@abrahamguo abrahamguo Oct 8, 2024

Choose a reason for hiding this comment

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

@JoshuaKGoldberg Sure. However, if you are choosing between str.split('') vs [...str], don't you agree that [...str] is certainly more correct than str.split(''), since str.split('') will break surrogate pairs?

and therefore, the premise of unicorn/prefer-spread is correct, even if [...str] might not be the best choice?

Copy link
Member

Choose a reason for hiding this comment

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

To be honest, I'm coming back from a couple of weeks of intense conference organizing, and don't have the headspace to re-think this deeply right now. I trust @Josh-Cena's opinion on things. 😄

Copy link
Member

Choose a reason for hiding this comment

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

Both split("") and ... are only correct under certain assumptions, and practically, most of the time, the former is not much more wrong than the latter. It's fine for unicorn to recommend spreading since it's nominally safer, and if users accept safety at the level of spreading they are free to configure the allow config, but banning it by default exposes such unsafety.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Awesome!! I think the only blocker from me is fixing existing violations from this repo.

I took the liberty of merging the latest main in to fix the unrelated CI failures.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Sep 13, 2024
@StyleShit
Copy link
Contributor Author

StyleShit commented Sep 13, 2024

Awesome!! I think the only blocker from me is fixing existing violations from this repo.

@JoshuaKGoldberg

Seems like my most important comment here is hidden, so I'm assuming you haven't seen it 😅
#8509 (comment)

The tests will still fail 😞

@StyleShit
Copy link
Contributor Author

@JoshuaKGoldberg
Continuing this thread here, so it'll be easier for you to follow:

builtinSymbolLikes.ts and isSymbolFromDefaultLibrary.ts

Seems like both of them don't follow the references/aliases of WeakRef. When I try to check for isBuiltinSymbolLike(program, type, 'WeakRefConstructor') on a WeakRef, I get false, because WeakRef doesn't extend WeakRefConstructor, but references to it:
https://github.com/microsoft/TypeScript/blob/48f0b3cc383bfd6337fc9b5a0afdc1642473a57d/src/lib/es2021.weakref.d.ts#L23

Anyway... Solved with a list of builtins (I don't like it, but I have no other idea at this point 😞)

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Sep 24, 2024
@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Oct 9, 2024
@Josh-Cena
Copy link
Member

Hi @StyleShit do you think you could find some time recently to finish it? Otherwise we could also take it on since there isn't a lot left

@StyleShit
Copy link
Contributor Author

Hi @StyleShit do you think you could find some time recently to finish it? Otherwise we could also take it on since there isn't a lot left

@Josh-Cena

I'm currently on reserve duty so I don't know whether I'll have time in the near future 😓
If you have some time to finish this PR, that would be great!
(honestly, I don't remember how much work is left here)

Thank you! 🙏

@JoshuaKGoldberg
Copy link
Member

From #8443 (comment):

Cool! I'd really like to get this one and #8509 in by the end of the year, they're great rules to have that I've wanted for a while. So let's timebox these two to, say, the end of the month. If you don't end up having time by then, no worries, one of us on the team can send in a PR with the same commit history & a co-author attribution.

Good luck + best wishes on reserve duty. ❤️

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: prevent array, iterable and function spreads into objects
8 participants