From 5a44c6b42471aeceb3771b1cf4ebb310d03a0154 Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Mon, 9 Dec 2024 10:38:27 +0800 Subject: [PATCH] fix(es/resolver): Fix wrong syntax context of vars with the same names as catch params (#9786) **Description:** When hoisting variables, the names of catch params are saved in `catch_param_decls` permanently in the process. So the following code skips resolving the variables with the same names. https://github.com/swc-project/swc/blob/e6fc5327b1a309eae840fe1ec3a2367adab37430/crates/swc_ecma_transforms_base/src/resolver/mod.rs#L1558 I think this is introduced in https://github.com/swc-project/swc/pull/4574. I'm not sure why do that because I didn't find related test cases. **Related issue:** - Closes https://github.com/swc-project/swc/issues/9785 --- .changeset/two-terms-invite.md | 6 ++++++ .../tests/fixture/issues/9785/input.js | 14 ++++++++++++++ .../tests/fixture/issues/9785/output.js | 9 +++++++++ .../swc_ecma_transforms_base/src/resolver/mod.rs | 4 ++-- 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/two-terms-invite.md create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/9785/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/9785/output.js diff --git a/.changeset/two-terms-invite.md b/.changeset/two-terms-invite.md new file mode 100644 index 000000000000..061780463a7e --- /dev/null +++ b/.changeset/two-terms-invite.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_transforms_base: patch +--- + +fix(es/transformation): Fix wrong syntax context of variables with the same names as catch params diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/9785/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/9785/input.js new file mode 100644 index 000000000000..477730c6991a --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9785/input.js @@ -0,0 +1,14 @@ +function dist_index_es_P(e) { + try { + t = JSON.stringify(e); + } catch (r) { + t = String(e); + } + for (var r = 0, o = 0; o < t.length; o++) { + r += 1; + } + console.log(r); + return r; +} + +dist_index_es_P("aa"); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/9785/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/9785/output.js new file mode 100644 index 000000000000..3d2bb22a9099 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/9785/output.js @@ -0,0 +1,9 @@ +!function(e) { + try { + t = JSON.stringify("aa"); + } catch (r) { + t = String("aa"); + } + for(var r = 0, o = 0; o < t.length; o++)r += 1; + console.log(r); +}(0); diff --git a/crates/swc_ecma_transforms_base/src/resolver/mod.rs b/crates/swc_ecma_transforms_base/src/resolver/mod.rs index 0c71e668d3b9..30cd3fc2212f 100644 --- a/crates/swc_ecma_transforms_base/src/resolver/mod.rs +++ b/crates/swc_ecma_transforms_base/src/resolver/mod.rs @@ -1627,14 +1627,14 @@ impl VisitMut for Hoister<'_, '_> { let params: Vec = find_pat_ids(&c.param); + let orig = self.catch_param_decls.clone(); + self.catch_param_decls .extend(params.into_iter().map(|v| v.0)); self.in_catch_body = true; c.body.visit_mut_with(self); - let orig = self.catch_param_decls.clone(); - // let mut excluded = find_ids::<_, Id>(&c.body); // excluded.retain(|id| {