GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — fix(block-scoped-function): hoist bindings used by hoisted functions … by yeonhj0507 · Pull Request #18659 · babel/babel Conversation
The I fixed this issue by modifying the 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…in switch-case Assisted-by : claude opus 5.5
|
There was an error while loading. Please reload this page.
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63745 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
commit: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
| ]), | ||
| ); | ||
|
|
||
| newPath.scope.crawl(); |
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
…recrawl Assited-by: Claude Opus 5.5
There was a problem 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.
There was an error while loading. Please reload this page.
| .get("cases") | ||
| .flatMap(c => c.get("consequent")) | ||
| .filter(p => p.isFunctionDeclaration()), | ||
| ); |
There was a problem 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.
There was an error while loading. Please reload this page.
| const hoistedIds: t.Identifier[] = []; | ||
| const inHoistedFn = (p: NodePath) => !!p.findParent(q => fnPaths.has(q)); | ||
| const usedNames = new Set<string>(); | ||
| const discriminent = path.get("discriminant"); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| const discriminent = path.get("discriminant"); | |
| const discriminant = path.get("discriminant"); |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| for (const name of [...usedNames]) { | ||
| if (referencesName(discriminant, name)) { |
There was a problem 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.
There was an error while loading. Please reload this page.
| ...(hoistedIds.length | ||
| ? [ | ||
| t.variableDeclaration( | ||
| "let", |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Function declaration in switch case loses access to sibling const after transform
There was an error while loading. Please reload this page.