Skip to content

fix(prefer-array-at): guard against more assignment cases where .at(-1) would be illegal - #149

Merged
43081j merged 1 commit into
e18e:mainfrom
TkDodo:tkdodo/improve-prefer-array-at
Aug 5, 2026
Merged

fix(prefer-array-at): guard against more assignment cases where .at(-1) would be illegal#149
43081j merged 1 commit into
e18e:mainfrom
TkDodo:tkdodo/improve-prefer-array-at

Conversation

@TkDodo

@TkDodo TkDodo commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

This PR makes the prefer-array-at rule more resilient to assignment cases:

  1. left-hand assignments
    like items[items.length - 1]! = 42 or items[items.length - 1]! += 42 are no longer transformed because they would lead to TS2364. Note that type assertions and non-null assertions no the target side needed explicit checking. This is what was reported in prefer-array-at produces invalid code: #148, normal assignments without ! were already properly checked.

  2. for-in and for-of assignments
    while uncommon, can lead to Uncaught ReferenceError: Invalid left-hand side in for-loop

  3. Destructuring
    also uncommon, but can lead to Uncaught SyntaxError: Invalid destructuring assignment target

  4. delete
    transforming delete items[items.length - 1] into delete items.at(-1) yields no runtime error, but doesn’t delete anything and is therefore wrong.

fixes #148


Note: This PR was created with the help of AI, but a human (me) was always in the loop and no communication was done using AI. I am not a meat proxy.

@43081j
43081j merged commit 5331f8e into e18e:main Aug 5, 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.

prefer-array-at produces invalid code:

2 participants