fix(es/resolver): Handle var after catch with the same name (#4772)

This commit is contained in:
brightwu 2022-05-24 15:53:13 +08:00 committed by GitHub
parent 43f9b94c3f
commit e2da3ddc03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 1 deletions

View File

@ -1027,7 +1027,7 @@ impl<'a> VisitMut for Resolver<'a> {
catch_param_decls: Default::default(),
excluded_from_catch: Default::default(),
};
stmts.visit_mut_children_with(&mut hoister)
stmts.visit_mut_with(&mut hoister)
}
// Phase 2.
@ -1823,4 +1823,40 @@ impl VisitMut for Hoister<'_, '_> {
fn visit_mut_var_declarator(&mut self, node: &mut VarDeclarator) {
node.name.visit_mut_with(self);
}
/// should visit var decls first, cause val decl may appear behind the
/// usage. this can deal with code below:
/// ```js
/// try {} catch (Ic) {
/// throw Ic;
/// }
/// var Ic;
/// ```
/// the `Ic` defined by catch param and the `Ic` defined by `var Ic` are
/// different variables.
/// If we deal with the `var Ic` first, we can know
/// that there is already an global declaration of Ic when deal with the try
/// block.
fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
let mut other_items = vec![];
for item in items {
match item {
ModuleItem::Stmt(Stmt::Decl(Decl::Var(decl)))
| ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Var(decl),
..
})) if decl.kind == VarDeclKind::Var => {
item.visit_mut_with(self);
}
_ => {
other_items.push(item);
}
}
}
for other_item in other_items {
other_item.visit_mut_with(self);
}
}
}

View File

@ -0,0 +1,8 @@
try {
console.log(111);
} catch (Ic) {
throw Ic;
}
var jc, Ic;
(Ic = jc).PV = "page_view";

View File

@ -0,0 +1,7 @@
try {
console.log(111);
} catch (Ic__3) {
throw Ic__3;
}
var jc__1, Ic__1;
(Ic__1 = jc__1).PV = "page_view";

View File

@ -0,0 +1,8 @@
try {
console.log(111);
} catch (Ic) {
throw Ic;
}
export var jc, Ic;
(Ic = jc).PV = "page_view";

View File

@ -0,0 +1,7 @@
try {
console.log(111);
} catch (Ic__3) {
throw Ic__3;
}
export var jc__1, Ic__1;
(Ic__1 = jc__1).PV = "page_view";