Skip to content

Wrap enclosing loop body when a nested for-head closure captures a let binding - #18337

Open
Marve10s wants to merge 1 commit into
babel:mainfrom
Marve10s:fix/nested-for-head-closure
Open

Marve10s wants to merge 1 commit into
babel:mainfrom
Marve10s:fix/nested-for-head-closure

Conversation

@Marve10s

Copy link
Copy Markdown
Q A
Fixed Issues? Fixes #18200
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? No
Need backport? 7.x
License MIT

The loop-body binding collector skipped nested loops, so a closure in a nested for (let x = ...) initializer captured one shared x across iterations of the enclosing loop. The collector now reports those head bindings when the initializer captures them, so the enclosing loop gets its per-iteration wrapper, and the existing for-head-closure-nested fixture drops its TODO and asserts [0, 1].

transform-block-scoping lowers a nested `for (let x = ...)` to `var`, so
a closure created inside that initializer captured one function-scoped
binding across every iteration of the enclosing loop. The loop-body
binding collector skipped nested loops entirely, so the enclosing loop
never learned that its body needed a per-iteration wrapper.

Collect a nested ForStatement's let/const head bindings when a closure in
its initializer captures them, so the enclosing loop wraps its body and
each entry of the nested loop gets a fresh binding. The existing
for-head-closure-nested fixture now asserts the spec result instead of
the TODO.

Fixes babel#18200

Assisted-by: Claude Fable 5.1
@github-actions

Copy link
Copy Markdown

Thanks for opening this pull request! 🎉

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

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!

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T09:33:24.119425Z 51db2da PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@babel-bot

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Oct 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: 51db2da

This branch has not been deployed

No deployments
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.

transform-block-scoping: closures share a binding when a nested for-loop head has an initializer

2 participants