Skip to content

Commit

Permalink
feat(semantic): add change_parent_id API in ScopeTree (#6807)
Browse files Browse the repository at this point in the history
Part of #6658

This API is useful when we want to move a scope to another scope
  • Loading branch information
Dunqing committed Oct 23, 2024
1 parent 088c1b6 commit e7e60da
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::hash::BuildHasherDefault;
use std::{hash::BuildHasherDefault, mem};

use indexmap::IndexMap;
use rustc_hash::{FxHashMap, FxHasher};
Expand Down Expand Up @@ -173,6 +173,23 @@ impl ScopeTree {
}
}

/// Change the parent scope of a scope.
///
/// This will also remove the scope from the child list of the old parent and add it to the new parent.
pub fn change_parent_id(&mut self, scope_id: ScopeId, new_parent_id: Option<ScopeId>) {
let old_parent_id = mem::replace(&mut self.parent_ids[scope_id], new_parent_id);
if self.build_child_ids {
// Remove this scope from old parent scope
if let Some(old_parent_id) = old_parent_id {
self.child_ids[old_parent_id].retain(|&child_id| child_id != scope_id);
}
// And add it to new parent scope
if let Some(parent_id) = new_parent_id {
self.child_ids[parent_id].push(scope_id);
}
}
}

/// Delete a scope.
pub fn delete_scope(&mut self, scope_id: ScopeId) {
if self.build_child_ids {
Expand Down

0 comments on commit e7e60da

Please sign in to comment.