Skip to content

Commit 60f487a

Browse files
committed
refactor(traverse): TraverseCtx::generate_binding take an Atom (#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.
1 parent 8f392e8 commit 60f487a

File tree

2 files changed

+21
-9
lines changed
  • crates
    • oxc_transformer/src/typescript
    • oxc_traverse/src/context

2 files changed

+21
-9
lines changed

crates/oxc_transformer/src/typescript/enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a> TypeScriptEnum<'a> {
7777
let enum_name = decl.id.name.clone();
7878
let func_scope_id = decl.scope_id.get().unwrap();
7979
let param_ident = ctx.generate_binding(
80-
enum_name.to_compact_str(),
80+
enum_name.clone(),
8181
func_scope_id,
8282
SymbolFlags::FunctionScopedVariable,
8383
);

crates/oxc_traverse/src/context/mod.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,26 +306,31 @@ impl<'a> TraverseCtx<'a> {
306306
/// Creates a symbol with the provided name and flags and adds it to the specified scope.
307307
pub fn generate_binding(
308308
&mut self,
309-
name: CompactStr,
309+
name: Atom<'a>,
310310
scope_id: ScopeId,
311311
flags: SymbolFlags,
312312
) -> BoundIdentifier<'a> {
313-
let name_atom = self.ast.atom(&name);
313+
let owned_name = name.to_compact_str();
314314

315315
// Add binding to scope
316-
let symbol_id =
317-
self.symbols_mut().create_symbol(SPAN, name.clone(), flags, scope_id, NodeId::DUMMY);
318-
self.scopes_mut().add_binding(scope_id, name, symbol_id);
316+
let symbol_id = self.symbols_mut().create_symbol(
317+
SPAN,
318+
owned_name.clone(),
319+
flags,
320+
scope_id,
321+
NodeId::DUMMY,
322+
);
323+
self.scopes_mut().add_binding(scope_id, owned_name, symbol_id);
319324

320-
BoundIdentifier::new(name_atom, symbol_id)
325+
BoundIdentifier::new(name, symbol_id)
321326
}
322327

323328
/// Generate binding in current scope.
324329
///
325330
/// Creates a symbol with the provided name and flags and adds it to the current scope.
326331
pub fn generate_in_current_scope(
327332
&mut self,
328-
name: CompactStr,
333+
name: Atom<'a>,
329334
flags: SymbolFlags,
330335
) -> BoundIdentifier<'a> {
331336
self.generate_binding(name, self.current_scope_id(), flags)
@@ -356,7 +361,14 @@ impl<'a> TraverseCtx<'a> {
356361
) -> BoundIdentifier<'a> {
357362
// Get name for UID
358363
let name = self.generate_uid_name(name);
359-
self.generate_binding(name, scope_id, flags)
364+
let name_atom = self.ast.atom(&name);
365+
366+
// Add binding to scope
367+
let symbol_id =
368+
self.symbols_mut().create_symbol(SPAN, name.clone(), flags, scope_id, NodeId::DUMMY);
369+
self.scopes_mut().add_binding(scope_id, name, symbol_id);
370+
371+
BoundIdentifier::new(name_atom, symbol_id)
360372
}
361373

362374
/// Generate UID in current scope.

0 commit comments

Comments
 (0)