Repository navigation
fix: closures in for-loop init capture wrong binding #18195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nicolo-ribaudo
merged 19 commits into
babel:main
from
joelle-a-dev:fix-for-init-closure-capture
Oct 5, 2026
+138
−11
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
118b003
fix: closures in for-loop init capture wrong binding
joelle-a-dev 6dd760a
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 95a1c29
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev f0df800
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev b987fbf
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev c8e9203
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev e1d4b5a
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 0a6014a
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 2b303e1
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 1d07498
fix: place head-closure snapshot right after its binding's declarator
joelle-a-dev 7feaa07
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 974a4e6
fix: remove incorrect claim from head-closure snapshot comment
joelle-a-dev b325f81
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev f1eb0af
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 4b2dab1
fix: copy head binding after the whole for initializer runs
joelle-a-dev 8d9fcce
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 17af7a2
fix: add proposed test case
joelle-a-dev 94046d2
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev 2140ea8
Merge branch 'main' into fix-for-init-closure-capture
joelle-a-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix: closures in for-loop init capture wrong binding
for (let i = 0, f = () => i; ...) creates a closure over i inside the loop head. That closure should keep seeing the value i had before the loop started, but the transform let it share the same variable the loop body mutates, so f() returned the wrong value. Now that closure gets its own copy of the value, frozen right after the loop head runs, so it stays correct no matter what the loop body does to i afterward. Fixes #18191
- Loading branch information
commit 118b00308a8a1f6c2e8e68b629451993d81a1876
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/babel-plugin-transform-block-scoping/test/fixtures/general/for-head-closure/exec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| let log = []; | ||
| for (let i = 0, f = () => i; i < 1; ) { | ||
| i = 42; | ||
| log.push(f()); | ||
| } | ||
| expect(log).toEqual([0]); |
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-18191/input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| for (let i = 0, f = () => i; i < 1; ) { | ||
| i = 42; | ||
| console.log(f()); | ||
| } |
3 changes: 3 additions & 0 deletions
3
...es/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-18191/options.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "plugins": ["transform-block-scoping"] | ||
| } |
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-18191/output.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| for (var i = 0, f = () => _i, _i = i; i < 1;) { | ||
| i = 42; | ||
| console.log(f()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.