Skip to content

fix: Wrong order of initialization in TS class constructor - #15583

Open
liuxingbaoyu wants to merge 2 commits into
babel:mainfrom
liuxingbaoyu:fix-constructor-order
Open

liuxingbaoyu wants to merge 2 commits into
babel:mainfrom
liuxingbaoyu:fix-constructor-order

Conversation

@liuxingbaoyu

@liuxingbaoyu liuxingbaoyu commented Apr 22, 2023 •

Copy link
Copy Markdown
Member
Q                       A
Fixed Issues? Fixes #15530, Fixes #15268
Patch: Bug Fix? √
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? √
Documentation PR Link
Any Dependency Changes?
License MIT

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.

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories area: typescript labels Apr 22, 2023
@liuxingbaoyu
liuxingbaoyu force-pushed the fix-constructor-order branch from 42427d0 to c0e7f12 Compare April 22, 2023 03:53
@babel-bot

babel-bot commented Apr 22, 2023 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54357/

@babel-bot

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54317/

@JLHwung

JLHwung commented Apr 24, 2023

Copy link
Copy Markdown
Contributor

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.

@liuxingbaoyu

Copy link
Copy Markdown
Member Author

Ok, I tried to explain it in the description.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but in Babel 8 we should emit this based on the setPublicClassFields assumption.

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment on lines +9 to +14
class Foo3 extends Foo {
constructor(private readonly incoming: number) {
super();
}
value = this.incoming;
}

Copy link
Copy Markdown
Member

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?

a ? super() : b;

@liuxingbaoyu

liuxingbaoyu commented Apr 25, 2023 •

Copy link
Copy Markdown
Member Author

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?

injectInitialization(classPath, path, assigns);

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;

@liuxingbaoyu liuxingbaoyu May 6, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

Note that when using TS with useDefineForClassFields and targeting a modern ES version TypeScript first initialized class fields and then parameter properties: microsoft/TypeScript#45995

Maybe we should keep this PR on hold until the TS team decides what is the expected behavior?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: typescript PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Class Fields

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Declare fields and class-properties are incompatible [Bug]: typescritpt constructor order

4 participants