mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
fix(semantic): incorrect scope for switch statement (#2513)
close: #2501
This commit is contained in:
parent
be6b8b7ce6
commit
1519b9000b
4 changed files with 22 additions and 6 deletions
|
|
@ -248,14 +248,14 @@ pub trait Visit<'a>: Sized {
|
||||||
|
|
||||||
fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
|
fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
|
||||||
let kind = AstKind::SwitchStatement(self.alloc(stmt));
|
let kind = AstKind::SwitchStatement(self.alloc(stmt));
|
||||||
self.enter_scope(ScopeFlags::empty());
|
|
||||||
self.enter_node(kind);
|
self.enter_node(kind);
|
||||||
self.visit_expression(&stmt.discriminant);
|
self.visit_expression(&stmt.discriminant);
|
||||||
|
self.enter_scope(ScopeFlags::empty());
|
||||||
for case in &stmt.cases {
|
for case in &stmt.cases {
|
||||||
self.visit_switch_case(case);
|
self.visit_switch_case(case);
|
||||||
}
|
}
|
||||||
self.leave_node(kind);
|
|
||||||
self.leave_scope();
|
self.leave_scope();
|
||||||
|
self.leave_node(kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {
|
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {
|
||||||
|
|
|
||||||
|
|
@ -245,14 +245,14 @@ pub trait VisitMut<'a>: Sized {
|
||||||
|
|
||||||
fn visit_switch_statement(&mut self, stmt: &mut SwitchStatement<'a>) {
|
fn visit_switch_statement(&mut self, stmt: &mut SwitchStatement<'a>) {
|
||||||
let kind = AstKind::SwitchStatement(self.alloc(stmt));
|
let kind = AstKind::SwitchStatement(self.alloc(stmt));
|
||||||
self.enter_scope(ScopeFlags::empty());
|
|
||||||
self.enter_node(kind);
|
self.enter_node(kind);
|
||||||
self.visit_expression(&mut stmt.discriminant);
|
self.visit_expression(&mut stmt.discriminant);
|
||||||
|
self.enter_scope(ScopeFlags::empty());
|
||||||
for case in stmt.cases.iter_mut() {
|
for case in stmt.cases.iter_mut() {
|
||||||
self.visit_switch_case(case);
|
self.visit_switch_case(case);
|
||||||
}
|
}
|
||||||
self.leave_node(kind);
|
|
||||||
self.leave_scope();
|
self.leave_scope();
|
||||||
|
self.leave_node(kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_switch_case(&mut self, case: &mut SwitchCase<'a>) {
|
fn visit_switch_case(&mut self, case: &mut SwitchCase<'a>) {
|
||||||
|
|
|
||||||
|
|
@ -1105,9 +1105,9 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
||||||
|
|
||||||
fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
|
fn visit_switch_statement(&mut self, stmt: &SwitchStatement<'a>) {
|
||||||
let kind = AstKind::SwitchStatement(self.alloc(stmt));
|
let kind = AstKind::SwitchStatement(self.alloc(stmt));
|
||||||
self.enter_scope(ScopeFlags::empty());
|
|
||||||
self.enter_node(kind);
|
self.enter_node(kind);
|
||||||
self.visit_expression(&stmt.discriminant);
|
self.visit_expression(&stmt.discriminant);
|
||||||
|
self.enter_scope(ScopeFlags::empty());
|
||||||
|
|
||||||
/* cfg */
|
/* cfg */
|
||||||
let discriminant_graph_ix = self.cfg.current_node_ix;
|
let discriminant_graph_ix = self.cfg.current_node_ix;
|
||||||
|
|
@ -1170,8 +1170,8 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
||||||
);
|
);
|
||||||
/* cfg */
|
/* cfg */
|
||||||
|
|
||||||
self.leave_node(kind);
|
|
||||||
self.leave_scope();
|
self.leave_scope();
|
||||||
|
self.leave_node(kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {
|
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {
|
||||||
|
|
|
||||||
|
|
@ -90,3 +90,19 @@ fn test_function_level_strict() {
|
||||||
.test();
|
.test();
|
||||||
tester.has_some_symbol("foo").is_not_in_scope(ScopeFlags::StrictMode).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();
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue