fix(semantic): incorrect scope for switch statement (#2513)

close: #2501
This commit is contained in:
Dunqing 2024-02-26 22:54:19 +08:00 committed by GitHub
parent be6b8b7ce6
commit 1519b9000b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 6 deletions

View file

@ -248,14 +248,14 @@ pub trait Visit<'a>: Sized {
fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
let kind = AstKind::SwitchStatement(self.alloc(stmt));
self.enter_scope(ScopeFlags::empty());
self.enter_node(kind);
self.visit_expression(&stmt.discriminant);
self.enter_scope(ScopeFlags::empty());
for case in &stmt.cases {
self.visit_switch_case(case);
}
self.leave_node(kind);
self.leave_scope();
self.leave_node(kind);
}
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {

View file

@ -245,14 +245,14 @@ pub trait VisitMut<'a>: Sized {
fn visit_switch_statement(&mut self, stmt: &mut SwitchStatement<'a>) {
let kind = AstKind::SwitchStatement(self.alloc(stmt));
self.enter_scope(ScopeFlags::empty());
self.enter_node(kind);
self.visit_expression(&mut stmt.discriminant);
self.enter_scope(ScopeFlags::empty());
for case in stmt.cases.iter_mut() {
self.visit_switch_case(case);
}
self.leave_node(kind);
self.leave_scope();
self.leave_node(kind);
}
fn visit_switch_case(&mut self, case: &mut SwitchCase<'a>) {

View file

@ -1105,9 +1105,9 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
let kind = AstKind::SwitchStatement(self.alloc(stmt));
self.enter_scope(ScopeFlags::empty());
self.enter_node(kind);
self.visit_expression(&stmt.discriminant);
self.enter_scope(ScopeFlags::empty());
/* cfg */
let discriminant_graph_ix = self.cfg.current_node_ix;
@ -1170,8 +1170,8 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
);
/* cfg */
self.leave_node(kind);
self.leave_scope();
self.leave_node(kind);
}
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {

View file

@ -90,3 +90,19 @@ fn test_function_level_strict() {
.test();
tester.has_some_symbol("foo").is_not_in_scope(ScopeFlags::StrictMode).test();
}
#[test]
fn test_switch_case() {
SemanticTester::js(
"
const foo = 1;
switch (foo) {
case 1:
const foo = 2;
}
",
)
.has_root_symbol("foo")
.has_number_of_references(1)
.test();
}