From e0bdc0f7c210c73f0291ab72e380743fe5f03b72 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 3 Jan 2024 23:48:07 -0500 Subject: [PATCH] fix(es/codegen): Emit `?` for an optional computed property (#8481) **Description:** Emits optional `?` for a computed property. --- crates/swc_ecma_codegen/src/lib.rs | 14 +++++++++++--- .../tests/fixture/typescript/class_prop/input.js | 2 ++ .../tests/fixture/typescript/class_prop/output.js | 2 ++ .../fixture/typescript/class_prop/output.min.js | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 249adb031e4..3d00612f616 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -1566,10 +1566,12 @@ where } emit!(n.key); + + if n.is_optional { + punct!("?"); + } + if let Some(type_ann) = &n.type_ann { - if n.is_optional { - punct!("?"); - } if n.definite { punct!("!"); } @@ -1635,6 +1637,12 @@ where emit!(n.key); + // emit for a computed property, but not an identifier already marked as + // optional + if n.is_optional && !n.key.as_ident().map(|i| i.optional).unwrap_or(false) { + punct!("?"); + } + if let Some(ty) = &n.type_ann { if n.definite { 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 bd8b4ccbc30..f82cedf0cd4 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 @@ -3,9 +3,11 @@ class MyClass extends Base { 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[]; } 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 bd8b4ccbc30..f82cedf0cd4 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 @@ -3,9 +3,11 @@ class MyClass extends Base { 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[]; } 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 0bed982a8c8..d69dcd1a32d 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";static readonly prop5!: string;readonly #prop6="asdf";public abstract override readonly prop7=5;override readonly #prop8=5;declare public static readonly prop9: 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;[value]?: string[]}