Skip to content

Commit

Permalink
refactor(semantic): ScopeTree::rename_binding remove old binding first
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 19, 2024
1 parent 02f968d commit 7337ae4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
10 changes: 2 additions & 8 deletions crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,6 @@ impl ScopeTree {

/// Rename a binding to a new name.
///
/// Preserves order of bindings.
///
/// The following must be true for successful operation:
/// * Binding exists in specified scope for `old_name`.
/// * Existing binding is for specified `symbol_id`.
Expand All @@ -356,14 +354,10 @@ impl ScopeTree {
new_name: CompactStr,
) {
let bindings = &mut self.bindings[scope_id];
// Insert on end
let existing_symbol_id = bindings.insert(new_name, symbol_id);
debug_assert!(existing_symbol_id.is_none());
// Remove old entry. `swap_remove` swaps the last entry into the place of removed entry.
// We just inserted the new entry as last, so the new entry takes the place of the old.
// Order of entries is same as before, only the key has changed.
let old_symbol_id = bindings.remove(old_name);
debug_assert_eq!(old_symbol_id, Some(symbol_id));
let existing_symbol_id = bindings.insert(new_name, symbol_id);
debug_assert!(existing_symbol_id.is_none());
}

/// Reserve memory for an `additional` number of scopes.
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,6 @@ impl TraverseScoping {

/// Rename symbol.
///
/// Preserves original order of bindings for scope.
///
/// The following must be true for successful operation:
/// * Binding exists in specified scope for `symbol_id`.
/// * No binding already exists in scope for `new_name`.
Expand Down

0 comments on commit 7337ae4

Please sign in to comment.