Skip to content
Open
Show file tree
Hide file tree
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 Jul 29, 2026
986958a
test: reference #20398 and #22770, cover array-literal substitution path
robobun Jul 29, 2026
872aca5
review: shorten NamedEvaluation comment
robobun Jul 29, 2026
b2411a3
test: mark FunctionHoistingKeepNames3/4 todo (pre-existing --keep-nam…
robobun Jul 29, 2026
d46d625
ci: retrigger
robobun Jul 29, 2026
a331541
test: link #36320 from FunctionHoistingKeepNames todo comments
robobun Jul 29, 2026
71a08f7
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun Nov 1, 2026
25dd207
js_parser: keep the declaration that names a single-use function when…
robobun Nov 1, 2026
f487910
test(aot): read block functions once, and update four stored frame co…
robobun Nov 1, 2026
5d93520
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun Nov 1, 2026
2cca0a3
Sweep what was built around the single-use inliner
robobun Nov 1, 2026
0691f3d
test: leave the printed text of a fold and of a block function to the…
robobun Nov 1, 2026
543dded
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun Nov 8, 2026
05c34e9
test: show the moved function in bun pm diff from runtime-transpiler.…
robobun Nov 8, 2026
b066c2a
aot: name a function that a let or const names and the next return reads
robobun Nov 8, 2026
39810c3
aot: leave the value of a let or const of Bun's own modules in the re…
robobun Nov 8, 2026
4287928
aot: name a returned function in Bun's own modules too, and hold each…
robobun Nov 8, 2026
5bd0d7f
Merge remote-tracking branch 'origin/main' into farm/c47e5de4/fix-inl…
robobun Nov 11, 2026
60e8b13
test: the class of describe(MyClass) has its name under bun test too
robobun Nov 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/js_parser/visit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,17 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
let last: &mut Decl = &mut local.decls.slice_mut()[last_idx];
let Some(replacement) = last.value else { break };

// The declaration site is a NamedEvaluation context: an anonymous
// function/arrow/class initializer picks up the binding name as its
// ".name". Substituting it into the use site drops that and makes the
// name observably "". The block-level function-declaration lowering
// above produces exactly this shape ("let f = function() {}"), so a
// "{ function f() {} }" in source would otherwise lose its name at
// runtime when used once.
Comment thread
robobun marked this conversation as resolved.
Resolved
if replacement.is_anonymous_named() {
break;
}

// The binding must be an identifier that is only used once.
// Ignore destructuring bindings since that's not the simple case.
// Destructuring bindings could potentially execute side-effecting
Expand Down
112 changes: 112 additions & 0 deletions test/js/bun/transpiler/transpiler-inline-anonymous-fn-name.test.ts
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");
});
Loading