Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong codegen for computed class properties when using TypeScript #1122

Closed
Tracked by #2228
paul-soporan opened this issue Oct 1, 2020 · 1 comment · Fixed by #1132
Closed
Tracked by #2228

Wrong codegen for computed class properties when using TypeScript #1122

paul-soporan opened this issue Oct 1, 2020 · 1 comment · Fixed by #1132
Assignees
Labels
Milestone

Comments

@paul-soporan
Copy link

Describe the bug

The SWC emit for non-define (aka TypeScript's default) computed class properties is wrong.

Input code

const identifier = 'bar';

class Foo {
  [identifier] = 5;
}

console.log(new Foo().bar)

Config

{
  "jsc": {
    "target": "es2020",
    "parser": {
      "syntax": "typescript"
    }
  }
}

Expected behavior

The emitted JavaScript currently is:

const identifier = 'bar';
class Foo {
    constructor(){
        this.identifier = 5;
    }
}
console.log(new Foo().bar);

I'd expect it to emit:

const identifier = 'bar';
class Foo {
    constructor(){
        this[identifier] = 5;
    }
}
console.log(new Foo().bar);

Version

The version of @swc/core: 1.2.34

Additional context

This doesn't happen when setting the jsc.parser.syntax to ecmascript:

  • when targetting es2020 it emits the computed class property as is.
  • when targeting lower versions it uses Object.defineProperty which probably goes through a different codepath.
@kdy1 kdy1 added this to the v1.2.35 milestone Oct 2, 2020
@kdy1 kdy1 self-assigned this Oct 4, 2020
kdy1 added a commit to kdy1/swc that referenced this issue Oct 4, 2020
@kdy1 kdy1 mentioned this issue Oct 4, 2020
@kdy1 kdy1 closed this as completed in #1132 Oct 4, 2020
kdy1 added a commit that referenced this issue Oct 4, 2020
swc_ecma_transforms:
 - Handle typescript class properties correctly. (#1122)
 - Handle optional chaining properly. (#1130)
 - Inject variables for nullisn coalescing in correct scope. (#1123)
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 26, 2022

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-project swc-project locked as resolved and limited conversation to collaborators Oct 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants