Repository navigation
fix: Wrong order of initialization in TS class constructor - #15583
liuxingbaoyu wants to merge 2 commits into
Conversation
42427d0 to
c0e7f12
Compare
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54357/ |
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54317/ |
|
It's not a trivial PR, can you summarize the change in the PR description? It well help reviewers as well as any new contributors when they are tracking a historical PR. |
|
Ok, I tried to explain it in the description. |
There was a problem hiding this comment.
These are unrelated, it's just that I don't think they deserve a separate PR. :)
| class Foo3 extends Foo { | ||
| constructor(incoming) { | ||
| super(); | ||
| this.incoming = incoming; |
There was a problem hiding this comment.
Unrelated, but in Babel 8 we should emit this based on the setPublicClassFields assumption.
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
I don't understand how this PR works. How can this fix the coordination issue between the TS plugin and the class features plugin, if only the class features plugin uses TAG_LAST_INJECTED and the TS plugin doesn't?
| class Foo3 extends Foo { | ||
| constructor(private readonly incoming: number) { | ||
| super(); | ||
| } | ||
| value = this.incoming; | ||
| } |
There was a problem hiding this comment.
Can you also add a test with super() in an expression position?
a ? super() : b;
Thanks to our very nice package structure, they are using the same helper injection initialization statements. 😃 |
| } | ||
| class Foo5 extends Foo { | ||
| constructor(incoming) { | ||
| var c = a ? (super(), (this.incoming = incoming, babelHelpers.defineProperty(this, "value", this.incoming)), this) : b; |
There was a problem hiding this comment.
var c = a ? (super(), (this.incoming = incoming, babelHelpers.defineProperty(this, "value", this.incoming)), this) : b;
vs
var c = a ? (super(), this.incoming = incoming, babelHelpers.defineProperty(this, "value", this.incoming), this) : b;
There is actually a potential bug here when more than two plugins inject initialization statements.
I tried to fix it, but had some difficulty, so I decided to defer to a future PR, which should affect very few people and introduce no new regressions.
The root of the problem is when we use insertAfter, it will insert a SequenceExpression even if it is already in a SequenceExpression.
I'm not sure if we should modify insertAfter or we should handle it manually in the plugin.
I tried to modify insertAfter, which caused a lot of CI errors. I tried to fix some of them, but there were still some difficulties.
I hope to be able to look into them after we reach a consensus.
There was a problem hiding this comment.
Don-t those two examples have the same behavior anyway? One just has unnecessary parentheses.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Because we are currently only searching for siblings of super().
|
Note that when using TS with Maybe we should keep this PR on hold until the TS team decides what is the expected behavior? |
The cause of the problem is that both the ts plugin and the class plugin insert the initialization statement after
super(), which will cause the initialization order to be reversed from the order in which the plugin is run.This PR makes the behavior to prefer inserting after the last inserted statement.