Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(semantic): add change_parent_id API in ScopeTree #6807

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading