swc
swc copied to clipboard
Static decorators are executed at the wrong time
Describe the bug
Decorators get hoisted before the class declaration which causes problems when a static function in a class is used to decorate a method.
Input code
class A {
static test(a: any, b: any) {}
@A.test()
hi() {}
}
Config
{
"$schema": "https://json.schemastore.org/swcrc",
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2022",
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Playground link
No response
SWC Info output
No response
Expected behavior
It should work.
Actual behavior
No response
Version
1.3.90
Additional context
No response
I don't have time to work on this more, so I'll leave a summary.
The PR doesn't work. I suspect this needs to be done outside the class, after it has been declared, similar to how TypeScript and the proposal encourage it.
Something like
let a = class A {}
(() => {
...do static stuff here
})
return a;
I suspect that would require a lot of change with the private methods and what not.
Reopening as I can work on this