-
Notifications
You must be signed in to change notification settings - Fork 5k
transpiler: preserve TDZ for top-level class declarations referenced earlier in the file #34933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
farm/a34e0399/class-decl-tdz-preserve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45574d3
transpiler: preserve TDZ for top-level class declarations referenced …
robobun 781067b
[autofix.ci] apply automated fixes
autofix-ci[bot] 791401a
compress rationale comments to three lines
robobun a509074
move tests to a dedicated file; pin function-body-mention behaviour
robobun f349104
tests: run concurrently; assert all order tokens are present
robobun fc9f02f
tests: assert the full TDZ error message, not only the error type
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
test/bundler/transpiler/runtime-transpiler-class-hoist.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // The runtime transpiler hoists top-level class declarations to help cyclic | ||
| // imports. That hoist must not jump over an earlier reference to the class | ||
| // binding, or the temporal dead zone disappears. | ||
| describe.concurrent("top-level class declaration TDZ", () => { | ||
| const tdzFixture = (decl: string) => /* js */ ` | ||
| const out = []; | ||
| try { | ||
| out.push("typeof=" + typeof K); | ||
| out.push("constructed=" + new K().constructor.name); | ||
| } catch (e) { | ||
| out.push("ERR=" + e.constructor.name); | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
| ${decl} | ||
| out.push("after=" + typeof K); | ||
| console.log(out.join(" | ")); | ||
| `; | ||
|
|
||
| async function run(cmd: string[], files: Record<string, string>) { | ||
| using dir = tempDir("runtime-transpiler-class-tdz", files); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), ...cmd], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout: stdout.trim(), stderr, exitCode }; | ||
| } | ||
|
|
||
| test.each([ | ||
| ["class K { m() {} }", "class declaration"], | ||
| ["export class K { m() {} }", "exported class declaration"], | ||
| ["export default class K { m() {} }", "export default named class"], | ||
| ])("preserved for a %s referenced before its declaration (%s)", async decl => { | ||
|
robobun marked this conversation as resolved.
|
||
| expect(await run(["index.mjs"], { "index.mjs": tdzFixture(decl) })).toMatchObject({ | ||
| stdout: "ERR=ReferenceError | after=function", | ||
| exitCode: 0, | ||
| }); | ||
| }); | ||
|
|
||
| test("preserved for a CommonJS top-level class declaration", async () => { | ||
| expect(await run(["index.cjs"], { "index.cjs": tdzFixture("class K { m() {} }") })).toMatchObject({ | ||
| stdout: "ERR=ReferenceError | after=function", | ||
| exitCode: 0, | ||
| }); | ||
| }); | ||
|
|
||
| // Classes with no prior mention of their name are still hoisted. References | ||
| // inside a preceding function body count as a mention, so those stay put. | ||
| test("still hoisted when no already-visited statement mentions the name", async () => { | ||
| const { stdout, exitCode } = await run(["build", "--no-bundle", "--target=bun", "index.mjs"], { | ||
| "index.mjs": /* js */ ` | ||
| const unrelated = 1; | ||
| export class A { m() { return "A"; } } | ||
| function make() { return new B(); } | ||
| export class B {} | ||
| console.log(new A().m(), unrelated, make()); | ||
| `, | ||
| }); | ||
| const tokens = ["class A", "unrelated = 1", "function make", "class B"]; | ||
| const indexed = tokens.map(s => [s, stdout.indexOf(s)] as const); | ||
| const order = [...indexed].sort((a, b) => a[1] - b[1]).map(([s]) => s); | ||
| expect({ missing: indexed.filter(([, i]) => i < 0).map(([s]) => s), order, exitCode }).toEqual({ | ||
| missing: [], | ||
| order: tokens, | ||
| exitCode: 0, | ||
| }); | ||
|
robobun marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.