From 5928f043b56bff29340364dabf4ec020e62666e6 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:36:30 +0000 Subject: [PATCH] feat(semantic): add `move_binding` API in ` ScopeTree` (#6808) Part of #6658 This API is useful when we want to move a scope to another scope --- crates/oxc_semantic/src/scope.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/oxc_semantic/src/scope.rs b/crates/oxc_semantic/src/scope.rs index b21edcde1662d..03103b2e57f83 100644 --- a/crates/oxc_semantic/src/scope.rs +++ b/crates/oxc_semantic/src/scope.rs @@ -328,6 +328,14 @@ impl ScopeTree { self.bindings[scope_id].shift_remove(name); } + /// Move a binding from one scope to another. + pub fn move_binding(&mut self, from: ScopeId, to: ScopeId, name: &str) { + let from_map = &mut self.bindings[from]; + if let Some((name, symbol_id)) = from_map.swap_remove_entry(name) { + self.bindings[to].insert(name, symbol_id); + } + } + /// Reserve memory for an `additional` number of scopes. pub fn reserve(&mut self, additional: usize) { self.parent_ids.reserve(additional);