fix(es/decorators): Use correct class name reference (#9375)

- Closes #9350
This commit is contained in:
magic-akari 2024-08-04 13:21:27 +08:00 committed by GitHub
parent a3a30403c1
commit badd6a9ede
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 99 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
swc_ecma_transforms_proposal: patch
---
fix(es/decorator): Use correct class name reference

View File

@ -0,0 +1,13 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2016"
},
"module": {
"type": "es6"
},
"isModule": true
}

View File

@ -0,0 +1,18 @@
function registerHook(key) {
return (...args) => console.log(args);
}
export default function design(base: typeof Component) {
return class Design extends base {
copy() {
console.log("copy");
}
@registerHook("beforeRender")
@registerHook("afterRender")
render() {
super.render();
console.log("design render");
}
};
}

View File

@ -0,0 +1,19 @@
function registerHook(key) {
return (...args) => console.log(args);
}
export default function design(base: typeof Component) {
const Design = "test name conflict";
return class Design extends base {
copy() {
console.log("copy");
}
@registerHook("beforeRender")
@registerHook("afterRender")
render() {
super.render();
console.log("design render");
}
};
}

View File

@ -0,0 +1,19 @@
import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate";
function registerHook(key) {
return (...args)=>console.log(args);
}
export default function design(base) {
var Design;
return Design = class Design extends base {
copy() {
console.log("copy");
}
render() {
super.render();
console.log("design render");
}
}, _ts_decorate([
registerHook("beforeRender"),
registerHook("afterRender")
], Design.prototype, "render", null), Design;
}

View File

@ -0,0 +1,20 @@
import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate";
function registerHook(key) {
return (...args)=>console.log(args);
}
export default function design(base) {
const Design = "test name conflict";
var Design1;
return Design1 = class Design1 extends base {
copy() {
console.log("copy");
}
render() {
super.render();
console.log("design render");
}
}, _ts_decorate([
registerHook("beforeRender"),
registerHook("afterRender")
], Design1.prototype, "render", null), Design1;
}

View File

@ -332,14 +332,16 @@ impl VisitMut for TscDecorator {
return;
}
let var_name = private_ident!("_class");
let ident = n.ident.get_or_insert_with(|| var_name.clone());
let ident = n
.ident
.get_or_insert_with(|| private_ident!("_class"))
.clone();
let old = mem::replace(&mut self.class_name, Some(ident.clone()));
n.visit_mut_children_with(self);
self.assign_class_expr_to = Some(var_name);
self.assign_class_expr_to = Some(ident);
self.class_name = old;
}