From 60f487a203d0cdd0b20b1544695cda4ec40419f5 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 24 Oct 2024 02:47:07 +0000 Subject: [PATCH] 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. --- crates/oxc_transformer/src/typescript/enum.rs | 2 +- crates/oxc_traverse/src/context/mod.rs | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/crates/oxc_transformer/src/typescript/enum.rs b/crates/oxc_transformer/src/typescript/enum.rs index c4bddb10c3875..291d58f49a61b 100644 --- a/crates/oxc_transformer/src/typescript/enum.rs +++ b/crates/oxc_transformer/src/typescript/enum.rs @@ -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, ); diff --git a/crates/oxc_traverse/src/context/mod.rs b/crates/oxc_traverse/src/context/mod.rs index 63fd5b548a9d9..3ef056d70cade 100644 --- a/crates/oxc_traverse/src/context/mod.rs +++ b/crates/oxc_traverse/src/context/mod.rs @@ -306,18 +306,23 @@ 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. @@ -325,7 +330,7 @@ impl<'a> TraverseCtx<'a> { /// 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) @@ -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.