Skip to content

Commit

Permalink
fix(es/resolver): Fix wrong syntax context of vars with the same name…
Browse files Browse the repository at this point in the history
…s 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
#4574. I'm not sure why do that
because I didn't find related test cases.

**Related issue:**

 - Closes #9785
  • Loading branch information
CPunisher authored Dec 9, 2024
1 parent e6fc532 commit 5a44c6b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/two-terms-invite.md
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9785/input.js
Original file line number Diff line number Diff line change
@@ -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");
9 changes: 9 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9785/output.js
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,14 +1627,14 @@ impl VisitMut for Hoister<'_, '_> {

let params: Vec<Id> = 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| {
Expand Down

0 comments on commit 5a44c6b

Please sign in to comment.