Skip to content

fix: wrap loop closures inside transformed async generators - #18684

Merged
nicolo-ribaudo merged 4 commits into
babel:mainfrom
hoanle0126:fix/async-generator-block-scoping-loop-return
Nov 23, 2026
Merged

nicolo-ribaudo merged 4 commits into
babel:mainfrom
hoanle0126:fix/async-generator-block-scoping-loop-return

Conversation

@hoanle0126

@hoanle0126 hoanle0126 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Fixed Issues? Fixes #18683
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

Because async generator lowering runs before block scoping, the extracted loop helper became a sync generator using yield*, which broke awaiting in a finally block on .return().

This records lowered async generators on file state so block scoping wraps the loop helper with wrapAsyncGenerator and asyncGeneratorDelegate, but only when the loop body actually has a try/finally outside nested functions.

@babel-bot

babel-bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: db26e68

@JLHwung

JLHwung commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Per our AI Policy, you should disclose AI usage in the commit trailer.

@hoanle0126
hoanle0126 force-pushed the fix/async-generator-block-scoping-loop-return branch from e587fcf to dca8156 Compare November 21, 2026 13:47
@hoanle0126

Copy link
Copy Markdown
Contributor Author

Thanks for the reminder @JLHwung! I've amended the commit to include the Assisted-by: Gemini 4 Neon trailer.

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for opening the PR.

The fix is correct, but currently it makes every closure-capturing loop in a compiled async generator about 3x slower.

Every loop that captures a variable inside a compiled async generator now builds an async generator and a delegate on each iteration, even without try/finally. The original issue is that await under .return() in a finally is not correctly handled. Since await can only run here, the async wrapping is only required when the loop body has a try with finally block. Of course when checking this pattern we should skip around function boundaries.

The PR description seems generated from LLM, you should have paraphrased it using your own words, it help you understand what your LLM is actually doing.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Good catch @JLHwung. I updated the loop transform to only wrap with wrapAsyncGenerator and asyncGeneratorDelegate when the loop body has a try/finally outside nested functions, added fixtures for the non-finally and nested-function cases, and rewrote the PR description.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
FunctionParent(path) {

With this change you can probably get rid of the findParent check in TryStatement.


let generatorNode: t.Function = originalNode;
if (isMethod) {
const retArg = (path.node.body.body.at(-1) as t.ReturnStatement)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here we rely on the output shape of the helper-wrap-function, if that helper changes, it will throw a very confusing error here.

Consider to let classOrObjectMethod of the helper-wrap-function reuse the original BlockStatement for container, and give the method a fresh block for the return wrap(...)() statement. Then the async-generator-functions can record originalNode.body before calling remapAsyncToGenerator, so we can drop the isMethod branch.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Applied both suggestions @JLHwung: classOrObjectMethod now reuses the original block statement so we record the body directly before wrapping, and the loop visitor uses FunctionParent so the try/finally check no longer needs parent traversal.

),
),
];
]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

here we create a fresh block statement, but we forgot to move the directives from the old block to the new one. So "use strict" in an object method within a sloppy script now lost the directive.

Please add a new input as well as exec test for this case.

@JLHwung

JLHwung commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

CI errors seems related, please investigate.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Good catch @JLHwung! That was also the cause of the 4 proposal-discard-binding CI failures. I moved body.directives onto the new method block, cleared them on the reused container block, and added both input and exec tests for "use strict" in an object method.

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new changes now look much better, compared with the first version of AI slop. The next time you open a PR, consider to prompt your LLM to open a new agent to review the branch, and commit until the review is clean.

The intention of LLM is not to replace you and let you open a PR in a very short time, quickly fix an issue and accumulate fame in open source. It is to spend as much time you would have hand-coded this PR, as interacting with the LLM: The goal is to create a PR that is much better than both you or LLM would have created in the first take. And such high quality PR ultimately benefits all of our users.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Thanks for the approval and the feedback @JLHwung. Fair point, I'll make sure to run thorough independent review passes and keep PR descriptions in my own words before opening future PRs.

@JLHwung
JLHwung force-pushed the fix/async-generator-block-scoping-loop-return branch from 13f8f98 to db26e68 Compare November 22, 2026 04:11
@nicolo-ribaudo

nicolo-ribaudo commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

I'll make sure to run thorough independent review passes

Other than that, please make sure to review and understand the code yourself. Do not open a PR until when you understand what the changes do and why they are the correct changes.

If you are just prompting multiple agents to write the code and review it you are not adding much value, we can just do it ourselves.

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Nov 23, 2026
@nicolo-ribaudo
nicolo-ribaudo added this pull request to the merge queue Nov 23, 2026
Merged via the queue into babel:main with commit 4a3c436 Nov 23, 2026
47 checks passed
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.

transform-async-generator-functions: .return() runs the next loop iteration when the body is a _loop and finally has an await

4 participants