Skip to content

Commit bbe5ded

Browse files
committed
refactor(semantic): set current_scope_id to scope_id in enter_scope (#4193)
close: #4170
1 parent 7f1addd commit bbe5ded

2 files changed

Lines changed: 34 additions & 122 deletions

File tree

crates/oxc_semantic/src/builder.rs

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl<'a> SemanticBuilder<'a> {
421421
}
422422

423423
impl<'a> Visit<'a> for SemanticBuilder<'a> {
424-
fn enter_scope(&mut self, flags: ScopeFlags, _: &Cell<Option<ScopeId>>) {
424+
fn enter_scope(&mut self, flags: ScopeFlags, scope_id: &Cell<Option<ScopeId>>) {
425425
let parent_scope_id =
426426
if flags.contains(ScopeFlags::Top) { None } else { Some(self.current_scope_id) };
427427

@@ -431,6 +431,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
431431
}
432432

433433
self.current_scope_id = self.scope.add_scope(parent_scope_id, flags);
434+
scope_id.set(Some(self.current_scope_id));
434435
self.unresolved_references.push(UnresolvedReferences::default());
435436
}
436437

@@ -470,7 +471,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
470471
},
471472
&program.scope_id,
472473
);
473-
program.scope_id.set(Some(self.current_scope_id));
474474

475475
/* cfg */
476476
let error_harness = control_flow!(|self, cfg| {
@@ -496,18 +496,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
496496
self.leave_scope();
497497
}
498498

499-
fn visit_block_statement(&mut self, stmt: &BlockStatement<'a>) {
500-
let kind = AstKind::BlockStatement(self.alloc(stmt));
501-
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
502-
stmt.scope_id.set(Some(self.current_scope_id));
503-
self.enter_node(kind);
504-
505-
self.visit_statements(&stmt.body);
506-
507-
self.leave_node(kind);
508-
self.leave_scope();
509-
}
510-
511499
fn visit_break_statement(&mut self, stmt: &BreakStatement<'a>) {
512500
let kind = AstKind::BreakStatement(self.alloc(stmt));
513501
self.enter_node(kind);
@@ -549,12 +537,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
549537
self.leave_node(kind);
550538
}
551539

552-
fn visit_debugger_statement(&mut self, stmt: &DebuggerStatement) {
553-
let kind = AstKind::DebuggerStatement(self.alloc(stmt));
554-
self.enter_node(kind);
555-
self.leave_node(kind);
556-
}
557-
558540
fn visit_do_while_statement(&mut self, stmt: &DoWhileStatement<'a>) {
559541
let kind = AstKind::DoWhileStatement(self.alloc(stmt));
560542
self.enter_node(kind);
@@ -608,15 +590,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
608590
self.leave_node(kind);
609591
}
610592

611-
fn visit_expression_statement(&mut self, stmt: &ExpressionStatement<'a>) {
612-
let kind = AstKind::ExpressionStatement(self.alloc(stmt));
613-
self.enter_node(kind);
614-
615-
self.visit_expression(&stmt.expression);
616-
617-
self.leave_node(kind);
618-
}
619-
620593
fn visit_logical_expression(&mut self, expr: &LogicalExpression<'a>) {
621594
// logical expressions are short-circuiting, and therefore
622595
// also represent control flow.
@@ -767,7 +740,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
767740
stmt.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration);
768741
if is_lexical_declaration {
769742
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
770-
stmt.scope_id.set(Some(self.current_scope_id));
771743
}
772744
self.enter_node(kind);
773745
if let Some(init) = &stmt.init {
@@ -833,27 +805,11 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
833805
}
834806
}
835807

836-
fn visit_for_statement_init(&mut self, init: &ForStatementInit<'a>) {
837-
let kind = AstKind::ForStatementInit(self.alloc(init));
838-
self.enter_node(kind);
839-
match init {
840-
ForStatementInit::UsingDeclaration(decl) => {
841-
self.visit_using_declaration(decl);
842-
}
843-
ForStatementInit::VariableDeclaration(decl) => {
844-
self.visit_variable_declaration(decl);
845-
}
846-
match_expression!(ForStatementInit) => self.visit_expression(init.to_expression()),
847-
}
848-
self.leave_node(kind);
849-
}
850-
851808
fn visit_for_in_statement(&mut self, stmt: &ForInStatement<'a>) {
852809
let kind = AstKind::ForInStatement(self.alloc(stmt));
853810
let is_lexical_declaration = stmt.left.is_lexical_declaration();
854811
if is_lexical_declaration {
855812
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
856-
stmt.scope_id.set(Some(self.current_scope_id));
857813
}
858814
self.enter_node(kind);
859815

@@ -918,7 +874,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
918874
let is_lexical_declaration = stmt.left.is_lexical_declaration();
919875
if is_lexical_declaration {
920876
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
921-
stmt.scope_id.set(Some(self.current_scope_id));
922877
}
923878
self.enter_node(kind);
924879

@@ -1104,7 +1059,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
11041059
self.enter_node(kind);
11051060
self.visit_expression(&stmt.discriminant);
11061061
self.enter_scope(ScopeFlags::empty(), &stmt.scope_id);
1107-
stmt.scope_id.set(Some(self.current_scope_id));
11081062

11091063
/* cfg */
11101064
let discriminant_graph_ix = control_flow!(|self, cfg| {
@@ -1348,16 +1302,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
13481302
self.leave_node(kind);
13491303
}
13501304

1351-
fn visit_finally_clause(&mut self, clause: &BlockStatement<'a>) {
1352-
let kind = AstKind::FinallyClause(self.alloc(clause));
1353-
self.enter_scope(ScopeFlags::empty(), &clause.scope_id);
1354-
clause.scope_id.set(Some(self.current_scope_id));
1355-
self.enter_node(kind);
1356-
self.visit_statements(&clause.body);
1357-
self.leave_node(kind);
1358-
self.leave_scope();
1359-
}
1360-
13611305
fn visit_while_statement(&mut self, stmt: &WhileStatement<'a>) {
13621306
let kind = AstKind::WhileStatement(self.alloc(stmt));
13631307
self.enter_node(kind);
@@ -1445,7 +1389,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
14451389
},
14461390
&func.scope_id,
14471391
);
1448-
func.scope_id.set(Some(self.current_scope_id));
14491392

14501393
/* cfg */
14511394
let (before_function_graph_ix, error_harness, function_graph_ix) =
@@ -1514,7 +1457,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
15141457
// Class expressions create a temporary scope with the class name as its only variable
15151458
// E.g., `let c = class A { foo() { console.log(A) } }`
15161459
self.enter_scope(ScopeFlags::empty(), &class.scope_id);
1517-
class.scope_id.set(Some(self.current_scope_id));
15181460
}
15191461

15201462
self.enter_node(kind);
@@ -1540,20 +1482,9 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
15401482
}
15411483
}
15421484

1543-
fn visit_static_block(&mut self, block: &StaticBlock<'a>) {
1544-
let kind = AstKind::StaticBlock(self.alloc(block));
1545-
self.enter_scope(ScopeFlags::ClassStaticBlock, &block.scope_id);
1546-
block.scope_id.set(Some(self.current_scope_id));
1547-
self.enter_node(kind);
1548-
self.visit_statements(&block.body);
1549-
self.leave_node(kind);
1550-
self.leave_scope();
1551-
}
1552-
15531485
fn visit_arrow_function_expression(&mut self, expr: &ArrowFunctionExpression<'a>) {
15541486
let kind = AstKind::ArrowFunctionExpression(self.alloc(expr));
15551487
self.enter_scope(ScopeFlags::Function | ScopeFlags::Arrow, &expr.scope_id);
1556-
expr.scope_id.set(Some(self.current_scope_id));
15571488

15581489
/* cfg */
15591490
let (current_node_ix, error_harness, function_graph_ix) = control_flow!(|self, cfg| {
@@ -1597,57 +1528,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
15971528
self.leave_node(kind);
15981529
self.leave_scope();
15991530
}
1600-
1601-
fn visit_ts_enum_declaration(&mut self, decl: &TSEnumDeclaration<'a>) {
1602-
let kind = AstKind::TSEnumDeclaration(self.alloc(decl));
1603-
self.enter_node(kind);
1604-
self.visit_binding_identifier(&decl.id);
1605-
self.enter_scope(ScopeFlags::empty(), &decl.scope_id);
1606-
decl.scope_id.set(Some(self.current_scope_id));
1607-
for member in &decl.members {
1608-
self.visit_ts_enum_member(member);
1609-
}
1610-
self.leave_scope();
1611-
self.leave_node(kind);
1612-
}
1613-
1614-
fn visit_ts_module_declaration(&mut self, decl: &TSModuleDeclaration<'a>) {
1615-
let kind = AstKind::TSModuleDeclaration(self.alloc(decl));
1616-
self.enter_node(kind);
1617-
match &decl.id {
1618-
TSModuleDeclarationName::Identifier(ident) => self.visit_identifier_name(ident),
1619-
TSModuleDeclarationName::StringLiteral(lit) => self.visit_string_literal(lit),
1620-
}
1621-
self.enter_scope(ScopeFlags::TsModuleBlock, &decl.scope_id);
1622-
decl.scope_id.set(Some(self.current_scope_id));
1623-
match &decl.body {
1624-
Some(TSModuleDeclarationBody::TSModuleDeclaration(decl)) => {
1625-
self.visit_ts_module_declaration(decl);
1626-
}
1627-
Some(TSModuleDeclarationBody::TSModuleBlock(block)) => {
1628-
self.visit_ts_module_block(block);
1629-
}
1630-
None => {}
1631-
}
1632-
self.leave_scope();
1633-
self.leave_node(kind);
1634-
}
1635-
1636-
fn visit_ts_type_parameter(&mut self, ty: &TSTypeParameter<'a>) {
1637-
let kind = AstKind::TSTypeParameter(self.alloc(ty));
1638-
self.enter_scope(ScopeFlags::empty(), &ty.scope_id);
1639-
ty.scope_id.set(Some(self.current_scope_id));
1640-
self.enter_node(kind);
1641-
if let Some(constraint) = &ty.constraint {
1642-
self.visit_ts_type(constraint);
1643-
}
1644-
1645-
if let Some(default) = &ty.default {
1646-
self.visit_ts_type(default);
1647-
}
1648-
self.leave_node(kind);
1649-
self.leave_scope();
1650-
}
16511531
}
16521532

16531533
impl<'a> SemanticBuilder<'a> {

tasks/coverage/parser_typescript.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9415,6 +9415,22 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
94159415
11 │ function baz<implements, protected>() { }
94169416
╰────
94179417

9418+
× The keyword 'implements' is reserved
9419+
╭─[compiler/strictModeReservedWord.ts:11:18]
9420+
10 │ function bar(private, implements, let) { }
9421+
11 │ function baz<implements, protected>() { }
9422+
· ──────────
9423+
12 │ function barn(cb: (private, public, package) => void) { }
9424+
╰────
9425+
9426+
× The keyword 'protected' is reserved
9427+
╭─[compiler/strictModeReservedWord.ts:11:30]
9428+
10 │ function bar(private, implements, let) { }
9429+
11 │ function baz<implements, protected>() { }
9430+
· ─────────
9431+
12 │ function barn(cb: (private, public, package) => void) { }
9432+
╰────
9433+
94189434
× The keyword 'private' is reserved
94199435
╭─[compiler/strictModeReservedWord.ts:12:24]
94209436
11 │ function baz<implements, protected>() { }
@@ -9703,6 +9719,22 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
97039719
16 │ }
97049720
╰────
97059721

9722+
× The keyword 'public' is reserved
9723+
╭─[compiler/strictModeReservedWordInClassDeclaration.ts:21:9]
9724+
20 │
9725+
21 │ class D<public, private>{ }
9726+
· ──────
9727+
22 │
9728+
╰────
9729+
9730+
× The keyword 'private' is reserved
9731+
╭─[compiler/strictModeReservedWordInClassDeclaration.ts:21:17]
9732+
20 │
9733+
21 │ class D<public, private>{ }
9734+
· ───────
9735+
22 │
9736+
╰────
9737+
97069738
× The keyword 'package' is reserved
97079739
╭─[compiler/strictModeReservedWordInClassDeclaration.ts:27:17]
97089740
26 │ class F1 implements public.private.implements { }

0 commit comments

Comments
 (0)