Skip to content

fix: assign closure body after handling return - #18193

Merged
nicolo-ribaudo merged 1 commit into
babel:mainfrom
JLHwung:fix-18192
Aug 25, 2026
Merged

nicolo-ribaudo merged 1 commit into
babel:mainfrom
JLHwung:fix-18192

Conversation

@JLHwung

@JLHwung JLHwung commented Aug 19, 2026 •

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

The root cause of #18192 is data inconsistency: When returnNum > 0, we wrap the return statement collected in state.returns within the { v: ... } object wrapper. However, the state.returns is a NodePath collected at the start of wrapLoopBody, while the emit output is from the loop body assigned to the loop closure body after the NodePath is collected: Therefore, changes to the NodePath here does not sync to the closure body and we see unwrapped return statement in the final output.

The wrapLoopBody essentially moves the loop body to the loop closure body. To avoid duplicate nodes during the transform, we move the loop body reassigning step to the end of the wrapLoopBody transform, in order to ensure that any AST changes in the loop body will be reflected on the loop closure.

@JLHwung JLHwung added the PR: Bug Fix 馃悰 A type of pull request used for our changelog categories label Aug 19, 2026
@babel-bot

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Aug 19, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: 2750fd3

@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.

Thanks for the detailed description :)

@nicolo-ribaudo
nicolo-ribaudo merged commit 2d762de into babel:main Aug 25, 2026
104 of 105 checks passed
@nicolo-ribaudo
nicolo-ribaudo deleted the fix-18192 branch August 25, 2026 12:39
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.

block-scoping transform doesn't wrap loop return value when for-loop body is not a BlockStatement

4 participants