From 6a8dd8cbb384372657923d4c1094e8053661aa56 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Thu, 25 Jan 2024 15:19:20 +0800 Subject: [PATCH] fix(es/decorator): Skip TypeScript class method/prop declarations (#8555) **Related issue:** - Closes #8552 --- .../tests/fixture/issues-8xxx/8552/input/.swcrc | 13 +++++++++++++ .../tests/fixture/issues-8xxx/8552/input/index.ts | 4 ++++ .../fixture/issues-8xxx/8552/output/index.ts | 2 ++ .../src/decorator_2022_03.rs | 15 ++++++++++++--- 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-8xxx/8552/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-8xxx/8552/input/index.ts create mode 100644 crates/swc/tests/fixture/issues-8xxx/8552/output/index.ts diff --git a/crates/swc/tests/fixture/issues-8xxx/8552/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8552/input/.swcrc new file mode 100644 index 000000000000..26b3251c1873 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8552/input/.swcrc @@ -0,0 +1,13 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "transform": { + "decoratorVersion": "2022-03" + }, + "target": "es2022" + }, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8552/input/index.ts b/crates/swc/tests/fixture/issues-8xxx/8552/input/index.ts new file mode 100644 index 000000000000..da534c2a0747 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8552/input/index.ts @@ -0,0 +1,4 @@ +abstract class C { + abstract [Symbol.iterator](); + declare [Symbol.toStringTag]; +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8552/output/index.ts b/crates/swc/tests/fixture/issues-8xxx/8552/output/index.ts new file mode 100644 index 000000000000..8ad54365639d --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8552/output/index.ts @@ -0,0 +1,2 @@ +class C { +} diff --git a/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs b/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs index 5368144b84c9..fbdd8b6f1134 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs @@ -735,14 +735,14 @@ impl Decorator202203 { fn process_decorators_of_class_members(&mut self, members: &mut [ClassMember]) { for mut m in members { match &mut m { - ClassMember::Method(m) => { + ClassMember::Method(m) if m.function.body.is_some() => { self.process_decorators(&mut m.function.decorators); self.process_prop_name(&mut m.key); } - ClassMember::PrivateMethod(m) => { + ClassMember::PrivateMethod(m) if m.function.body.is_some() => { self.process_decorators(&mut m.function.decorators); } - ClassMember::ClassProp(m) => { + ClassMember::ClassProp(m) if !m.declare => { self.process_decorators(&mut m.decorators); self.process_prop_name(&mut m.key); } @@ -1333,6 +1333,11 @@ impl VisitMut for Decorator202203 { } fn visit_mut_class_method(&mut self, n: &mut ClassMethod) { + // method without body is TypeScript's method declaration. + if n.function.body.is_none() { + return; + } + n.visit_mut_children_with(self); if n.function.decorators.is_empty() { @@ -1383,6 +1388,10 @@ impl VisitMut for Decorator202203 { } fn visit_mut_class_prop(&mut self, p: &mut ClassProp) { + if p.declare { + return; + } + p.visit_mut_children_with(self); if p.decorators.is_empty() {