Skip to content

Commit

Permalink
fix(es/transforms/base): Fix resolver (#1672)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_base:
 - `resolver`: Handle variables in for in/of loop body correctly.
  • Loading branch information
kdy1 authored May 10, 2021
1 parent b713972 commit 9381d0d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ecmascript/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.56.1"
version = "0.56.2"

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/transforms/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_base"
repository = "https://github.com/swc-project/swc.git"
version = "0.14.1"
version = "0.14.2"

[dependencies]
fxhash = "0.2.1"
Expand Down
15 changes: 13 additions & 2 deletions ecmascript/transforms/base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ impl<'a> Resolver<'a> {
}
}

fn visit_mut_stmt_within_child_scope(&mut self, s: &mut Stmt) {
let child_mark = Mark::fresh(self.mark);
let mut child = Resolver::new(
child_mark,
Scope::new(ScopeKind::Block, Some(&self.current)),
self.handle_types,
);

child.visit_mut_stmt_within_same_scope(s)
}

fn visit_mut_stmt_within_same_scope(&mut self, s: &mut Stmt) {
match s {
Stmt::Block(s) => {
Expand Down Expand Up @@ -638,7 +649,7 @@ impl<'a> VisitMut for Resolver<'a> {
n.left.visit_mut_with(&mut child);
n.right.visit_mut_with(&mut child);

child.visit_mut_stmt_within_same_scope(&mut *n.body);
child.visit_mut_stmt_within_child_scope(&mut *n.body);
}

fn visit_mut_for_of_stmt(&mut self, n: &mut ForOfStmt) {
Expand All @@ -652,7 +663,7 @@ impl<'a> VisitMut for Resolver<'a> {
n.left.visit_mut_with(&mut child);
n.right.visit_mut_with(&mut child);

child.visit_mut_stmt_within_same_scope(&mut *n.body);
child.visit_mut_stmt_within_child_scope(&mut *n.body);
}

fn visit_mut_for_stmt(&mut self, n: &mut ForStmt) {
Expand Down
14 changes: 14 additions & 0 deletions ecmascript/transforms/base/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2915,3 +2915,17 @@ fn issue_1653_2() {
",
);
}

to_ts!(
ts_for_of_statements_for_of_23_01,
"
for (const v of new FooIterator) {
const v = 0; // new scope
}
",
"
for (const v__1 of new FooIterator){
const v__3 = 0;
}
"
);

0 comments on commit 9381d0d

Please sign in to comment.