swc icon indicating copy to clipboard operation
swc copied to clipboard

Static decorators are executed at the wrong time

Open jrandolf-google opened this issue 2 years ago • 2 comments

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

jrandolf-google avatar Sep 28 '23 13:09 jrandolf-google

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.

jrandolf-google avatar Sep 28 '23 14:09 jrandolf-google

Reopening as I can work on this

kdy1 avatar Sep 29 '23 08:09 kdy1