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

linter: false positive in unicorn/no-useless-spread #4872

Closed
camc314 opened this issue Aug 13, 2024 · 11 comments · Fixed by #4944
Closed

linter: false positive in unicorn/no-useless-spread #4872

camc314 opened this issue Aug 13, 2024 · 11 comments · Fixed by #4944
Assignees
Labels
A-linter Area - Linter C-bug Category - Bug

Comments

@camc314
Copy link
Collaborator

camc314 commented Aug 13, 2024

the following code proiduces an error on the ...

const test2 = [...new Set(test || [])];

https://oxc-project.github.io/oxc/playground/?code=3YCAAICogICAgICAgICxG0qZRraXVswdRc9QqqX01RSLQj6CIL60mOY7cTm3PXoTnCmrrStudD9%2FdpNkgA%3D%3D

this is incorrect as new Set() returns a Set where as [...new Set(test || [])] returns an array.

looks like this regressed in #4791 ?

@camc314 camc314 added the C-bug Category - Bug label Aug 13, 2024
@DonIsaac
Copy link
Collaborator

It did come from there, but it was intentional. The thought is that most code written like this doesn't necessarily intending to cast a set into an array, and this instead indicates a bug.

Could we suggest that users use Array.from() to cast iterables into arrays in these cases?

@DonIsaac DonIsaac added the A-linter Area - Linter label Aug 13, 2024
@camc314
Copy link
Collaborator Author

camc314 commented Aug 13, 2024

hmm to me, this feels like quite a common case? a user wants to remove any duplicate elements from the array.

maybe set is special here and has different semantics?

@DonIsaac
Copy link
Collaborator

I'm ok with changing this behavior if such usage is commonplace. Did you run into this because of a linter diagnostic in one of your codebases? Do you have examples of this being done elsewhere?

@camc314
Copy link
Collaborator Author

camc314 commented Aug 13, 2024

yeah we've got it in a bunch of places in our internal codebases.

searching GitHub also reveals ~155k results:

https://github.com/search?q=%22%5B...new+Set%22+AND+%28language%3AJavaScript+OR+language%3ATypeScript+%29&type=code

searching google for "remove duplicate values from array", and this seems to be the main solution (some solutions suggest .filter and .includes which feels painfully slow)

Ref:

@DonIsaac
Copy link
Collaborator

👀 guess we're changing this then

@DonIsaac
Copy link
Collaborator

I'm currently moving apartments, but I'll address this when I have time. I have a particular solution in mind.

@camc314
Copy link
Collaborator Author

camc314 commented Aug 14, 2024

does this seem like a reasonable use case for ... and new Set? maybe we could have an option that turns this behaviour off

@DonIsaac
Copy link
Collaborator

This does seem reasonable and used quite frequently, based on the links you sent. I'm ok with removing it straight up. Thoughts?

@camc314
Copy link
Collaborator Author

camc314 commented Aug 14, 2024

sounds good, yeah i think it's gonna be a common use case - we shouldn't report on it

@craftedsystems
Copy link

Please remove this as it is just wrong. :)

@danielfrenda
Copy link

Another thing to point out is pairing this with the prefer-spread rule is annoying.
It seems eslint's no-useless-spread already accounts for this and wont report on [...new Set( )]
Which then makes an // eslint ignore a pain, where its needed for oxlint but not for eslint, which autofixes the unused ignore directive away causing oxlint to fail

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

Successfully merging a pull request may close this issue.

4 participants