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

addInitializer missing on class field decorators #8525

Closed
phiresky opened this issue Jan 19, 2024 · 2 comments · Fixed by #8619
Closed

addInitializer missing on class field decorators #8525

phiresky opened this issue Jan 19, 2024 · 2 comments · Fixed by #8619
Labels
Milestone

Comments

@phiresky
Copy link
Contributor

phiresky commented Jan 19, 2024

Describe the bug

function bar(_, ctx) {
  console.log("should not be undefined", ctx.addInitializer);
}
class Foo {
  @bar baz = 1
}


new Foo()

outputs undefined

Input code

function bar(_, ctx) {
  console.log("should not be undefined", ctx.addInitializer);
}
class Foo {
  @bar baz = 1
}


new Foo()

Config

{
  "jsc": {
    "parser": { "syntax": "typescript", "decorators": true },
    "transform": { "decoratorVersion": "2022-03" }
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.100&code=H4sIAAAAAAAAAyWNwQrCMBAF7%2FsVj55SkILnIngS%2FApJk60uhF1oEiwV%2F920nmeYmauGIqaY%2FOIeJ4Sy9vgQEEyzJR6SPV2XX1ZThFrBxKgaeRbl2B3%2B4GO8qxTxSTZe%2Fo2RvhSSzxk3syN4bYd22XDBuUEi5fcOXU8%2FkPoNpIUAAAA%3D&config=H4sIAAAAAAAAAz2MwQqEMAxE7%2F2KkLOCdG%2F7Id5LjeDCtmWShRXpvxsRvL1h5s0RiPijmd90OHpoCSq4MrHuxdLfmW1vohlbMx6IF8kVySrUO8NPqA%2B3bUhF14rvffAMZ4FutVxXcYpxnF5M3ZUeejgB%2BD%2B8W4QAAAA%3D

SWC Info output

No response

Expected behavior

addInitializer should be defined on class property decorators as well as .

The same code works on babel and tsc. I thought it was an issue there since the proposal readme says addInitializer does not exist, but apparently the readme is outdated against the actual spec.

Actual behavior

No response

Version

1.3.104

Additional context

https://arai-a.github.io/ecma262-compare/?pr=2417

image

microsoft/TypeScript#57096

causes mobx to not work with swc: mobxjs/mobx#3817

@nberlette
Copy link

This is what's causing the issue right here:

if (kind !== 0 /* FIELD */) {
ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
}

kdy1 pushed a commit that referenced this issue Feb 14, 2024
**Description:**

At some point the spec text of ES decorators was changed to also have
addInitializer for field decorators. This is already used e.g. in mobx.
This simple change removes the special case that was present for field
decorators and seems to fix the issue for me.

This change is backwards compatible since it only affects code that was
broken before.

**Related issue:**

 - Closes #8525
@kdy1 kdy1 modified the milestones: Planned, v1.4.2 Feb 19, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Mar 20, 2024

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 Mar 20, 2024
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.

4 participants