Skip to content

fix(traverse,transform-regenerator): resolve var binding scope on removal - #18617

Merged
JLHwung merged 2 commits into
babel:mainfrom
hoanle0126:fix/regenerator-catch-var-hoist-18582
Nov 26, 2026
Merged

JLHwung merged 2 commits into
babel:mainfrom
hoanle0126:fix/regenerator-catch-var-hoist-18582

Conversation

@hoanle0126

@hoanle0126 hoanle0126 commented Nov 14, 2026 •

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

When removing or hoisting a var declaration inside a lexical scope such as catch (x), removing the binding from this.scope deleted the catch parameter binding instead of the hoisted function/program binding.

Update path.remove() in @babel/traverse to resolve var bindings from getFunctionParent() ?? getProgramParent(), and use funPath.scope for var bindings in @babel/plugin-transform-regenerator's hoist.

@babel-bot

babel-bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

commit: 3cd7a20

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

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

With suggested minimal changes, it should pass all the tests introduced in this PR.

removeHoistedVarBindings(path, funPath.scope);
if (expr === null) {
path.remove();
removeWithoutScopeBindingRemoval(path);

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.

I think this issue should be fixed on path.remove(), specifically in

// Remove scope information relative to this node
const bindings = t.getBindingIdentifiers(this.node, false, false, true);
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
}

For a var declaration, we should find the scope from getFunctionParent() ?? getProgramParent(), rather than its nearest scope this.scope, which might be lexical like the catch clause.

for (const name of Object.keys(path.getBindingIdentifiers())) {
// Remove the binding, to avoid "duplicate declaration" errors when it will
// be injected again.
path.scope.removeBinding(name);

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.

Add here, we should use funcScope for var and path.scope for other lexical declarations, such as let and const.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Updated per your suggestions: fixed path.remove() in @babel/traverse to resolve var declarations against the function/program scope, simplified hoist.ts to use funPath.scope for var, and added removal tests for traverse.

@hoanle0126 hoanle0126 changed the title fix(transform-regenerator): keep catch parameter bindings when hoisting var fix(traverse,transform-regenerator): resolve var binding scope on removal Nov 20, 2026
@JLHwung
JLHwung added this pull request to the merge queue Nov 26, 2026
Merged via the queue into babel:main with commit 717672c Nov 26, 2026
46 checks passed
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18748.

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.

regenerator: var x inside a nested catch (x) makes references in the inner catch block resolve to the outer catch parameter

4 participants