diff --git a/crates/oxc_ast/src/visit.rs b/crates/oxc_ast/src/visit.rs index a592eca10..7db7e9673 100644 --- a/crates/oxc_ast/src/visit.rs +++ b/crates/oxc_ast/src/visit.rs @@ -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>) { diff --git a/crates/oxc_ast/src/visit_mut.rs b/crates/oxc_ast/src/visit_mut.rs index fa41384f4..2dfaf94cc 100644 --- a/crates/oxc_ast/src/visit_mut.rs +++ b/crates/oxc_ast/src/visit_mut.rs @@ -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>) { diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 0b0e88824..c96a40d0c 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -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>) { diff --git a/crates/oxc_semantic/tests/scopes.rs b/crates/oxc_semantic/tests/scopes.rs index dea32f60c..d05abe66a 100644 --- a/crates/oxc_semantic/tests/scopes.rs +++ b/crates/oxc_semantic/tests/scopes.rs @@ -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(); +}