Skip to content

fix(prefer-array-to-sorted): skip non-array iterable spreads#134

Merged
43081j merged 3 commits into
e18e:mainfrom
webpro:fix-prefer-array-to-sorted-set
Jun 27, 2026
Merged

fix(prefer-array-to-sorted): skip non-array iterable spreads#134
43081j merged 3 commits into
e18e:mainfrom
webpro:fix-prefer-array-to-sorted-set

Conversation

@webpro

@webpro webpro commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix prefer-array-to-sorted so it does not autofix spread-copy sorts when the spread source is known to be a non-array iterable.

This avoids unsafe rewrites like:

[...new Set(values)].sort()
[...versions].sort()
[...map.entries()].sort()

to invalid calls like:

new Set(values).toSorted()
versions.toSorted()
map.entries().toSorted()

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.ts
  • npm run lint
  • npm run build

@webpro
webpro force-pushed the fix-prefer-array-to-sorted-set branch 2 times, most recently from d5f872e to 4278c73 Compare June 27, 2026 09:09
Comment thread src/rules/prefer-array-to-sorted.ts Outdated
const NON_ARRAY_COPY_SOURCE_CTORS = new Set([
'Set',
'Map',
'WeakSet',

@43081j 43081j Jun 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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(?)])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nice catch, let's keep it focused to "spreadables"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
webpro force-pushed the fix-prefer-array-to-sorted-set branch from 4278c73 to eb9d841 Compare June 27, 2026 10:15
43081j
43081j previously approved these changes Jun 27, 2026
@43081j
43081j force-pushed the fix-prefer-array-to-sorted-set branch from ea80c19 to c3a3c2f Compare June 27, 2026 14:41
Variable IDs are the same as their declarator's name, so no need
checking it again.
@43081j
43081j merged commit cb0b86e into e18e:main Jun 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants