-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix(es/compat): Handle useDefineForClassFields: false
#7055
Conversation
swc/crates/swc_ecma_transforms_typescript/src/strip.rs Lines 55 to 71 in d4ebb5e
swc/crates/swc/src/config/mod.rs Lines 1479 to 1482 in d4ebb5e
Currently, the default value for |
I think so. Thank you! |
So, we choose the Define semantics when there is no input value from the user, right? |
Yes, I think it's more important to be consistent than to align with |
425b13b
to
37765b4
Compare
It appears that microsoft/TypeScript#45995 is a bug in TypeScript that occurs when When its value is present, the order of the TS constructor parameter properties always precedes the ECMAScript class field for the four cases of ES Version and useDefineForClassFields: true/false. Please refer to the following table for more detailed information.
|
useDefineForClassFields: false
in ecmascriptuseDefineForClassFields: false
🤣 It was a bug of |
The microsoft/TypeScript#45995 issue arises only when useDefineForClassFields is missing. However, if it is explicitly set to either true or false, then TS's constructor parameter properties consistently appear first. Hence, we must adhere to TS's behavior. Moreover, there is another issue in TS where the initialization of constructor parameter properties does not always take effect. Check the following code. // @target: es2022
// @useDefineForClassFields: true
class Foo {
b = this.a;
constructor(public a = 1){
}
} output: class Foo {
a;
b = this.a;
constructor(a = 1) {
this.a = a;
}
} The value obtained by b is undefined. However, if we reduce the ES version to below ES2022, class Foo {
constructor(a = 1) {
Object.defineProperty(this, "a", {
enumerable: true,
configurable: true,
writable: true,
value: a
});
Object.defineProperty(this, "b", {
enumerable: true,
configurable: true,
writable: true,
value: this.a
});
}
} B will get the correct value. I believe that such a situation is more reasonable. Therefore, for the scenarios above ES2022, I have made an improvement. SWC will output the following code to ensure correctness. class Foo {
a;
b;
constructor(a = 1) {
this.a = a;
this.b = this.a;
}
} |
Sounds good, thank you so much for your hard work. |
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.
Thank you!
swc-bump:
- swc_ecma_ast
- swc --breaking
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.
Automated review comment generated by auto-rebase script
Description:
BREAKING CHANGE:
IMPORTANT NOTE: Users of decorators are recommended to configure
"useDefineForClassFields": false
to ensure that your code is properly transpiled.Related issue (if exists):
useDefineForClassFields
behavior #6985