From f416aff7d7036de72509132603d9b423a0b95f68 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Thu, 25 Jan 2024 16:44:02 +0800 Subject: [PATCH] fix(es/decorator): Preserve state while traversing the `module_items` scope (#8556) **Related issue:** - Closes #8551 --- .../swc/tests/fixture/issues-8xxx/8551/input/.swcrc | 13 +++++++++++++ .../tests/fixture/issues-8xxx/8551/input/index.ts | 7 +++++++ .../tests/fixture/issues-8xxx/8551/output/index.ts | 10 ++++++++++ .../src/decorator_2022_03.rs | 10 ++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-8xxx/8551/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-8xxx/8551/input/index.ts create mode 100644 crates/swc/tests/fixture/issues-8xxx/8551/output/index.ts diff --git a/crates/swc/tests/fixture/issues-8xxx/8551/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8551/input/.swcrc new file mode 100644 index 000000000000..26b3251c1873 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8551/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/8551/input/index.ts b/crates/swc/tests/fixture/issues-8xxx/8551/input/index.ts new file mode 100644 index 000000000000..6cdc8ce59826 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8551/input/index.ts @@ -0,0 +1,7 @@ +class C { + [Symbol.iterator]() { } +} + +namespace NS { + export function f() { } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8551/output/index.ts b/crates/swc/tests/fixture/issues-8xxx/8551/output/index.ts new file mode 100644 index 000000000000..4c436895fef9 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8551/output/index.ts @@ -0,0 +1,10 @@ +var _computedKey; +_computedKey = Symbol.iterator; +class C { + [_computedKey]() {} +} +var NS; +(function(NS) { + function f() {} + NS.f = f; +})(NS || (NS = {})); 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 fbdd8b6f1134..1227e9af286f 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs @@ -1499,7 +1499,10 @@ impl VisitMut for Decorator202203 { } fn visit_mut_module_items(&mut self, n: &mut Vec) { - let old_extra_lets = self.extra_lets.take(); + let extra_vars = self.extra_vars.take(); + let extra_lets = self.extra_lets.take(); + let pre_class_inits = self.pre_class_inits.take(); + let extra_exports = self.extra_exports.take(); let mut new = Vec::with_capacity(n.len()); @@ -1559,7 +1562,10 @@ impl VisitMut for Decorator202203 { n.visit_mut_with(&mut IdentRenamer::new(&self.rename_map)); } - self.extra_lets = old_extra_lets; + self.extra_vars = extra_vars; + self.extra_lets = extra_lets; + self.pre_class_inits = pre_class_inits; + self.extra_exports = extra_exports; } fn visit_mut_private_prop(&mut self, p: &mut PrivateProp) {