Skip to content

fix: wrap enclosing loop when a nested for head closure captures its bindings - #18331

Merged
JLHwung merged 5 commits into
babel:mainfrom
joellemd:fix-nested-for-init-closure
Oct 20, 2026
Merged

JLHwung merged 5 commits into
babel:mainfrom
joellemd:fix-nested-for-init-closure

Conversation

@joellemd

@joellemd joellemd commented Oct 9, 2026

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

When a let binding in a nested for loop's head is captured by a closure in that same head (ex: for (let i = k, f = () => i; ...)), the binding becomes a function-scoped var and is shared across iterations of the enclosing loop, so every f returns the last value. The enclosing loop body is now wrapped in _loop() in this case, so each outer iteration gets fresh head bindings. This resolves the TODO in the for-head-closure-nested fixture and adds a fixture for the case where the binding isn't mutated.

…bindings

Signed-off-by: Joelle Mahfoud <joelle_mahfoud@live.com>
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

Thanks for opening this pull request! 🎉

We really appreciate you taking the time to contribute, @joelle-a-dev.

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!

@pkg-pr-new

pkg-pr-new Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 25910ac

@joellemd

Copy link
Copy Markdown
Contributor Author

@nicolo-ribaudo @JLHwung if you can please review this, addressing the todo from previous PR

@babel-bot

babel-bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

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

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add an input/output test?

@joellemd

Copy link
Copy Markdown
Contributor Author

@nicolo-ribaudo done

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Oct 19, 2026

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@JLHwung
JLHwung merged commit 07261cb into babel:main Oct 20, 2026
71 checks passed
@babel-bot

Copy link
Copy Markdown
Collaborator

⚠️ The automatic backport to 7.x failed.

This PR builds on #18195 (closures in for-loop init capture wrong binding), which is not on 7.x. The loop.ts change relies on inHead from relativeLoopLocation, which #18195 introduced. The fixtures this PR modifies also don't exist on 7.x.

Conflicting file:

This PR needs to be backported manually, after #18195 is backported.

Generated by 🔙 Backport to 7.x for #18331 · copilot · auto · 14.3 AIC · ⌖ 0.554 AIC · ⊞ 9.1K · ◷

@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18370.

nicolo-ribaudo pushed a commit that referenced this pull request Oct 20, 2026
…bindings (#18370)

fix: wrap enclosing loop when a nested for head closure captures its bindings (#18331)

backport of 07261cb

Co-authored-by: Joelle Mahfoud <joelle_mahfoud@live.com>
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.

4 participants