mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(semantic): add change_parent_id API in ScopeTree (#6807)
Part of #6658 This API is useful when we want to move a scope to another scope
This commit is contained in:
parent
088c1b6460
commit
e7e60dab68
1 changed files with 18 additions and 1 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::hash::BuildHasherDefault;
|
||||
use std::{hash::BuildHasherDefault, mem};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use rustc_hash::{FxHashMap, FxHasher};
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue