fix(semantic): incorrect symbol’s scope_id after var hoisting (#4458)

Bug found in https://github.com/rolldown/rolldown/pull/1726
This commit is contained in:
Dunqing 2024-07-25 21:12:26 +08:00 committed by GitHub
parent e1ca4122d8
commit e4ca06ae8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View file

@ -71,6 +71,7 @@ impl<'a> Binder<'a> for VariableDeclarator<'a> {
// avoid same symbols appear in multi-scopes
builder.scope.remove_binding(*scope_id, &name);
builder.scope.add_binding(target_scope_id, name, symbol_id);
builder.symbols.scope_ids[symbol_id] = target_scope_id;
break;
}
}

View file

@ -138,3 +138,18 @@ fn test_catch_clause_parameters() {
.has_number_of_references(1)
.test();
}
#[test]
fn var_hoisting() {
SemanticTester::js(
"
try {} catch (e) {
var e = 0;
}
",
)
.has_root_symbol("e")
// `e` was hoisted to the top scope so the symbol's scope is also the top scope
.is_in_scope(ScopeFlags::Top)
.test();
}