swc icon indicating copy to clipboard operation
swc copied to clipboard

Decorators for symbol property are compiled incorrectly

Open dko-slapdash opened this issue 2 years ago • 1 comments

Describe the bug

When using a @Decorator for a symbol property, the compiler generates a wrong output.

Input code

const sym = Symbol("sym");

class Cls {
  @Memoize()
  [sym]() {}
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true
    },
    "target": "es2020"
  },
  "module": {
    "type": "commonjs"
  }
}

Playground link

https://play.swc.rs/?version=1.2.212&code=H4sIAAAAAAAAA0vOzysuUSiuzFWwVQiuzE3Kz9FQAvKUNK25uJJzEouLFZxzihWquRQUHHxTc%2FMzq1I1NIGcaKCaWA1NheparloAlz70DEMAAAA%3D&config=H4sIAAAAAAAAAxWMQQrAIAwE%2F7JnD8WjvxENpaU2kkSoiH%2Bv3padnR24NSEM1ChKspP21%2BKHAOuVNMlVDQ6ZEks0FkUwaTQdLMpJtoak%2FvAHVlU4t4f2y5YXSlwKv7dizh8GxEdwawAAAA%3D%3D

Expected behavior

It should define _key before defining the class.

Actual behavior

_key is undefined, so the property does not define. Compiled result:

...
const sym = Symbol("sym");
var _key;
class Cls {
    [_key]() {}
}
_key = sym; // <--- This must be BEFORE the class definition
__decorate([
    Memoize()
], Cls.prototype, _key, null);

Version

1.2.212

Additional context

No response

dko-slapdash avatar Jul 12 '22 07:07 dko-slapdash

I think we saw this as well.

CMCDragonkai avatar Jul 22 '22 05:07 CMCDragonkai

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

swc-bot avatar Oct 30 '22 12:10 swc-bot