Skip to content
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
15 changes: 15 additions & 0 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,21 @@ impl<'a> TraverseCtx<'a> {
) -> ReferenceId {
self.scoping.create_reference_in_current_scope(name, flag)
}

/// 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
/// `IdentifierReference`s for the same binding, it is better to look up the `SymbolId` only once,
/// and generate `IdentifierReference`s with `TraverseCtx::create_reference_id`.
///
/// This is a shortcut for `ctx.scoping.clone_identifier_reference`.
pub fn clone_identifier_reference(
&mut self,
ident: &IdentifierReference<'a>,
flag: ReferenceFlag,
) -> IdentifierReference<'a> {
self.scoping.clone_identifier_reference(ident, flag)
}
}

// Methods used internally within crate
Expand Down
18 changes: 18 additions & 0 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,24 @@ impl TraverseScoping {
let symbol_id = self.scopes.find_binding(self.current_scope_id, name.as_str());
self.create_reference(name, symbol_id, flag)
}

/// 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
/// `IdentifierReference`s for the same binding, it is better to look up the `SymbolId` only once,
/// and generate `IdentifierReference`s with `TraverseScoping::create_reference_id`.
pub fn clone_identifier_reference<'a>(
&mut self,
ident: &IdentifierReference<'a>,
flag: ReferenceFlag,
) -> IdentifierReference<'a> {
let reference =
self.symbols().get_reference(ident.reference_id.get().unwrap_or_else(|| {
unreachable!("IdentifierReference must have a reference_id");
}));
let symbol_id = reference.symbol_id();
self.create_reference_id(ident.span, ident.name.clone(), symbol_id, flag)
}
}

// Methods used internally within crate
Expand Down