eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

[no-useless-spread] Handle more cases for `toSorted` (and the likes)

Open boris-petrov opened this issue 1 year ago • 4 comments

no-useless-spread already handles cases like [...foo.toSorted()] which is awesome! However, it would be nice to also handle [...foo].toSorted() and [...foo].sort(), both of which can be replaced with foo.toSorted().

cc @fisker, @sindresorhus

P.S. Similar work was done in this PR.

boris-petrov avatar Oct 29 '23 12:10 boris-petrov

[...foo].toSorted()

is not safe to fix, since foo can be other types like string, Set..

Or even can't be reported.

fisker avatar Oct 29 '23 14:10 fisker

[...foo].sort()

Should definitely go into a new rule like prefer-array-to-sorted.

fisker avatar Oct 29 '23 14:10 fisker

Wouldn't this suffer from the same problem — foo could still be a string, Set, etc. — and so [...foo].sort() cannot be reported either?

abrahamguo avatar Nov 29 '23 23:11 abrahamguo

I think it's reasonable to make a generic rule just to forbid .sort() except expression statements.

fisker avatar Nov 30 '23 03:11 fisker