fix(prefer-array-to-sorted): skip non-array iterable spreads#134
Merged
Conversation
webpro
force-pushed
the
fix-prefer-array-to-sorted-set
branch
2 times, most recently
from
June 27, 2026 09:09
d5f872e to
4278c73
Compare
43081j
reviewed
Jun 27, 2026
| const NON_ARRAY_COPY_SOURCE_CTORS = new Set([ | ||
| 'Set', | ||
| 'Map', | ||
| 'WeakSet', |
Contributor
There was a problem hiding this comment.
weak sets and weak maps can't be iterated FYI.
not meaning this is wrong but the examples wouldn't work in the tests ([...new WeakSet(?)])
Contributor
Author
There was a problem hiding this comment.
nice catch, let's keep it focused to "spreadables"
Contributor
Author
There was a problem hiding this comment.
Fixed in eb9d841: removed WeakSet/WeakMap from the constructor/type lists and dropped the invalid WeakSet spread test. The guard now stays focused on iterable non-array spread sources like Set, Map, and iterator-returning calls.
…t type info Avoid unsafe untyped autofixes for spread-copy sorts whose source is known not to be an array. These rewrites can otherwise turn Set/Map/iterator sources into invalid `.toSorted()` calls. Bail on known collection constructors, local identifiers initialized from those constructors, iterator-returning `.entries()`/`.keys()`/`.values()` calls, and TS syntax-only iterable annotations when type services are unavailable. Keep `Object.entries()` and known array sources fixable. Verified with the focused rule test, lint, build, and a consumer probe across representative checked-out projects.
webpro
force-pushed
the
fix-prefer-array-to-sorted-set
branch
from
June 27, 2026 10:15
4278c73 to
eb9d841
Compare
43081j
previously approved these changes
Jun 27, 2026
43081j
force-pushed
the
fix-prefer-array-to-sorted-set
branch
from
June 27, 2026 14:41
ea80c19 to
c3a3c2f
Compare
Variable IDs are the same as their declarator's name, so no need checking it again.
43081j
approved these changes
Jun 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
prefer-array-to-sortedso it does not autofix spread-copy sorts when the spread source is known to be a non-array iterable.This avoids unsafe rewrites like:
to invalid calls like:
The rule now bails for known collection constructors, local identifiers initialized from those constructors, iterator-returning
.entries()/.keys()/.values()calls, and TS syntax-only iterable annotations when type services are unavailable.Object.entries()remains fixable.Verification
npm test -- src/rules/prefer-array-to-sorted.test.tsnpm run lintnpm run build