Skip to content

Commit

Permalink
fix(traverse): insert_scope_below update child scopes records
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 2, 2024
1 parent 2ccbd93 commit e960207
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl ScopeTree {
pub fn set_parent_id(&mut self, scope_id: ScopeId, parent_id: Option<ScopeId>) {
self.parent_ids[scope_id] = parent_id;
if self.build_child_ids {
// Set this scope as child of parent scope
if let Some(parent_id) = parent_id {
self.child_ids[parent_id].push(scope_id);
}
Expand Down Expand Up @@ -247,12 +248,24 @@ impl ScopeTree {
&mut self.bindings[scope_id]
}

/// Return whether this `ScopeTree` has child IDs recorded
#[inline]
pub fn has_child_ids(&self) -> bool {
self.build_child_ids
}

/// Get the child scopes of a scope
#[inline]
pub fn get_child_ids(&self, scope_id: ScopeId) -> &[ScopeId] {
&self.child_ids[scope_id]
}

/// Get a mutable reference to a scope's children
#[inline]
pub fn get_child_ids_mut(&mut self, scope_id: ScopeId) -> &mut Vec<ScopeId> {
&mut self.child_ids[scope_id]
}

/// Create a scope.
#[inline]
pub fn add_scope(
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ impl TraverseScoping {
}

fn insert_scope_below(&mut self, child_scope_ids: &[ScopeId], flags: ScopeFlags) -> ScopeId {
// Remove these scopes from parent's children
if self.scopes.has_child_ids() {
let current_child_scope_ids = self.scopes.get_child_ids_mut(self.current_scope_id);
current_child_scope_ids.retain(|scope_id| !child_scope_ids.contains(scope_id));
}

// Create new scope as child of parent
let new_scope_id = self.create_child_scope_of_current(flags);

Expand Down

0 comments on commit e960207

Please sign in to comment.