Skip to content

fix(for-of): also check the names the head reads - #18261

Merged
JLHwung merged 3 commits into
babel:mainfrom
Kjubikstronk:fix-18253-for-of-shadowed-reference
Nov 14, 2026
Merged

JLHwung merged 3 commits into
babel:mainfrom
Kjubikstronk:fix-18253-for-of-shadowed-reference

Conversation

@Kjubikstronk

Copy link
Copy Markdown
Contributor
Q A
Fixed Issues? Fixes #18253
Patch: Bug Fix? 👍
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Fixed Issue #18253, reported by zloirock. The broken code I found was for (obj.value of [1]) { const obj = 2 }.

Since the loop is being rewritten and obj.value = ... is moved into the loop body, it hits the body's own obj instead of the outer one which causes a crash, or with the block-scoping plugin, it writes to the wrong variable!

This was actually already guarded against by wrapping the body in its own { } block. But it only triggered when the loop head declares a name the body also declares, for example: for (const x of …) { const x }. It never checked names the head only reads.

My fix:
Also check the names the head reads, that covers obj in obj.value, key in o[key], and obj in { a = obj }.

Tests: two new test files. One runs the code and crashes without the fix.

@github-actions

Copy link
Copy Markdown

Thanks for opening this pull request! 🎉

We really appreciate you taking the time to contribute, @Kjubikstronk.

A maintainer will take a look as soon as they can. In the meantime, please make sure that:

  • the description explains what changed and why
  • any related issues are linked
  • existing tests still pass

If anything needs adjusting we'll leave comments here. Thanks again!

@github-actions

Copy link
Copy Markdown

Mixed activity

Activity patterns show a mix of organic and automated signals.

View full analysis →

Evidence
  • Fork spike detected: Burst of 8 fork events in a single 24-hour window

This is an automated analysis by AgentScan

@babel-bot

babel-bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63210

@pkg-pr-new

pkg-pr-new Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 7296eab

@Kjubikstronk
Kjubikstronk force-pushed the fix-18253-for-of-shadowed-reference branch from da6ffde to 03056e9 Compare September 30, 2026 12:08
@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories and removed agentscan:mixed-signals labels Oct 5, 2026
Assisted-by: Claude
@Kjubikstronk

Copy link
Copy Markdown
Contributor Author

Is anything else needed here? It's had both approvals since 6 Oct, and the only change since is the comment fix in 95f2032.

rocktrila-prog

This comment was marked as spam.

@JLHwung
JLHwung added this pull request to the merge queue Nov 14, 2026
Merged via the queue into babel:main with commit 9456a5f Nov 14, 2026
52 checks passed
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18616.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: for-of member assignment targets a shadowing binding from the loop body

6 participants