From 80be50d4b6005c91e3041815d87b8070ba130030 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 06:26:46 +0000 Subject: [PATCH 1/6] js_parser: name a lowered anonymous decorated class after the parameter it is a default for Standard decorator lowering rewrites a class expression to `_class = class {}`, so an anonymous class loses the name its position would have given it and the lowering restores it from decorator_class_name. visit_args never set that for a plain parameter default, so `function f(K = class { @dec m() {} })` produced a class named "_class" (destructuring defaults, declarations and assignments already set it). Set it around the default value visit like visit_binding does. --- src/js_parser/visit/mod.rs | 10 ++ test/bundler/transpiler/es-decorators.test.ts | 113 ++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/src/js_parser/visit/mod.rs b/src/js_parser/visit/mod.rs index dd62b2fc2ed2..02b375a9210e 100644 --- a/src/js_parser/visit/mod.rs +++ b/src/js_parser/visit/mod.rs @@ -227,7 +227,17 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let dup: Option<&mut StringVoidMap> = duplicate_args_check.as_deref_mut(); self.visit_binding(arg.binding, dup); if let Some(default) = arg.default.as_mut() { + // Propagate name from the parameter to an anonymous decorated class default value + let prev_decorator_class_name = self.decorator_class_name; + if default.is_anonymous_named() + && let ExprData::EClass(e_class) = &default.data + && e_class.should_lower_standard_decorators + && let BData::BIdentifier(id) = arg.binding.data + { + self.decorator_class_name = Some(self.load_name_from_ref(id.r#ref)); + } self.visit_expr(default); + self.decorator_class_name = prev_decorator_class_name; } } } diff --git a/test/bundler/transpiler/es-decorators.test.ts b/test/bundler/transpiler/es-decorators.test.ts index bdc61e68c48d..2a1f0f817e08 100644 --- a/test/bundler/transpiler/es-decorators.test.ts +++ b/test/bundler/transpiler/es-decorators.test.ts @@ -982,6 +982,119 @@ describe("ES Decorators", () => { }); }); + describe("anonymous decorated class as a parameter default value", () => { + // The lowering rewrites the class to `_class = class {}`, so the name the + // parameter would have given it has to be carried over explicitly, the same + // way it is for `const K = class {}` and destructuring defaults. Expected + // values match node running the same code with the decorators removed. + test.concurrent("takes the parameter name in every kind of parameter list", async () => { + const { stdout, stderr, exitCode } = await runDecorator(` + function dec() {} + function declaration(K = class { @dec m() {} }) { return K.name; } + const expression = function (K = class { @dec m() {} }) { return K.name; }; + const arrow = (K = class { @dec m() {} }) => K.name; + function secondParameter(first, K = class { @dec m() {} }) { return K.name; } + class C { + constructor(K = class { @dec m() {} }) { this.fromConstructor = K.name; } + method(K = class { @dec m() {} }) { return K.name; } + static staticMethod(K = class { @dec m() {} }) { return K.name; } + } + const obj = { + method(K = class { @dec m() {} }) { return K.name; }, + set setter(K = class { @dec m() {} }) { this.fromSetter = K.name; }, + }; + obj.setter = undefined; + function destructured({ K = class { @dec m() {} } } = {}) { return K.name; } + console.log(JSON.stringify({ + declaration: declaration(), + expression: expression(), + arrow: arrow(), + secondParameter: secondParameter(1), + constructorParameter: new C().fromConstructor, + method: new C().method(), + staticMethod: C.staticMethod(), + objectMethod: obj.method(), + setter: obj.fromSetter, + destructured: destructured(), + })); + `); + expect(stderr).toBe(""); + expect(JSON.parse(stdout)).toEqual({ + declaration: "K", + expression: "K", + arrow: "K", + secondParameter: "K", + constructorParameter: "K", + method: "K", + staticMethod: "K", + objectMethod: "K", + setter: "K", + destructured: "K", + }); + expect(exitCode).toBe(0); + }); + + test.concurrent("class decorator sees the parameter name as context.name", async () => { + const { stdout, stderr, exitCode } = await runDecorator(` + function dec(cls, ctx) { console.log("ctx.name:", JSON.stringify(ctx.name)); } + function f(K = @dec class {}) { return K.name; } + console.log(JSON.stringify(f())); + `); + expect(stderr).toBe(""); + expect(stdout).toBe('ctx.name: "K"\n"K"\n'); + expect(exitCode).toBe(0); + }); + + test.concurrent("class with an accessor and no decorators is lowered too", async () => { + const { stdout, stderr, exitCode } = await runDecorator(` + function f(K = class { accessor x = 1; }) { return [K.name, new K().x]; } + console.log(JSON.stringify(f())); + `); + expect(stderr).toBe(""); + expect(stdout).toBe('["K",1]\n'); + expect(exitCode).toBe(0); + }); + + test.concurrent("parameter name does not override an explicit class name or reach a nested class", async () => { + const { stdout, stderr, exitCode } = await runDecorator(` + function dec() {} + function named(K = class Named { @dec m() {} }) { return K.name; } + function nested(K = [class { @dec m() {} }][0]) { return K.name; } + function passed(K = class { @dec m() {} }) { return K.name; } + console.log(JSON.stringify([named(), nested(), passed(class Passed {})])); + `); + expect(stderr).toBe(""); + const [named, nested, passed] = JSON.parse(stdout); + expect(named).toBe("Named"); + expect(nested).not.toBe("K"); + expect(passed).toBe("Passed"); + expect(exitCode).toBe(0); + }); + + test.concurrent("TypeScript constructor parameter property", async () => { + using dir = tempDir("es-dec-param-property", { + "tsconfig.json": JSON.stringify({ compilerOptions: {} }), + "test.ts": ` + function dec(value: unknown, ctx: ClassMethodDecoratorContext) {} + class Holder { + constructor(public K = class { @dec m() {} }) {} + } + console.log(new Holder().K.name); + `, + }); + await using proc = Bun.spawn({ + cmd: [bunExe(), "test.ts"], + env: bunEnv, + cwd: String(dir), + stderr: "pipe", + }); + const [stdout, rawStderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(filterStderr(rawStderr)).toBe(""); + expect(stdout).toBe("K\n"); + expect(exitCode).toBe(0); + }); + }); + describe("private member calls in lowered classes", () => { // When a class is lowered for standard decorators, `recv.#m(...)` becomes // `__privateGet(recv, _m).call(recv, ...)`. The receiver must be evaluated From 288315c68813c2478ef920fd398cf0306b3bff72 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 06:57:59 +0000 Subject: [PATCH 2/6] js_parser: share the initializer naming between parameters, for loop declarations and destructuring defaults visit_for_loop_init's SLocal arm visited declaration values the same way visit_args visited parameter defaults, so `for (const K = class { @dec m() {} };;)` named the class "_class" as well. Move the decorator_class_name propagation into visit_binding_initializer and use it from all four sites. --- src/js_parser/visit/mod.rs | 69 ++++++++----------- test/bundler/transpiler/es-decorators.test.ts | 32 +++++++-- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/js_parser/visit/mod.rs b/src/js_parser/visit/mod.rs index 02b375a9210e..c863a4dcbff2 100644 --- a/src/js_parser/visit/mod.rs +++ b/src/js_parser/visit/mod.rs @@ -227,21 +227,32 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let dup: Option<&mut StringVoidMap> = duplicate_args_check.as_deref_mut(); self.visit_binding(arg.binding, dup); if let Some(default) = arg.default.as_mut() { - // Propagate name from the parameter to an anonymous decorated class default value - let prev_decorator_class_name = self.decorator_class_name; - if default.is_anonymous_named() - && let ExprData::EClass(e_class) = &default.data - && e_class.should_lower_standard_decorators - && let BData::BIdentifier(id) = arg.binding.data - { - self.decorator_class_name = Some(self.load_name_from_ref(id.r#ref)); - } - self.visit_expr(default); - self.decorator_class_name = prev_decorator_class_name; + self.visit_binding_initializer(arg.binding, default); } } } + /// Visits the initializer of a parameter, `for` loop declaration or destructuring + /// element. An anonymous class takes its name from the binding, but standard + /// decorator lowering rewrites it to `_class = class {}`, so the name is handed to + /// the lowering through `decorator_class_name` instead (see `e_class`). + pub(crate) fn visit_binding_initializer( + &mut self, + binding: BindingNodeIndex, + initializer: &mut Expr, + ) { + let prev_decorator_class_name = self.decorator_class_name; + if initializer.is_anonymous_named() + && let ExprData::EClass(e_class) = &initializer.data + && e_class.should_lower_standard_decorators + && let BData::BIdentifier(id) = binding.data + { + self.decorator_class_name = Some(self.load_name_from_ref(id.r#ref)); + } + self.visit_expr(initializer); + self.decorator_class_name = prev_decorator_class_name; + } + // `Vec` is not `Copy`; mutate in place. pub(crate) fn visit_ts_decorators(&mut self, decs: &mut ExprNodeList) { for dec in decs.slice_mut() { @@ -586,7 +597,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O for dec in st.decls.slice_mut() { self.visit_binding(dec.binding, None); if let Some(val) = dec.value.as_mut() { - self.visit_expr(val); + self.visit_binding_initializer(dec.binding, val); } } st.kind = self.select_local_kind(st.kind); @@ -642,22 +653,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O // Arena-owned B::Array valid for 'a; exclusive during visit pass. for item in bind.items_mut() { self.visit_binding(item.binding, duplicate_arg_check.as_deref_mut()); - if let Some(default_value) = item.default_value { + if let Some(default_value) = item.default_value.as_mut() { let was_anonymous_named_expr = default_value.is_anonymous_named(); - let prev_decorator_class_name2 = self.decorator_class_name; - if was_anonymous_named_expr { - if let ExprData::EClass(e_class) = &default_value.data { - if e_class.should_lower_standard_decorators { - if let BData::BIdentifier(id) = item.binding.data { - let id = id.get(); - self.decorator_class_name = - Some(self.load_name_from_ref(id.r#ref)); - } - } - } - } - self.visit_expr(item.default_value.as_mut().unwrap()); - self.decorator_class_name = prev_decorator_class_name2; + self.visit_binding_initializer(item.binding, default_value); if let BData::BIdentifier(bind_) = item.binding.data { let bind_ = bind_.get(); @@ -681,22 +679,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O } self.visit_binding(property.value, duplicate_arg_check.as_deref_mut()); - if let Some(default_value) = property.default_value { + if let Some(default_value) = property.default_value.as_mut() { let was_anonymous_named_expr = default_value.is_anonymous_named(); - let prev_decorator_class_name3 = self.decorator_class_name; - if was_anonymous_named_expr { - if let ExprData::EClass(e_class) = &default_value.data { - if e_class.should_lower_standard_decorators { - if let BData::BIdentifier(id) = property.value.data { - let id = id.get(); - self.decorator_class_name = - Some(self.load_name_from_ref(id.r#ref)); - } - } - } - } - self.visit_expr(property.default_value.as_mut().unwrap()); - self.decorator_class_name = prev_decorator_class_name3; + self.visit_binding_initializer(property.value, default_value); if let BData::BIdentifier(bind_) = property.value.data { let bind_ = bind_.get(); diff --git a/test/bundler/transpiler/es-decorators.test.ts b/test/bundler/transpiler/es-decorators.test.ts index 2a1f0f817e08..44596d8143bc 100644 --- a/test/bundler/transpiler/es-decorators.test.ts +++ b/test/bundler/transpiler/es-decorators.test.ts @@ -982,11 +982,11 @@ describe("ES Decorators", () => { }); }); - describe("anonymous decorated class as a parameter default value", () => { + describe("anonymous decorated class as a binding initializer", () => { // The lowering rewrites the class to `_class = class {}`, so the name the - // parameter would have given it has to be carried over explicitly, the same - // way it is for `const K = class {}` and destructuring defaults. Expected - // values match node running the same code with the decorators removed. + // binding would have given it has to be carried over explicitly, the same + // way it already is for `const K = class {}`. Expected values match node + // running the same code with the decorators removed. test.concurrent("takes the parameter name in every kind of parameter list", async () => { const { stdout, stderr, exitCode } = await runDecorator(` function dec() {} @@ -1004,7 +1004,8 @@ describe("ES Decorators", () => { set setter(K = class { @dec m() {} }) { this.fromSetter = K.name; }, }; obj.setter = undefined; - function destructured({ K = class { @dec m() {} } } = {}) { return K.name; } + function objectPattern({ K = class { @dec m() {} } } = {}) { return K.name; } + function arrayPattern([K = class { @dec m() {} }] = []) { return K.name; } console.log(JSON.stringify({ declaration: declaration(), expression: expression(), @@ -1015,7 +1016,8 @@ describe("ES Decorators", () => { staticMethod: C.staticMethod(), objectMethod: obj.method(), setter: obj.fromSetter, - destructured: destructured(), + objectPattern: objectPattern(), + arrayPattern: arrayPattern(), })); `); expect(stderr).toBe(""); @@ -1029,11 +1031,27 @@ describe("ES Decorators", () => { staticMethod: "K", objectMethod: "K", setter: "K", - destructured: "K", + objectPattern: "K", + arrayPattern: "K", }); expect(exitCode).toBe(0); }); + test.concurrent("takes the variable name in a for loop declaration", async () => { + const { stdout, stderr, exitCode } = await runDecorator(` + function dec() {} + const names = []; + for (const K = class { @dec m() {} }; names.length < 1; ) names.push(K.name); + for (let K = class { @dec m() {} }, L = class { accessor x; }; names.length < 3; ) names.push(K.name, L.name); + for (var V = class { @dec m() {} }; names.length < 4; ) names.push(V.name); + for (const [D = class { @dec m() {} }] = []; names.length < 5; ) names.push(D.name); + console.log(JSON.stringify(names)); + `); + expect(stderr).toBe(""); + expect(JSON.parse(stdout)).toEqual(["K", "K", "L", "V", "D"]); + expect(exitCode).toBe(0); + }); + test.concurrent("class decorator sees the parameter name as context.name", async () => { const { stdout, stderr, exitCode } = await runDecorator(` function dec(cls, ctx) { console.log("ctx.name:", JSON.stringify(ctx.name)); } From e0b3cdd7e53bf707747fbac68d433063c8446f69 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 07:03:04 +0000 Subject: [PATCH 3/6] js_parser: shorten the visit_binding_initializer doc comment --- src/js_parser/visit/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/js_parser/visit/mod.rs b/src/js_parser/visit/mod.rs index c863a4dcbff2..7171a014ddcb 100644 --- a/src/js_parser/visit/mod.rs +++ b/src/js_parser/visit/mod.rs @@ -232,10 +232,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O } } - /// Visits the initializer of a parameter, `for` loop declaration or destructuring - /// element. An anonymous class takes its name from the binding, but standard - /// decorator lowering rewrites it to `_class = class {}`, so the name is handed to - /// the lowering through `decorator_class_name` instead (see `e_class`). + /// Decorator lowering turns the initializer into `_class = class {}`, so an anonymous + /// class gets the binding's name through `decorator_class_name` (see `e_class`). pub(crate) fn visit_binding_initializer( &mut self, binding: BindingNodeIndex, From 54bad43583b9cce64eecd166bf97ad188e43edf5 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 07:06:35 +0000 Subject: [PATCH 4/6] js_parser: one-line doc comment for visit_binding_initializer --- src/js_parser/visit/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/js_parser/visit/mod.rs b/src/js_parser/visit/mod.rs index 7171a014ddcb..d5ef0d0fa041 100644 --- a/src/js_parser/visit/mod.rs +++ b/src/js_parser/visit/mod.rs @@ -232,8 +232,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O } } - /// Decorator lowering turns the initializer into `_class = class {}`, so an anonymous - /// class gets the binding's name through `decorator_class_name` (see `e_class`). + /// Names an anonymous decorated class after the binding it initializes (`decorator_class_name`). pub(crate) fn visit_binding_initializer( &mut self, binding: BindingNodeIndex, From dbd1cce1ac7fa80c696f916d58ca85be5d4dd4c6 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 12:04:00 +0000 Subject: [PATCH 5/6] js_parser: compute the binding-derived class name in a helper and use it in visit_decls too decorator_class_name_from_binding returns the name instead of owning the visit, so visit_decls, which visits through visit_expr_in_out, can drop its inline copy as well, and the helper has the same shape as decorator_class_name_from_key. --- src/js_parser/visit/mod.rs | 65 +++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/js_parser/visit/mod.rs b/src/js_parser/visit/mod.rs index d5ef0d0fa041..2f0f108bac89 100644 --- a/src/js_parser/visit/mod.rs +++ b/src/js_parser/visit/mod.rs @@ -227,27 +227,31 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let dup: Option<&mut StringVoidMap> = duplicate_args_check.as_deref_mut(); self.visit_binding(arg.binding, dup); if let Some(default) = arg.default.as_mut() { - self.visit_binding_initializer(arg.binding, default); + let prev_decorator_class_name = self.decorator_class_name; + self.decorator_class_name = + self.decorator_class_name_from_binding(arg.binding, default); + self.visit_expr(default); + self.decorator_class_name = prev_decorator_class_name; } } } - /// Names an anonymous decorated class after the binding it initializes (`decorator_class_name`). - pub(crate) fn visit_binding_initializer( - &mut self, + /// The `.name` that `binding` gives an anonymous class initializer; lowering the class would lose it. + pub(crate) fn decorator_class_name_from_binding( + &self, binding: BindingNodeIndex, - initializer: &mut Expr, - ) { - let prev_decorator_class_name = self.decorator_class_name; - if initializer.is_anonymous_named() - && let ExprData::EClass(e_class) = &initializer.data - && e_class.should_lower_standard_decorators - && let BData::BIdentifier(id) = binding.data - { - self.decorator_class_name = Some(self.load_name_from_ref(id.r#ref)); + value: &Expr, + ) -> Option<&'a [u8]> { + let ExprData::EClass(class) = value.data else { + return None; + }; + if class.class_name.is_some() || !class.should_lower_standard_decorators { + return None; } - self.visit_expr(initializer); - self.decorator_class_name = prev_decorator_class_name; + let BData::BIdentifier(id) = binding.data else { + return None; + }; + Some(self.load_name_from_ref(id.r#ref)) } // `Vec` is not `Copy`; mutate in place. @@ -315,18 +319,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O !SCAN_ONLY, "only_scan_imports_and_do_not_visit must not run this." ); - // Propagate name from binding to anonymous decorated class expressions let prev_decorator_class_name = self.decorator_class_name; - if was_anonymous_named_expr { - if let ExprData::EClass(e_class) = &val.data { - if e_class.should_lower_standard_decorators { - if let BData::BIdentifier(id) = decl.binding.data { - let id = id.get(); - self.decorator_class_name = Some(self.load_name_from_ref(id.r#ref)); - } - } - } - } + self.decorator_class_name = + self.decorator_class_name_from_binding(decl.binding, &val); if self.react_compiler.is_some() && self.current_scope == self.module_scope && let BData::BIdentifier(id) = decl.binding.data @@ -594,7 +589,11 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O for dec in st.decls.slice_mut() { self.visit_binding(dec.binding, None); if let Some(val) = dec.value.as_mut() { - self.visit_binding_initializer(dec.binding, val); + let prev_decorator_class_name = self.decorator_class_name; + self.decorator_class_name = + self.decorator_class_name_from_binding(dec.binding, val); + self.visit_expr(val); + self.decorator_class_name = prev_decorator_class_name; } } st.kind = self.select_local_kind(st.kind); @@ -652,7 +651,11 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O self.visit_binding(item.binding, duplicate_arg_check.as_deref_mut()); if let Some(default_value) = item.default_value.as_mut() { let was_anonymous_named_expr = default_value.is_anonymous_named(); - self.visit_binding_initializer(item.binding, default_value); + let prev_decorator_class_name = self.decorator_class_name; + self.decorator_class_name = + self.decorator_class_name_from_binding(item.binding, default_value); + self.visit_expr(default_value); + self.decorator_class_name = prev_decorator_class_name; if let BData::BIdentifier(bind_) = item.binding.data { let bind_ = bind_.get(); @@ -678,7 +681,11 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O self.visit_binding(property.value, duplicate_arg_check.as_deref_mut()); if let Some(default_value) = property.default_value.as_mut() { let was_anonymous_named_expr = default_value.is_anonymous_named(); - self.visit_binding_initializer(property.value, default_value); + let prev_decorator_class_name = self.decorator_class_name; + self.decorator_class_name = + self.decorator_class_name_from_binding(property.value, default_value); + self.visit_expr(default_value); + self.decorator_class_name = prev_decorator_class_name; if let BData::BIdentifier(bind_) = property.value.data { let bind_ = bind_.get(); From 89adfb8ee7942b3f8bca740bd4639d54ac41c9d6 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:21:03 +0000 Subject: [PATCH 6/6] ci: retrigger