Skip to content

Add no-for-loop-extra-variable rule for cached array-length loops#2919

Open
juarezsousa-ctrl wants to merge 6 commits intosindresorhus:mainfrom
juarezsousa-ctrl:fix/no-for-loop-extra-variable
Open

Add no-for-loop-extra-variable rule for cached array-length loops#2919
juarezsousa-ctrl wants to merge 6 commits intosindresorhus:mainfrom
juarezsousa-ctrl:fix/no-for-loop-extra-variable

Conversation

@juarezsousa-ctrl
Copy link
Copy Markdown

Fixes #295

This adds a new unicorn/no-for-loop-extra-variable rule to handle the specific cached array-length for loop shape discussed in #295.

Context:

This PR takes a narrower approach:

  • only targets the conservative i = 0, j = array.length pattern
  • only fixes local, unambiguous cases
  • skips ambiguous cases instead of trying to hoist or preserve cross-scope usages
  • rewrites the loop into a simpler form so existing rules can operate on it afterward

no-for-loop itself is left unchanged in this PR.

Included:

  • new no-for-loop-extra-variable rule
  • dedicated tests for supported and skipped cases
  • rule export wiring
  • docs/readme entry

@juarezsousa-ctrl juarezsousa-ctrl force-pushed the fix/no-for-loop-extra-variable branch from 9cc676b to 89d4146 Compare March 28, 2026 18:33
@juarezsousa-ctrl
Copy link
Copy Markdown
Author

Rebased on latest main, resolved the conflict, and reran the checks.

All checks are passing now.

@sindresorhus sindresorhus force-pushed the main branch 2 times, most recently from 814b622 to 0e023e3 Compare April 2, 2026 12:41
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.

A case not handled by no-for-loop

1 participant