Repository navigation
fix: Wrong order of initialization in TS class constructor #15583
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Foo { | ||
| } | ||
|
|
||
| class Foo2 extends Foo { | ||
| constructor(private readonly incoming: number) {} | ||
| value = this.incoming; | ||
| } | ||
|
|
||
| class Foo3 extends Foo { | ||
| constructor(private readonly incoming: number) { | ||
| super(); | ||
| } | ||
| value = this.incoming; | ||
| } | ||
|
|
||
| class Foo4 extends Foo { | ||
| constructor(private readonly incoming: number) { | ||
| if (1) { | ||
| super(); | ||
| } else { | ||
| super(); | ||
| } | ||
| } | ||
| value = this.incoming; | ||
| } | ||
|
|
||
|
|
||
| class Foo5 extends Foo { | ||
| constructor(private readonly incoming: number) { | ||
| var c = a ? super() : b; | ||
| } | ||
| value = this.incoming; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "transform-typescript", | ||
| { | ||
| "allowDeclareFields": true | ||
| } | ||
| ], | ||
| "proposal-class-properties" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class Foo {} | ||
| class Foo2 extends Foo { | ||
| constructor(incoming) {} | ||
| } | ||
| class Foo3 extends Foo { | ||
| constructor(incoming) { | ||
| super(); | ||
| this.incoming = incoming; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated, but in Babel 8 we should emit this based on the |
||
| babelHelpers.defineProperty(this, "value", this.incoming); | ||
| } | ||
| } | ||
| class Foo4 extends Foo { | ||
| constructor(incoming) { | ||
| if (1) { | ||
| super(); | ||
| this.incoming = incoming; | ||
| babelHelpers.defineProperty(this, "value", this.incoming); | ||
| } else { | ||
| super(); | ||
| this.incoming = incoming; | ||
| babelHelpers.defineProperty(this, "value", this.incoming); | ||
| } | ||
| } | ||
| } | ||
| class Foo5 extends Foo { | ||
| constructor(incoming) { | ||
| var c = a ? (super(), (this.incoming = incoming, babelHelpers.defineProperty(this, "value", this.incoming)), this) : b; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The root of the problem is when we use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don-t those two examples have the same behavior anyway? One just has unnecessary parentheses.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it works fine when enabling these two plugins. If someone uses three plugins that will inject initialization statements, I think it will stay wrong before this PR.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we are currently only searching for siblings of |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| class Foo { | ||
| constructor(private readonly incoming: number) {} | ||
| value = this.incoming; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "plugins": [ | ||
| [ | ||
| "transform-typescript", | ||
| { | ||
| "allowDeclareFields": true | ||
| } | ||
| ], | ||
| "proposal-class-properties" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class Foo { | ||
| constructor(incoming) { | ||
| this.incoming = incoming; | ||
| babelHelpers.defineProperty(this, "value", this.incoming); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a test with
super()in an expression position?