GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(block-scoped-function): hoist bindings used by hoisted functions … by yeonhj0507 · Pull Request #18659 · babel/babel

Conversation

@yeonhj0507

@yeonhj0507 yeonhj0507 commented Nov 19, 2026

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

The SwitchStatement visitor hoists functions in the case scope, but it doesn't consider the let/const/class bindings in the case scope. And it happens without block-scoping.

I fixed this issue by modifying the SwitchStatement Visitor to find the bindings that are used in the hoisted function scope but declared in the case scope and hoist them together, when the function is hoisted.

And I added 3 tests to check this fix: they each check the class, const, and partial hoisting.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yeonhj0507
fix(block-scoped-function): hoist bindings used by hoisted functions …
34813c2
…in switch-case

Assisted-by : claude opus 5.5
@babel-bot

babel-bot commented Nov 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Open in StackBlitz

commit: 42aa1c9

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JLHwung
JLHwung reviewed Nov 19, 2026
View reviewed changes

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thank you, could you add a new test case where there is a shadowed name in the discriminant?

const c = "outer";
switch (c) { case "outer": const c = "inner"; function h() { return c; } log(h()); }

V8 prints inner, main prints outer and in this PR nothing is printed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
JLHwung
JLHwung reviewed Nov 19, 2026
View reviewed changes
Comment thread packages/babel-plugin-transform-block-scoped-functions/src/index.ts Outdated
]),
);

newPath.scope.crawl();

@JLHwung JLHwung Nov 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

recrawling is expensive, do we have test fixtures where it will break without recrawling?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yeonhj0507 yeonhj0507 Nov 20, 2026

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thank you. I just fixed that issue and added a test for it. And I didn't consider that replaceWith already crawls the new block scope, so I removed the recrawl.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yeonhj0507
fix(block-scoped-functions): avoid shadowing discriminant and remove …
165715e
…recrawl

Assited-by: Claude Opus 5.5
JLHwung
JLHwung reviewed Nov 23, 2026
View reviewed changes

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This PR looks good to me in general, left a few nits.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yeonhj0507 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread packages/babel-plugin-transform-block-scoped-functions/src/index.ts
.get("cases")
.flatMap(c => c.get("consequent"))
.filter(p => p.isFunctionDeclaration()),
);

@JLHwung JLHwung Nov 23, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Can we exit early if fnPaths is empty?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yeonhj0507 reacted with eyes emoji
All reactions
  • 👀 1 reaction
Comment thread packages/babel-plugin-transform-block-scoped-functions/src/index.ts Outdated
const hoistedIds: t.Identifier[] = [];
const inHoistedFn = (p: NodePath) => !!p.findParent(q => fnPaths.has(q));
const usedNames = new Set<string>();
const discriminent = path.get("discriminant");

@JLHwung JLHwung Nov 23, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
const discriminent = path.get("discriminant");
const discriminant = path.get("discriminant");

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yeonhj0507 reacted with laugh emoji
All reactions
  • 😄 1 reaction
@yeonhj0507
fix: modify few nits
42aa1c9
JLHwung
JLHwung approved these changes Nov 24, 2026
View reviewed changes
joellemd
joellemd reviewed Nov 27, 2026
View reviewed changes
Comment thread packages/babel-plugin-transform-block-scoped-functions/src/index.ts
}

for (const name of [...usedNames]) {
if (referencesName(discriminant, name)) {

@joellemd joellemd Nov 27, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This handles reads in the discriminant but not writes. referencesName only visits ReferencedIdentifier, and an assignment target isn't one, so something like this gets missed

let c = 5;
switch (c = 1) {
  case 1:
    const c = "inner";
    function h() { return c; }
}
// c should be 1 here, but it's still 5

The hoisted let c ends up shadowing the outer c, so the discriminant assigns to the inner one instead. Same thing happens with destructuring switch ([c] = arr). Maybe check the binding identifiers in the discriminant too, or look at the outer binding's constantViolations?

Unrelated to this PR, but while you're here: hoisted function names have the same problem.

const h = 1;
switch (h) { case 1: r = "matched"; function h() {} break; default: r = "default"; }

That ends up in default, since let h = function h() {} goes above the switch. It was already broken on main, so feel free to leave it for a follow up but running the same check over the names in fnPaths would probably fix it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/babel-plugin-transform-block-scoped-functions/src/index.ts
...(hoistedIds.length
? [
t.variableDeclaration(
"let",

@joellemd joellemd Nov 27, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Since every hoisted binding becomes a let here, consts used by a hoisted function can be reassigned now:

"use strict";
switch (1) {
  case 1:
    const c = 1;
    function h() { c = 2; return c; }
    h(); // should throw a TypeError, now returns 2
}

block-scoping can't add its readOnlyError either, because by the time it runs the binding isn't a const anymore. Should we skip hoisting a const that has constantViolations, or replace those writes with a throw? @JLHwung @nicolo-ribaudo

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JLHwung JLHwung Nov 27, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Good catch, I think we can skip hoisting so that we don't swallow this runtime error. The block scoping transform will be applied mostly on production environment with legacy targets. In development environment with minimal transpile, such error should have been caught by developers.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JLHwung JLHwung JLHwung approved these changes
+1 more reviewer
@joellemd joellemd joellemd left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Function declaration in switch case loses access to sibling const after transform

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@yeonhj0507 @babel-bot @JLHwung @joellemd
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.