Compare commits

...

2 Commits

Author SHA1 Message Date
autofix-ci[bot]
fe1c104ac9 [autofix.ci] apply automated fixes 2025-09-20 04:24:29 +00:00
Claude Bot
e7ac561e63 Fix arrow function names in test files
Arrow functions were losing their inferred names when defined inside test
blocks. This was because the keepExprSymbolName function was disabled,
which prevented the transpiler from preserving the variable name as the
function name.

The fix re-enables keepExprSymbolName to call the __name runtime helper,
which sets the function name property at runtime.

Fixes #22770

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-20 04:16:20 +00:00
2 changed files with 68 additions and 12 deletions

View File

@@ -3742,19 +3742,16 @@ pub fn NewParser_(
}
}
pub fn keepExprSymbolName(_: *P, _value: Expr, _: string) Expr {
return _value;
// var start = p.expr_list.items.len;
// p.expr_list.ensureUnusedCapacity(2) catch unreachable;
// p.expr_list.appendAssumeCapacity(_value);
// p.expr_list.appendAssumeCapacity(p.newExpr(E.String{
// .utf8 = name,
// }, _value.loc));
pub fn keepExprSymbolName(p: *P, _value: Expr, name: string) Expr {
// Create a call to the __name runtime helper
var args = p.allocator.alloc(Expr, 2) catch unreachable;
args[0] = _value;
args[1] = p.newExpr(E.String{ .data = name }, _value.loc);
// var value = p.callRuntime(_value.loc, "", p.expr_list.items[start..p.expr_list.items.len]);
// // Make sure tree shaking removes this if the function is never used
// value.getCall().can_be_unwrapped_if_unused = true;
// return value;
var value = p.callRuntime(_value.loc, "__name", args);
// Make sure tree shaking removes this if the function is never used
value.data.e_call.can_be_unwrapped_if_unused = .if_unused;
return value;
}
pub fn isSimpleParameterList(args: []G.Arg, has_rest_arg: bool) bool {

View File

@@ -0,0 +1,59 @@
import { expect, test } from "bun:test";
// Regression test for issue #22770
// https://github.com/oven-sh/bun/issues/22770
// Arrow functions defined in tests should preserve their name property
test("arrow function names are preserved inside tests", () => {
// Basic arrow function
const arrow1 = () => {};
expect(arrow1.name).toBe("arrow1");
// Arrow function with parameters
const arrow2 = (x: number) => x * 2;
expect(arrow2.name).toBe("arrow2");
// Arrow function with body
const arrow3 = () => {
return 42;
};
expect(arrow3.name).toBe("arrow3");
// Async arrow function
const arrow4 = async () => {};
expect(arrow4.name).toBe("arrow4");
// Arrow function in object literal
const obj = {
method: () => {},
};
expect(obj.method.name).toBe("method");
// Arrow function in nested scope
function nested() {
const arrow5 = () => {};
expect(arrow5.name).toBe("arrow5");
}
nested();
// Arrow function assigned later
let arrow6: () => void;
arrow6 = () => {};
expect(arrow6.name).toBe("arrow6");
});
test("arrow function names work with destructuring", () => {
const { foo = () => {} } = {};
expect(foo.name).toBe("foo");
const [bar = () => {}] = [];
expect(bar.name).toBe("bar");
});
test("anonymous arrow functions remain anonymous", () => {
const anon = (() => () => {})();
expect(anon.name).toBe("");
const arr = [() => {}];
expect(arr[0].name).toBe("");
});