Skip to content

Commit

Permalink
feat(traverse): support clone_identifier_reference method in `Trave…
Browse files Browse the repository at this point in the history
…rseCtx` (#4880)

related: #4804

needs from: #4876

The `clone_identifier_reference` method is used to clone an `IdentifierReference` and create a `Reference` and insert it to `SymbolTable`'s `resolved_references`.

The reason we need this is because we need to make sure that `IdentifierReference`'s `reference_id` is unique
  • Loading branch information
Dunqing committed Aug 15, 2024
1 parent 47c9552 commit 72a37fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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

0 comments on commit 72a37fc

Please sign in to comment.