Skip to content

Commit 5a44c6b

Browse files
authored
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 #4574. I'm not sure why do that because I didn't find related test cases. **Related issue:** - Closes #9785
1 parent e6fc532 commit 5a44c6b

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

.changeset/two-terms-invite.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_transforms_base: patch
4+
---
5+
6+
fix(es/transformation): Fix wrong syntax context of variables with the same names as catch params
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function dist_index_es_P(e) {
2+
try {
3+
t = JSON.stringify(e);
4+
} catch (r) {
5+
t = String(e);
6+
}
7+
for (var r = 0, o = 0; o < t.length; o++) {
8+
r += 1;
9+
}
10+
console.log(r);
11+
return r;
12+
}
13+
14+
dist_index_es_P("aa");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
!function(e) {
2+
try {
3+
t = JSON.stringify("aa");
4+
} catch (r) {
5+
t = String("aa");
6+
}
7+
for(var r = 0, o = 0; o < t.length; o++)r += 1;
8+
console.log(r);
9+
}(0);

crates/swc_ecma_transforms_base/src/resolver/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1627,14 +1627,14 @@ impl VisitMut for Hoister<'_, '_> {
16271627

16281628
let params: Vec<Id> = find_pat_ids(&c.param);
16291629

1630+
let orig = self.catch_param_decls.clone();
1631+
16301632
self.catch_param_decls
16311633
.extend(params.into_iter().map(|v| v.0));
16321634

16331635
self.in_catch_body = true;
16341636
c.body.visit_mut_with(self);
16351637

1636-
let orig = self.catch_param_decls.clone();
1637-
16381638
// let mut excluded = find_ids::<_, Id>(&c.body);
16391639

16401640
// excluded.retain(|id| {

0 commit comments

Comments
 (0)