From 572bcaefc1b6ba9f92c57a35ede61fec9f3221bd Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 19 Jan 2024 00:09:06 -0500 Subject: [PATCH] fix(es/ast): Add `definite` and `is_override` to `AutoAccessor` (#8436) **Description:** Adds a `definite` and `is_override` property to `AutoAccessor` as they were missing. It also fixes codegen for `AutoAccessor` when emitting TypeScript (which is the main reason I need this PR). **BREAKING CHANGE:** Adds a `definite` and `is_override` property. **Related issue:** - Closes #8344 --- crates/swc_ecma_ast/src/class.rs | 8 + crates/swc_ecma_codegen/src/lib.rs | 16 + .../fixture/typescript/class_prop/input.js | 6 + .../fixture/typescript/class_prop/output.js | 6 + .../typescript/class_prop/output.min.js | 2 +- .../src/parser/class_and_fn.rs | 2 + .../tests/tsc/autoAccessor1.json | 16 +- .../tests/tsc/autoAccessor10.json | 20 +- .../tests/tsc/autoAccessor2.json | 16 +- .../tests/tsc/autoAccessor3.json | 16 +- .../tests/tsc/autoAccessor4.json | 16 +- .../tests/tsc/autoAccessor6.json | 4 +- .../tests/tsc/autoAccessor7.json | 8 +- .../tests/tsc/autoAccessor8.json | 24 +- .../tests/tsc/autoAccessor9.json | 24 +- .../tsc/autoAccessorAllowedModifiers.json | 68 +++- ...autoAccessorNoUseDefineForClassFields.json | 32 +- .../typescript/class/auto-accessor/input.ts | 11 + .../class/auto-accessor/input.ts.json | 381 ++++++++++++++++++ .../src/strip_type.rs | 8 + .../tests/fixture/auto-accessor/input.ts | 4 + .../tests/fixture/auto-accessor/output.js | 4 + .../tests/strip_correctness.rs | 1 + crates/swc_ecma_visit/src/lib.rs | 2 + 24 files changed, 633 insertions(+), 62 deletions(-) create mode 100644 crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts.json create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/input.ts create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/output.js diff --git a/crates/swc_ecma_ast/src/class.rs b/crates/swc_ecma_ast/src/class.rs index ba3762e2fae3..99b47a136f53 100644 --- a/crates/swc_ecma_ast/src/class.rs +++ b/crates/swc_ecma_ast/src/class.rs @@ -331,6 +331,12 @@ pub struct AutoAccessor { /// Typescript extension. #[cfg_attr(feature = "serde-impl", serde(default))] pub accessibility: Option, + + #[cfg_attr(feature = "serde-impl", serde(default))] + pub is_override: bool, + + #[cfg_attr(feature = "serde-impl", serde(default))] + pub definite: bool, } impl Take for AutoAccessor { @@ -343,6 +349,8 @@ impl Take for AutoAccessor { is_static: false, decorators: Take::dummy(), accessibility: None, + is_override: false, + definite: false, } } } diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 9c9984cbabf5..945e48371eae 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -1353,16 +1353,32 @@ where fn emit_auto_accessor(&mut self, n: &AutoAccessor) -> Result { self.emit_list(n.span, Some(&n.decorators), ListFormat::Decorators)?; + self.emit_accessibility(n.accessibility)?; + if n.is_static { keyword!("static"); space!(); } + if n.is_override { + keyword!("override"); + space!(); + } + keyword!("accessor"); space!(); emit!(n.key); + if let Some(type_ann) = &n.type_ann { + if n.definite { + punct!("!"); + } + punct!(":"); + space!(); + emit!(type_ann); + } + if let Some(init) = &n.value { formatting_space!(); punct!("="); diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js index f82cedf0cd44..c3c20abba303 100644 --- a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js @@ -9,5 +9,11 @@ class MyClass extends Base { public abstract override readonly prop7 = 5; override readonly #prop8 = 5; declare public static readonly prop9: string; + accessor prop10!: string = ""; + public accessor prop11 = ""; + public static accessor prop12 = ""; + public prop13: string; + private static prop14: string; + protected override accessor prop15 = 5; [value]?: string[]; } diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js index f82cedf0cd44..c3c20abba303 100644 --- a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js @@ -9,5 +9,11 @@ class MyClass extends Base { public abstract override readonly prop7 = 5; override readonly #prop8 = 5; declare public static readonly prop9: string; + accessor prop10!: string = ""; + public accessor prop11 = ""; + public static accessor prop12 = ""; + public prop13: string; + private static prop14: string; + protected override accessor prop15 = 5; [value]?: string[]; } diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js index d69dcd1a32d5..58f4f301b48b 100644 --- a/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js @@ -1 +1 @@ -class MyClass extends Base{prop1?: string;prop2!: string;#prop3?: string;#prop4?: string="test";#privateOptionalNoType?;static readonly prop5!: string;readonly #prop6="asdf";public abstract override readonly prop7=5;override readonly #prop8=5;declare public static readonly prop9: string;[value]?: string[]} +class MyClass extends Base{prop1?: string;prop2!: string;#prop3?: string;#prop4?: string="test";#privateOptionalNoType?;static readonly prop5!: string;readonly #prop6="asdf";public abstract override readonly prop7=5;override readonly #prop8=5;declare public static readonly prop9: string;accessor prop10!: string="";public accessor prop11="";public static accessor prop12="";public prop13: string;private static prop14: string;protected override accessor prop15=5;[value]?: string[]} diff --git a/crates/swc_ecma_parser/src/parser/class_and_fn.rs b/crates/swc_ecma_parser/src/parser/class_and_fn.rs index c08715d694c0..a566ba28b8de 100644 --- a/crates/swc_ecma_parser/src/parser/class_and_fn.rs +++ b/crates/swc_ecma_parser/src/parser/class_and_fn.rs @@ -1093,6 +1093,8 @@ impl Parser { is_static, decorators, accessibility, + is_override, + definite, })); } diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor1.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor1.json index 49a932136390..1aae0883a373 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor1.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor1.json @@ -63,7 +63,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -95,7 +97,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -134,7 +138,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -166,7 +172,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor10.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor10.json index 414775327d1a..ab0b274daaa4 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor10.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor10.json @@ -56,7 +56,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -159,7 +161,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -297,7 +301,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "StaticBlock", @@ -429,7 +435,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -488,7 +496,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor2.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor2.json index 3a24ab3e0362..43f627f7b32a 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor2.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor2.json @@ -71,7 +71,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -111,7 +113,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -158,7 +162,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -198,7 +204,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "Constructor", diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor3.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor3.json index 4adbffce24a6..49b35a600ab8 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor3.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor3.json @@ -63,7 +63,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -95,7 +97,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -134,7 +138,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -166,7 +172,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor4.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor4.json index abe3579415d7..8093d48f0ac2 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor4.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor4.json @@ -63,7 +63,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -95,7 +97,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -134,7 +138,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -166,7 +172,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor6.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor6.json index 56185dd23193..ca29c7d28c64 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor6.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor6.json @@ -63,7 +63,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor7.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor7.json index eb0663baba78..778ed91d6a31 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor7.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor7.json @@ -63,7 +63,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -122,7 +124,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": { diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor8.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor8.json index 7485aefb562a..1000d62a29ef 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor8.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor8.json @@ -63,7 +63,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -102,7 +104,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -168,7 +172,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -207,7 +213,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -301,7 +309,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -340,7 +350,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessor9.json b/crates/swc_ecma_parser/tests/tsc/autoAccessor9.json index 6c6b4ce6da44..75918a349602 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessor9.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessor9.json @@ -56,7 +56,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -153,7 +155,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "ClassProperty", @@ -294,7 +298,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -544,7 +550,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "ClassProperty", @@ -641,7 +649,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -738,7 +748,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "ClassProperty", diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessorAllowedModifiers.json b/crates/swc_ecma_parser/tests/tsc/autoAccessorAllowedModifiers.json index 63687e7c38eb..ffcdb6125067 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessorAllowedModifiers.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessorAllowedModifiers.json @@ -63,7 +63,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -102,7 +104,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": "public" + "accessibility": "public", + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -141,7 +145,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": "private" + "accessibility": "private", + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -180,7 +186,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": "protected" + "accessibility": "protected", + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -219,7 +227,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -258,7 +268,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -297,7 +309,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": "public" + "accessibility": "public", + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -336,7 +350,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": "private" + "accessibility": "private", + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -375,7 +391,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": "protected" + "accessibility": "protected", + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -422,7 +440,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -461,7 +481,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -500,7 +522,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -547,7 +571,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -586,7 +612,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": true } ], "superClass": null, @@ -652,7 +680,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": true, + "definite": false }, { "type": "AutoAccessor", @@ -691,7 +721,9 @@ }, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": true, + "definite": false } ], "superClass": { @@ -766,7 +798,9 @@ }, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, diff --git a/crates/swc_ecma_parser/tests/tsc/autoAccessorNoUseDefineForClassFields.json b/crates/swc_ecma_parser/tests/tsc/autoAccessorNoUseDefineForClassFields.json index 25f5f047eb51..d876c7286bc9 100644 --- a/crates/swc_ecma_parser/tests/tsc/autoAccessorNoUseDefineForClassFields.json +++ b/crates/swc_ecma_parser/tests/tsc/autoAccessorNoUseDefineForClassFields.json @@ -56,7 +56,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -123,7 +125,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -190,7 +194,9 @@ "typeAnnotation": null, "isStatic": true, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "AutoAccessor", @@ -230,7 +236,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -289,7 +297,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -356,7 +366,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -461,7 +473,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false } ], "superClass": null, @@ -528,7 +542,9 @@ "typeAnnotation": null, "isStatic": false, "decorators": [], - "accessibility": null + "accessibility": null, + "isOverride": false, + "definite": false }, { "type": "ClassProperty", diff --git a/crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts b/crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts new file mode 100644 index 000000000000..6bfbd0142bcd --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts @@ -0,0 +1,11 @@ +class C1 extends B { + accessor a: any; + accessor b = 1; + static accessor c: any; + static accessor d = 2; + public static accessor e = 4; + protected static accessor f: number; + private accessor g!: string; + public accessor h = 1; + public override accessor i = 10; +} diff --git a/crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts.json b/crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts.json new file mode 100644 index 000000000000..4bf764ad9213 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/class/auto-accessor/input.ts.json @@ -0,0 +1,381 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 291, + "ctxt": 0 + }, + "body": [ + { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 7, + "end": 9, + "ctxt": 0 + }, + "value": "C1", + "optional": false + }, + "declare": false, + "span": { + "start": 1, + "end": 291, + "ctxt": 0 + }, + "decorators": [], + "body": [ + { + "type": "AutoAccessor", + "span": { + "start": 26, + "end": 42, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 35, + "end": 36, + "ctxt": 0 + }, + "value": "a", + "optional": false + }, + "value": null, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 36, + "end": 41, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 38, + "end": 41, + "ctxt": 0 + }, + "kind": "any" + } + }, + "isStatic": false, + "decorators": [], + "accessibility": null, + "isOverride": false, + "definite": false + }, + { + "type": "AutoAccessor", + "span": { + "start": 47, + "end": 62, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 56, + "end": 57, + "ctxt": 0 + }, + "value": "b", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 60, + "end": 61, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + }, + "typeAnnotation": null, + "isStatic": false, + "decorators": [], + "accessibility": null, + "isOverride": false, + "definite": false + }, + { + "type": "AutoAccessor", + "span": { + "start": 67, + "end": 90, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 83, + "end": 84, + "ctxt": 0 + }, + "value": "c", + "optional": false + }, + "value": null, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 84, + "end": 89, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 86, + "end": 89, + "ctxt": 0 + }, + "kind": "any" + } + }, + "isStatic": true, + "decorators": [], + "accessibility": null, + "isOverride": false, + "definite": false + }, + { + "type": "AutoAccessor", + "span": { + "start": 95, + "end": 117, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 111, + "end": 112, + "ctxt": 0 + }, + "value": "d", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 115, + "end": 116, + "ctxt": 0 + }, + "value": 2.0, + "raw": "2" + }, + "typeAnnotation": null, + "isStatic": true, + "decorators": [], + "accessibility": null, + "isOverride": false, + "definite": false + }, + { + "type": "AutoAccessor", + "span": { + "start": 122, + "end": 151, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 145, + "end": 146, + "ctxt": 0 + }, + "value": "e", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 149, + "end": 150, + "ctxt": 0 + }, + "value": 4.0, + "raw": "4" + }, + "typeAnnotation": null, + "isStatic": true, + "decorators": [], + "accessibility": "public", + "isOverride": false, + "definite": false + }, + { + "type": "AutoAccessor", + "span": { + "start": 156, + "end": 192, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 182, + "end": 183, + "ctxt": 0 + }, + "value": "f", + "optional": false + }, + "value": null, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 183, + "end": 191, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 185, + "end": 191, + "ctxt": 0 + }, + "kind": "number" + } + }, + "isStatic": true, + "decorators": [], + "accessibility": "protected", + "isOverride": false, + "definite": false + }, + { + "type": "AutoAccessor", + "span": { + "start": 197, + "end": 225, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 214, + "end": 215, + "ctxt": 0 + }, + "value": "g", + "optional": false + }, + "value": null, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 216, + "end": 224, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 218, + "end": 224, + "ctxt": 0 + }, + "kind": "string" + } + }, + "isStatic": false, + "decorators": [], + "accessibility": "private", + "isOverride": false, + "definite": true + }, + { + "type": "AutoAccessor", + "span": { + "start": 230, + "end": 252, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 246, + "end": 247, + "ctxt": 0 + }, + "value": "h", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 250, + "end": 251, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + }, + "typeAnnotation": null, + "isStatic": false, + "decorators": [], + "accessibility": "public", + "isOverride": false, + "definite": false + }, + { + "type": "AutoAccessor", + "span": { + "start": 257, + "end": 289, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 282, + "end": 283, + "ctxt": 0 + }, + "value": "i", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 286, + "end": 288, + "ctxt": 0 + }, + "value": 10.0, + "raw": "10" + }, + "typeAnnotation": null, + "isStatic": false, + "decorators": [], + "accessibility": "public", + "isOverride": true, + "definite": false + } + ], + "superClass": { + "type": "Identifier", + "span": { + "start": 18, + "end": 19, + "ctxt": 0 + }, + "value": "B", + "optional": false + }, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_transforms_typescript/src/strip_type.rs b/crates/swc_ecma_transforms_typescript/src/strip_type.rs index a12a89174429..df6b38fee55a 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip_type.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip_type.rs @@ -162,6 +162,14 @@ impl VisitMut for StripType { prop.visit_mut_children_with(self); } + fn visit_mut_auto_accessor(&mut self, n: &mut AutoAccessor) { + n.type_ann = None; + n.accessibility = None; + n.definite = false; + n.is_override = false; + n.visit_mut_children_with(self); + } + fn visit_mut_ts_import_equals_decl(&mut self, _: &mut TsImportEqualsDecl) { // n.id.visit_mut_with(self); } diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/input.ts b/crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/input.ts new file mode 100644 index 000000000000..4f4cd98ead30 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/input.ts @@ -0,0 +1,4 @@ +class Test extends B { + accessor a!: string; + public override accessor b: string = ""; +} diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/output.js new file mode 100644 index 000000000000..e3746715bd53 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/auto-accessor/output.js @@ -0,0 +1,4 @@ +class Test extends B { + accessor a; + accessor b = ""; +} diff --git a/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs b/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs index 3f8d808ad4de..4fd229a1bb86 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip_correctness.rs @@ -239,6 +239,7 @@ fn identity(entry: PathBuf) { export_default_from: true, import_attributes: true, allow_super_outside_method: true, + auto_accessors: true, ..Default::default() }), (&*js_fm).into(), diff --git a/crates/swc_ecma_visit/src/lib.rs b/crates/swc_ecma_visit/src/lib.rs index 1fd732a068f0..01213861e4ba 100644 --- a/crates/swc_ecma_visit/src/lib.rs +++ b/crates/swc_ecma_visit/src/lib.rs @@ -1878,6 +1878,8 @@ define!({ pub is_static: bool, pub decorators: Vec, pub accessibility: Option, + pub is_override: bool, + pub definite: bool, } pub enum Key {