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| {