mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(semantic): directly record current_node_id when adding a scope (#4265)
close: #4234
This commit is contained in:
parent
f85188b1af
commit
c418bf53ce
3 changed files with 19 additions and 25 deletions
|
|
@ -189,7 +189,7 @@ impl<'a> SemanticBuilder<'a> {
|
|||
/// # Panics
|
||||
pub fn build(mut self, program: &Program<'a>) -> SemanticBuilderReturn<'a> {
|
||||
if self.source_type.is_typescript_definition() {
|
||||
let scope_id = self.scope.add_scope(None, ScopeFlags::Top);
|
||||
let scope_id = self.scope.add_scope(None, AstNodeId::dummy(), ScopeFlags::Top);
|
||||
program.scope_id.set(Some(scope_id));
|
||||
} else {
|
||||
self.visit_program(program);
|
||||
|
|
@ -449,7 +449,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
|||
flags = self.scope.get_new_scope_flags(flags, parent_scope_id);
|
||||
}
|
||||
|
||||
self.current_scope_id = self.scope.add_scope(parent_scope_id, flags);
|
||||
self.current_scope_id = self.scope.add_scope(parent_scope_id, self.current_node_id, flags);
|
||||
scope_id.set(Some(self.current_scope_id));
|
||||
|
||||
if let Some(parent_scope_id) = parent_scope_id {
|
||||
|
|
@ -471,8 +471,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
|||
if !flags.is_top() {
|
||||
self.bind_function_or_class_expression();
|
||||
}
|
||||
|
||||
self.add_current_node_id_to_current_scope();
|
||||
}
|
||||
|
||||
fn leave_scope(&mut self) {
|
||||
|
|
@ -501,6 +499,15 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
|||
|
||||
fn visit_program(&mut self, program: &Program<'a>) {
|
||||
let kind = AstKind::Program(self.alloc(program));
|
||||
/* cfg */
|
||||
let error_harness = control_flow!(|self, cfg| {
|
||||
let error_harness = cfg.attach_error_harness(ErrorEdgeKind::Implicit);
|
||||
let _program_basic_block = cfg.new_basic_block_normal();
|
||||
error_harness
|
||||
});
|
||||
/* cfg - must be above directives as directives are in cfg */
|
||||
|
||||
self.enter_node(kind);
|
||||
self.enter_scope(
|
||||
{
|
||||
let mut flags = ScopeFlags::Top;
|
||||
|
|
@ -512,16 +519,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
|||
&program.scope_id,
|
||||
);
|
||||
|
||||
/* cfg */
|
||||
let error_harness = control_flow!(|self, cfg| {
|
||||
let error_harness = cfg.attach_error_harness(ErrorEdgeKind::Implicit);
|
||||
let _program_basic_block = cfg.new_basic_block_normal();
|
||||
error_harness
|
||||
});
|
||||
/* cfg - must be above directives as directives are in cfg */
|
||||
|
||||
self.enter_node(kind);
|
||||
|
||||
for directive in &program.directives {
|
||||
self.visit_directive(directive);
|
||||
}
|
||||
|
|
@ -1778,10 +1775,6 @@ impl<'a> SemanticBuilder<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn add_current_node_id_to_current_scope(&mut self) {
|
||||
self.scope.set_node_id(self.current_scope_id, self.current_node_id);
|
||||
}
|
||||
|
||||
fn make_all_namespaces_valuelike(&mut self) {
|
||||
for symbol_id in &self.namespace_stack {
|
||||
// Ambient modules cannot be value modules
|
||||
|
|
|
|||
|
|
@ -179,12 +179,17 @@ impl ScopeTree {
|
|||
&mut self.bindings[scope_id]
|
||||
}
|
||||
|
||||
pub fn add_scope(&mut self, parent_id: Option<ScopeId>, flags: ScopeFlags) -> ScopeId {
|
||||
pub fn add_scope(
|
||||
&mut self,
|
||||
parent_id: Option<ScopeId>,
|
||||
node_id: AstNodeId,
|
||||
flags: ScopeFlags,
|
||||
) -> ScopeId {
|
||||
let scope_id = self.parent_ids.push(parent_id);
|
||||
_ = self.child_ids.push(vec![]);
|
||||
_ = self.flags.push(flags);
|
||||
_ = self.bindings.push(Bindings::default());
|
||||
_ = self.node_ids.push(AstNodeId::dummy());
|
||||
_ = self.node_ids.push(node_id);
|
||||
|
||||
// Set this scope as child of parent scope.
|
||||
if let Some(parent_id) = parent_id {
|
||||
|
|
@ -194,10 +199,6 @@ impl ScopeTree {
|
|||
scope_id
|
||||
}
|
||||
|
||||
pub(crate) fn set_node_id(&mut self, scope_id: ScopeId, node_id: AstNodeId) {
|
||||
self.node_ids[scope_id] = node_id;
|
||||
}
|
||||
|
||||
pub fn add_binding(&mut self, scope_id: ScopeId, name: CompactStr, symbol_id: SymbolId) {
|
||||
self.bindings[scope_id].insert(name, symbol_id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ impl TraverseScoping {
|
|||
/// `flags` provided are amended to inherit from parent scope's flags.
|
||||
pub fn create_scope_child_of_current(&mut self, flags: ScopeFlags) -> ScopeId {
|
||||
let flags = self.scopes.get_new_scope_flags(flags, self.current_scope_id);
|
||||
self.scopes.add_scope(Some(self.current_scope_id), flags)
|
||||
self.scopes.add_scope(Some(self.current_scope_id), AstNodeId::dummy(), flags)
|
||||
}
|
||||
|
||||
/// Insert a scope into scope tree below a statement.
|
||||
|
|
|
|||
Loading…
Reference in a new issue