From 66bf8e907d09349d847a99036747edb724c4a20a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 22 Feb 2024 19:14:29 -0500 Subject: [PATCH] fix(es/codegen): Emit computed getter and setter signatures (#8656) **Description:** Emits computed getter/setter signatures. --- crates/swc_ecma_codegen/src/typescript.rs | 16 ++++++++++++++-- .../tests/fixture/typescript/interface/input.ts | 14 ++++++++++++++ .../tests/fixture/typescript/interface/output.ts | 13 +++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 crates/swc_ecma_codegen/tests/fixture/typescript/interface/input.ts create mode 100644 crates/swc_ecma_codegen/tests/fixture/typescript/interface/output.ts diff --git a/crates/swc_ecma_codegen/src/typescript.rs b/crates/swc_ecma_codegen/src/typescript.rs index 20326dd30347..bb133378ed07 100644 --- a/crates/swc_ecma_codegen/src/typescript.rs +++ b/crates/swc_ecma_codegen/src/typescript.rs @@ -935,7 +935,13 @@ where keyword!("get"); space!(); - emit!(n.key); + if n.computed { + punct!("["); + emit!(n.key); + punct!("]"); + } else { + emit!(n.key) + } punct!("("); punct!(")"); @@ -953,7 +959,13 @@ where keyword!("set"); space!(); - emit!(n.key); + if n.computed { + punct!("["); + emit!(n.key); + punct!("]"); + } else { + emit!(n.key) + } punct!("("); emit!(n.param); diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/interface/input.ts b/crates/swc_ecma_codegen/tests/fixture/typescript/interface/input.ts new file mode 100644 index 000000000000..4502827b0189 --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/interface/input.ts @@ -0,0 +1,14 @@ +const symbol1 = Symbol(); +const symbol2 = Symbol(); +const symbol3 = Symbol(); + +export interface Test { + [symbol1]: string; + [symbol2](): string; + get [symbol3](): string; + set [symbol3](value: string); + prop1: string; + method1(value: string, param2: number): string; + get method2(): string; + set method2(value: string); +} \ No newline at end of file diff --git a/crates/swc_ecma_codegen/tests/fixture/typescript/interface/output.ts b/crates/swc_ecma_codegen/tests/fixture/typescript/interface/output.ts new file mode 100644 index 000000000000..ab8b42f79148 --- /dev/null +++ b/crates/swc_ecma_codegen/tests/fixture/typescript/interface/output.ts @@ -0,0 +1,13 @@ +const symbol1 = Symbol(); +const symbol2 = Symbol(); +const symbol3 = Symbol(); +export interface Test { + [symbol1]: string; + [symbol2](): string; + get [symbol3](): string; + set [symbol3](value: string); + prop1: string; + method1(value: string, param2: number): string; + get method2(): string; + set method2(value: string); +}