From 7566ddf0b7802650f0f25ca7f7c607f58faa4972 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 17 Dec 2023 07:47:20 +0100 Subject: [PATCH] fix(es/codegen): Fix codegen of ts specific syntax in class props (#8426) **Description:** This fixes the codegen of private properties with `is_optional` or `definite` and class properties with `definite`. --- crates/swc_ecma_codegen/src/lib.rs | 9 +++++++++ .../tests/fixture/typescript/class_prop/input.js | 8 ++++++++ .../tests/fixture/typescript/class_prop/output.js | 8 ++++++++ .../fixture/typescript/class_prop/output.min.js | 1 + .../fixture/legacy-metadata/issues/1160/1/output.ts | 2 +- .../fixture/legacy-metadata/issues/1421/1/output.ts | 2 +- .../tests/fixture/legacy-only/issues/862/1/output.ts | 12 ++++++------ .../tests/fixture/legacy-only/issues/862/2/output.ts | 2 +- .../swc_ecma_transforms_typescript/src/strip_type.rs | 2 ++ 9 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js create mode 100644 crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js create mode 100644 crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 1277d2d7e70..5b825db2abd 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -1533,6 +1533,12 @@ where emit!(n.key); if let Some(type_ann) = &n.type_ann { + if n.is_optional { + punct!("?"); + } + if n.definite { + punct!("!"); + } punct!(":"); space!(); emit!(type_ann); @@ -1583,6 +1589,9 @@ where emit!(n.key); if let Some(ty) = &n.type_ann { + if n.definite { + punct!("!"); + } punct!(":"); space!(); emit!(ty); 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 new file mode 100644 index 00000000000..b5325b3ca63 --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/input.js @@ -0,0 +1,8 @@ +class MyClass { + prop1?: string; + prop2!: string; + #prop3?: string; + #prop4?: string = "test"; + static readonly prop5!: string; + readonly #prop6 = "asdf"; +} 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 new file mode 100644 index 00000000000..b5325b3ca63 --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.js @@ -0,0 +1,8 @@ +class MyClass { + prop1?: string; + prop2!: string; + #prop3?: string; + #prop4?: string = "test"; + static readonly prop5!: string; + readonly #prop6 = "asdf"; +} 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 new file mode 100644 index 00000000000..4d6c0686d64 --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/class_prop/output.min.js @@ -0,0 +1 @@ +class MyClass{prop1?: string;prop2!: string;#prop3?: string;#prop4?: string="test";static readonly prop5!: string;readonly #prop6="asdf"} diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts index 399561474cb..a2c767876c7 100644 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts @@ -3,7 +3,7 @@ enum MyEnum { y = "yyy" } class Xpto { - value: MyEnum; + value!: MyEnum; } _ts_decorate([ Decorator(), diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts index 722c2d7678f..38a6e5e5013 100644 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts @@ -1,5 +1,5 @@ class User { - currency: "usd" | "eur" | "yen"; + currency!: "usd" | "eur" | "yen"; } _ts_decorate([ column(), diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts index 01c4d06529e..224206c1f37 100644 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts @@ -1,10 +1,10 @@ export class Product extends TimestampedEntity { - id: string; - price: number; - type: ProductType; - productEntityId: string; - /* ANCHOR: Relations ------------------------------------------------------ */ orders: Order[]; - discounts: Discount[]; + id!: string; + price!: number; + type!: ProductType; + productEntityId!: string; + /* ANCHOR: Relations ------------------------------------------------------ */ orders!: Order[]; + discounts!: Discount[]; } _ts_decorate([ PrimaryGeneratedColumn("uuid") diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts index 7d78e10d2b4..1642c73ce82 100644 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts @@ -1,5 +1,5 @@ export class Product extends TimestampedEntity { - id: string; + id!: string; } _ts_decorate([ PrimaryGeneratedColumn("uuid") diff --git a/crates/swc_ecma_transforms_typescript/src/strip_type.rs b/crates/swc_ecma_transforms_typescript/src/strip_type.rs index ee5dcda9738..b62a9b14577 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip_type.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip_type.rs @@ -144,6 +144,7 @@ impl VisitMut for StripType { prop.readonly = false; prop.is_override = false; prop.is_optional = false; + prop.definite = false; prop.accessibility = None; prop.visit_mut_children_with(self); } @@ -152,6 +153,7 @@ impl VisitMut for StripType { prop.readonly = false; prop.is_override = false; prop.is_optional = false; + prop.definite = false; prop.accessibility = None; prop.visit_mut_children_with(self); }