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

no-useless-spread false positive when spreading a Set() #4980

Closed
jelly opened this issue Aug 19, 2024 · 3 comments
Closed

no-useless-spread false positive when spreading a Set() #4980

jelly opened this issue Aug 19, 2024 · 3 comments
Labels
C-bug Category - Bug

Comments

@jelly
Copy link
Contributor

jelly commented Aug 19, 2024

The following warning has an fixer available which transforms the code into

  ⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
     ╭─[src/helpers.js:703:19]
 702 │
 703 │     const uniq = [...new Set(devs)];
     ·                   ───
 704 │     uniq.sort();
     ╰────
  help: `new Set(devs)` returns a new array. Spreading it into an array expression to create a new array is redundant.
-    const uniq = [...new Set(devs)];
+    const uniq = [new Set(devs)];

This would fails as a Set has no sort(), but maybe more importantly the spread operator isn't useless as removing it produces different code as can be seen below:

> new Set([1,2,3,3]).sort()
Uncaught TypeError: (intermediate value).sort is not a function
> [...new Set([1,2,3,3])].sort()
[ 1, 2, 3 ]
> [new Set([1,2,3,3])].sort()
[ Set(3) { 1, 2, 3 } ]
@jelly jelly added the C-bug Category - Bug label Aug 19, 2024
@jelly jelly changed the title no-useless-spread fixers produces broken code and flags for a false posiitve no-useless-spread false positive when spreading a Set() Aug 19, 2024
@camc314
Copy link
Collaborator

camc314 commented Aug 19, 2024

duplicate of #4872 ? can you confirm?

@jelly
Copy link
Contributor Author

jelly commented Aug 19, 2024

duplicate of #4872 ? can you confirm?

Sorry, should have tested main. This is indeed fixed.

@jelly jelly closed this as completed Aug 19, 2024
@camc314
Copy link
Collaborator

camc314 commented Aug 19, 2024

duplicate of #4872 ? can you confirm?

Sorry, should have tested main. This is indeed fixed.

no worries, thanks for verifying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category - Bug
Projects
None yet
Development

No branches or pull requests

2 participants