Skip to content

Commit

Permalink
feat(traverse): add methods for deleting references
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 6, 2024
1 parent 875c498 commit 7e97a52
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,22 @@ impl<'a> TraverseCtx<'a> {
self.scoping.create_reference_in_current_scope(name, flags)
}

/// Delete a reference.
///
/// Provided `name` must match `reference_id`.
///
/// This is a shortcut for `ctx.scoping.delete_reference`.
pub fn delete_reference(&mut self, reference_id: ReferenceId, name: &str) {
self.scoping.delete_reference(reference_id, name);
}

/// Delete reference for an `IdentifierReference`.
///
/// This is a shortcut for `ctx.scoping.delete_reference_for_identifier`.
pub fn delete_reference_for_identifier(&mut self, ident: &IdentifierReference) {
self.scoping.delete_reference_for_identifier(ident);
}

/// Clone `IdentifierReference` based on the original reference's `SymbolId` and name.
///
/// This method makes a lookup of the `SymbolId` for the reference. If you need to create multiple
Expand Down
19 changes: 19 additions & 0 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,25 @@ impl TraverseScoping {
self.create_reference(name, symbol_id, flags)
}

/// Delete a reference.
///
/// Provided `name` must match `reference_id`.
pub fn delete_reference(&mut self, reference_id: ReferenceId, name: &str) {
let symbol_id = self.symbols.get_reference(reference_id).symbol_id();
if let Some(symbol_id) = symbol_id {
self.symbols.delete_resolved_reference(symbol_id, reference_id);
} else {
self.scopes.delete_root_unresolved_reference(name, reference_id);
}
}

/// Delete reference for an `IdentifierReference`.
#[allow(clippy::missing_panics_doc)]
pub fn delete_reference_for_identifier(&mut self, ident: &IdentifierReference) {
// `unwrap` should never panic as `IdentifierReference`s should always have a `ReferenceId`
self.delete_reference(ident.reference_id().unwrap(), &ident.name);
}

/// Clone `IdentifierReference` based on the original reference's `SymbolId` and name.
///
/// This method makes a lookup of the `SymbolId` for the reference. If you need to create multiple
Expand Down

0 comments on commit 7e97a52

Please sign in to comment.