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(traverse): add methods for deleting references #5559

Merged
Merged
Show file tree
Hide file tree
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
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