Skip to content

Commit

Permalink
refactor(traverse): TraverseCtx::generate_binding take an Atom (#…
Browse files Browse the repository at this point in the history
…6830)

Follow-up after #6805.

`TraverseCtx::generate_binding` take an `Atom` instead of a `CompactStr`. If it's an existing var name (which it must be, otherwise we'd be using `TraverseCtx::generate_uid`), an `Atom` will already exist in the arena for this symbol name, so we'd be better off reusing it, rather than writing the same `Atom` into the arena again.
  • Loading branch information
overlookmotel committed Oct 24, 2024
1 parent 8f392e8 commit 60f487a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/typescript/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'a> TypeScriptEnum<'a> {
let enum_name = decl.id.name.clone();
let func_scope_id = decl.scope_id.get().unwrap();
let param_ident = ctx.generate_binding(
enum_name.to_compact_str(),
enum_name.clone(),
func_scope_id,
SymbolFlags::FunctionScopedVariable,
);
Expand Down
28 changes: 20 additions & 8 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,31 @@ impl<'a> TraverseCtx<'a> {
/// Creates a symbol with the provided name and flags and adds it to the specified scope.
pub fn generate_binding(
&mut self,
name: CompactStr,
name: Atom<'a>,
scope_id: ScopeId,
flags: SymbolFlags,
) -> BoundIdentifier<'a> {
let name_atom = self.ast.atom(&name);
let owned_name = name.to_compact_str();

// Add binding to scope
let symbol_id =
self.symbols_mut().create_symbol(SPAN, name.clone(), flags, scope_id, NodeId::DUMMY);
self.scopes_mut().add_binding(scope_id, name, symbol_id);
let symbol_id = self.symbols_mut().create_symbol(
SPAN,
owned_name.clone(),
flags,
scope_id,
NodeId::DUMMY,
);
self.scopes_mut().add_binding(scope_id, owned_name, symbol_id);

BoundIdentifier::new(name_atom, symbol_id)
BoundIdentifier::new(name, symbol_id)
}

/// Generate binding in current scope.
///
/// Creates a symbol with the provided name and flags and adds it to the current scope.
pub fn generate_in_current_scope(
&mut self,
name: CompactStr,
name: Atom<'a>,
flags: SymbolFlags,
) -> BoundIdentifier<'a> {
self.generate_binding(name, self.current_scope_id(), flags)
Expand Down Expand Up @@ -356,7 +361,14 @@ impl<'a> TraverseCtx<'a> {
) -> BoundIdentifier<'a> {
// Get name for UID
let name = self.generate_uid_name(name);
self.generate_binding(name, scope_id, flags)
let name_atom = self.ast.atom(&name);

// Add binding to scope
let symbol_id =
self.symbols_mut().create_symbol(SPAN, name.clone(), flags, scope_id, NodeId::DUMMY);
self.scopes_mut().add_binding(scope_id, name, symbol_id);

BoundIdentifier::new(name_atom, symbol_id)
}

/// Generate UID in current scope.
Expand Down

0 comments on commit 60f487a

Please sign in to comment.