Repository navigation
js_parser: keep the declaration that names a single-use function or class outside a bundle #36313
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
19
commits into
main
Choose a base branch
from
farm/c47e5de4/fix-inline-anonymous-fn-name
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 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6df0a85
js_parser: don't inline single-use anonymous fn/arrow/class initializers
robobun 986958a
test: reference #20398 and #22770, cover array-literal substitution path
robobun 872aca5
review: shorten NamedEvaluation comment
robobun b2411a3
test: mark FunctionHoistingKeepNames3/4 todo (pre-existing --keep-nam…
robobun d46d625
ci: retrigger
robobun a331541
test: link #36320 from FunctionHoistingKeepNames todo comments
robobun 71a08f7
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun 25dd207
js_parser: keep the declaration that names a single-use function when…
robobun f487910
test(aot): read block functions once, and update four stored frame co…
robobun 5d93520
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun 2cca0a3
Sweep what was built around the single-use inliner
robobun 0691f3d
test: leave the printed text of a fold and of a block function to the…
robobun 543dded
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun 05c34e9
test: show the moved function in bun pm diff from runtime-transpiler.…
robobun b066c2a
aot: name a function that a let or const names and the next return reads
robobun 39810c3
aot: leave the value of a let or const of Bun's own modules in the re…
robobun 4287928
aot: name a returned function in Bun's own modules too, and hold each…
robobun 5bd0d7f
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun 60e8b13
test: the class of describe(MyClass) has its name under bun test too
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
Some comments aren't visible on the classic Files Changed page.
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
112 changes: 112 additions & 0 deletions
112
test/js/bun/transpiler/transpiler-inline-anonymous-fn-name.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,112 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // The runtime transpiler runs with minify_syntax enabled (target=bun implies | ||
| // tree_shaking, which implies inlining, which implies minify_syntax). Its | ||
| // single-use-let inlining previously substituted an anonymous | ||
| // function/arrow/class initializer into the use site, dropping the ".name" it | ||
| // would have received via NamedEvaluation at the declaration. | ||
| // https://github.com/oven-sh/bun/issues/20398 | ||
| // https://github.com/oven-sh/bun/issues/22770 | ||
| // | ||
| // Block-level function declarations are lowered to exactly that shape | ||
| // ("let f = function() {}"), so "{ function f() {} ... }" lost its name at | ||
| // runtime when f was referenced exactly once. This was first observed as | ||
| // performance.timerify() emitting entries with an empty name. | ||
| // | ||
| // These tests spawn a fresh bun process so the fixture goes through the | ||
| // real runtime transpiler rather than this test file's own transpile. | ||
|
|
||
| test("single-use let of an anonymous function keeps its .name at runtime", async () => { | ||
| const src = ` | ||
| function t1() { | ||
| let fn = function () {}; | ||
| return fn.name; | ||
| } | ||
| function t2() { | ||
| const arrow = () => {}; | ||
| return arrow.name; | ||
| } | ||
| function t3() { | ||
| let Cls = class {}; | ||
| return Cls.name; | ||
| } | ||
| function t4() { | ||
| // named function expression: inlining is fine, name is intrinsic | ||
| let fn = function named() {}; | ||
| return fn.name; | ||
| } | ||
| function t5() { | ||
| // non-function initializer still inlines | ||
| let n = 42; | ||
| return n + 1; | ||
| } | ||
| function t6() { | ||
| // issue #20398: substituted into an array literal | ||
| const f = () => 0; | ||
| const a = [f]; | ||
| return a[0].name; | ||
| } | ||
| console.log(JSON.stringify([t1(), t2(), t3(), t4(), t5(), t6()])); | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", src], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect({ out: JSON.parse(stdout.trim()), stderr, exitCode }).toEqual({ | ||
| out: ["fn", "arrow", "Cls", "named", 43, "f"], | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }); | ||
|
|
||
| test("block-level function declaration keeps its .name at runtime", async () => { | ||
| using dir = tempDir("block-fn-name", { | ||
| // .mjs so the block-level function declaration is a strict-mode let, | ||
| // which is the shape the lowering rewrites to "let f = function() {}". | ||
| "entry.mjs": ` | ||
| let a, b; | ||
| { | ||
| function slow() { return 42; } | ||
| a = slow.name; | ||
| } | ||
| { | ||
| function work() {} | ||
| const wrapped = work; | ||
| b = wrapped.name; | ||
| } | ||
| console.log(JSON.stringify([a, b])); | ||
| `, | ||
| }); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "entry.mjs"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect({ out: JSON.parse(stdout.trim()), stderr, exitCode }).toEqual({ | ||
| out: ["slow", "work"], | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }); | ||
|
|
||
| test("Bun.Transpiler minify.syntax does not inline anonymous function into its single use", () => { | ||
| const transpiler = new Bun.Transpiler({ loader: "js", minify: { syntax: true } }); | ||
| const out = transpiler.transformSync(` | ||
| function outer() { | ||
| let fn = function () {}; | ||
| return fn.name; | ||
| } | ||
| `); | ||
| // The declaration must survive so JSC's NamedEvaluation assigns "fn". | ||
| expect(out).toContain("let fn = function"); | ||
| expect(out).not.toContain("return function"); | ||
| }); |
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.