Skip to content

Commit c418bf5

Browse files
committed
refactor(semantic): directly record current_node_id when adding a scope (#4265)
close: #4234
1 parent f85188b commit c418bf5

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

crates/oxc_semantic/src/builder.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'a> SemanticBuilder<'a> {
189189
/// # Panics
190190
pub fn build(mut self, program: &Program<'a>) -> SemanticBuilderReturn<'a> {
191191
if self.source_type.is_typescript_definition() {
192-
let scope_id = self.scope.add_scope(None, ScopeFlags::Top);
192+
let scope_id = self.scope.add_scope(None, AstNodeId::dummy(), ScopeFlags::Top);
193193
program.scope_id.set(Some(scope_id));
194194
} else {
195195
self.visit_program(program);
@@ -449,7 +449,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
449449
flags = self.scope.get_new_scope_flags(flags, parent_scope_id);
450450
}
451451

452-
self.current_scope_id = self.scope.add_scope(parent_scope_id, flags);
452+
self.current_scope_id = self.scope.add_scope(parent_scope_id, self.current_node_id, flags);
453453
scope_id.set(Some(self.current_scope_id));
454454

455455
if let Some(parent_scope_id) = parent_scope_id {
@@ -471,8 +471,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
471471
if !flags.is_top() {
472472
self.bind_function_or_class_expression();
473473
}
474-
475-
self.add_current_node_id_to_current_scope();
476474
}
477475

478476
fn leave_scope(&mut self) {
@@ -501,6 +499,15 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
501499

502500
fn visit_program(&mut self, program: &Program<'a>) {
503501
let kind = AstKind::Program(self.alloc(program));
502+
/* cfg */
503+
let error_harness = control_flow!(|self, cfg| {
504+
let error_harness = cfg.attach_error_harness(ErrorEdgeKind::Implicit);
505+
let _program_basic_block = cfg.new_basic_block_normal();
506+
error_harness
507+
});
508+
/* cfg - must be above directives as directives are in cfg */
509+
510+
self.enter_node(kind);
504511
self.enter_scope(
505512
{
506513
let mut flags = ScopeFlags::Top;
@@ -512,16 +519,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
512519
&program.scope_id,
513520
);
514521

515-
/* cfg */
516-
let error_harness = control_flow!(|self, cfg| {
517-
let error_harness = cfg.attach_error_harness(ErrorEdgeKind::Implicit);
518-
let _program_basic_block = cfg.new_basic_block_normal();
519-
error_harness
520-
});
521-
/* cfg - must be above directives as directives are in cfg */
522-
523-
self.enter_node(kind);
524-
525522
for directive in &program.directives {
526523
self.visit_directive(directive);
527524
}
@@ -1778,10 +1775,6 @@ impl<'a> SemanticBuilder<'a> {
17781775
}
17791776
}
17801777

1781-
fn add_current_node_id_to_current_scope(&mut self) {
1782-
self.scope.set_node_id(self.current_scope_id, self.current_node_id);
1783-
}
1784-
17851778
fn make_all_namespaces_valuelike(&mut self) {
17861779
for symbol_id in &self.namespace_stack {
17871780
// Ambient modules cannot be value modules

crates/oxc_semantic/src/scope.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,17 @@ impl ScopeTree {
179179
&mut self.bindings[scope_id]
180180
}
181181

182-
pub fn add_scope(&mut self, parent_id: Option<ScopeId>, flags: ScopeFlags) -> ScopeId {
182+
pub fn add_scope(
183+
&mut self,
184+
parent_id: Option<ScopeId>,
185+
node_id: AstNodeId,
186+
flags: ScopeFlags,
187+
) -> ScopeId {
183188
let scope_id = self.parent_ids.push(parent_id);
184189
_ = self.child_ids.push(vec![]);
185190
_ = self.flags.push(flags);
186191
_ = self.bindings.push(Bindings::default());
187-
_ = self.node_ids.push(AstNodeId::dummy());
192+
_ = self.node_ids.push(node_id);
188193

189194
// Set this scope as child of parent scope.
190195
if let Some(parent_id) = parent_id {
@@ -194,10 +199,6 @@ impl ScopeTree {
194199
scope_id
195200
}
196201

197-
pub(crate) fn set_node_id(&mut self, scope_id: ScopeId, node_id: AstNodeId) {
198-
self.node_ids[scope_id] = node_id;
199-
}
200-
201202
pub fn add_binding(&mut self, scope_id: ScopeId, name: CompactStr, symbol_id: SymbolId) {
202203
self.bindings[scope_id].insert(name, symbol_id);
203204
}

crates/oxc_traverse/src/context/scoping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl TraverseScoping {
121121
/// `flags` provided are amended to inherit from parent scope's flags.
122122
pub fn create_scope_child_of_current(&mut self, flags: ScopeFlags) -> ScopeId {
123123
let flags = self.scopes.get_new_scope_flags(flags, self.current_scope_id);
124-
self.scopes.add_scope(Some(self.current_scope_id), flags)
124+
self.scopes.add_scope(Some(self.current_scope_id), AstNodeId::dummy(), flags)
125125
}
126126

127127
/// Insert a scope into scope tree below a statement.

0 commit comments

Comments
 (0)