diff --git a/crates/perry-hir/src/lower/stmt.rs b/crates/perry-hir/src/lower/stmt.rs index b4eb710db07..8f80be4ce57 100644 --- a/crates/perry-hir/src/lower/stmt.rs +++ b/crates/perry-hir/src/lower/stmt.rs @@ -718,6 +718,23 @@ pub(crate) fn lower_stmt( false, )?; if let Some(inner_name) = inner_name_for_register { + // #6679: a NAMED class EXPRESSION's `.name` + // is its own explicit name (`Named` in + // `const B = class Named {}`), NOT the outer + // binding name. Per spec a named class + // expression is not an anonymous function + // definition, so assignment NamedEvaluation + // (`SetFunctionName` from `const B =`) must + // not override the declared name. This fast + // path registers the class under `bind_name` + // so `new B()` / `instanceof B` resolve + // statically, so record a display-name + // override (the #5592 mechanism, exactly like + // the mixin arm below) that codegen emits for + // the `.name` string constant instead of the + // `bind_name` registration key. + ctx.class_display_names + .insert(lowered_class.id, inner_name.clone()); lowered_class.aliases.push(inner_name); } // Computed member keys (`static get [expr]()`, diff --git a/crates/perry-hir/src/lower/tests.rs b/crates/perry-hir/src/lower/tests.rs index ee2cb187e5a..372f60fe80b 100644 --- a/crates/perry-hir/src/lower/tests.rs +++ b/crates/perry-hir/src/lower/tests.rs @@ -507,3 +507,44 @@ fn test_plain_class_to_class_heritage_keeps_static_extends_name() { "static class-to-class heritage keeps its extends_name" ); } + +/// #6679: a NAMED class EXPRESSION's `.name` is its own explicit name +/// (`Named` in `const B = class Named {}`), not the outer binding name. Per +/// spec a named class expression is not an anonymous function definition, so +/// the assignment's NamedEvaluation (`SetFunctionName` from `const B =`) must +/// not clobber the declared name. The module-top-level `const X = class {…}` +/// fast path registers the class under the binding name so `new B()` / +/// `instanceof B` resolve statically, and records a `class_display_names` +/// override to the explicit name for codegen to emit as `.name`. An ANONYMOUS +/// `const A = class {}` takes the inferred binding name and needs no override. +#[test] +fn test_named_class_expression_var_decl_reports_explicit_name() { + let source = r#" + const B = class Named {}; + const A = class {}; + "#; + let module = perry_parser::parse_typescript(source, "t.ts").expect("source parses"); + let hir = super::lower_module(&module, "t", "t.ts").expect("source lowers"); + + let named = hir + .classes + .iter() + .find(|c| c.name == "B") + .expect("class registered under binding name `B`"); + assert_eq!( + hir.class_display_names.get(&named.id).map(String::as_str), + Some("Named"), + "named class expression must report its explicit name as `.name`" + ); + + let anon = hir + .classes + .iter() + .find(|c| c.name == "A") + .expect("anonymous class registered under inferred name `A`"); + assert_eq!( + hir.class_display_names.get(&anon.id), + None, + "anonymous class expression uses the inferred binding name, no override" + ); +} diff --git a/test-files/test_gap_6679_named_class_expr_name.ts b/test-files/test_gap_6679_named_class_expr_name.ts new file mode 100644 index 00000000000..c83988dcaad --- /dev/null +++ b/test-files/test_gap_6679_named_class_expr_name.ts @@ -0,0 +1,66 @@ +// Issue #6679: a NAMED class EXPRESSION's `.name` must be its own explicit +// class name, not the outer binding name. `const B = class Named {}` binds +// `B`, but the class was declared `Named`, so `B.name === "Named"`. Per spec a +// named class expression is NOT an anonymous function definition, so the +// assignment's NamedEvaluation (`SetFunctionName` from the `const B =` target) +// must not clobber the declared name. perry's module-top-level +// `const X = class {…}` fast path registered the class under the binding name +// and used that for `.name`, so `B.name` wrongly returned "B". An ANONYMOUS +// class expression (`const A = class {}`) still infers "A" — that is correct. +// +// Expected output: +// Named +// A +// Foo +// D +// Named +// Baz +// Meth +// Inner +// Exp +// object + +// Module-top-level fast path (the bug site): explicit name wins. +const B = class Named {}; +console.log(B.name); // Named + +// Control: anonymous class expression correctly infers the binding name. +const A = class {}; +console.log(A.name); // A + +// Explicit name wins even with static members present. +const C = class Foo { + static bar() { + return 1; + } +}; +console.log(C.name); // Foo + +// Inner name equal to the binding name: unchanged. +const D = class D {}; +console.log(D.name); // D + +// An instance's constructor.name follows the class's `.name`. +console.log(new B().constructor.name); // Named + +// Assignment name-inference path also honors the explicit name. +let E; +E = class Baz {}; +console.log(E.name); // Baz + +// Object-property position (general class-expression value arm). +const obj = { m: class Meth {} }; +console.log(obj.m.name); // Meth + +// Class expression returned from a function body. +function f() { + return class Inner {}; +} +console.log(f().name); // Inner + +// Exported named class expression. +export const G = class Exp {}; +console.log(G.name); // Exp + +// The named class expression is still constructible via the binding. +console.log(typeof new C()); // object