From 635e9187d3981891e3fb32b19af3b5bb12bbb700 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Wed, 18 Sep 2024 02:23:36 +0000 Subject: [PATCH] feat(traverse): `generate_uid_name` method (#5839) Rename `find_uid_name` to `generate_uid_name` and make it a public method. --- crates/oxc_traverse/src/context/mod.rs | 27 ++++++++++++ crates/oxc_traverse/src/context/scoping.rs | 49 ++++++++++++++-------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/crates/oxc_traverse/src/context/mod.rs b/crates/oxc_traverse/src/context/mod.rs index 5a961169fc0ff..55789487f4142 100644 --- a/crates/oxc_traverse/src/context/mod.rs +++ b/crates/oxc_traverse/src/context/mod.rs @@ -280,8 +280,23 @@ impl<'a> TraverseCtx<'a> { self.scoping.insert_scope_below_expression(expr, flags) } + /// Generate UID var name. + /// + /// Finds a unique variable name which does clash with any other variables used in the program. + /// + /// See [`TraverseScoping::generate_uid_name`] for important information on how UIDs are generated. + /// There are some potential "gotchas". + /// + /// This is a shortcut for `ctx.scoping.generate_uid_name`. + pub fn generate_uid_name(&mut self, name: &str) -> CompactStr { + self.scoping.generate_uid_name(name) + } + /// Generate UID. /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". + /// /// This is a shortcut for `ctx.scoping.generate_uid`. #[inline] pub fn generate_uid(&mut self, name: &str, scope_id: ScopeId, flags: SymbolFlags) -> SymbolId { @@ -290,6 +305,9 @@ impl<'a> TraverseCtx<'a> { /// Generate UID in current scope. /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". + /// /// This is a shortcut for `ctx.scoping.generate_uid_in_current_scope`. #[inline] pub fn generate_uid_in_current_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId { @@ -298,6 +316,9 @@ impl<'a> TraverseCtx<'a> { /// Generate UID in root scope. /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". + /// /// This is a shortcut for `ctx.scoping.generate_uid_in_root_scope`. #[inline] pub fn generate_uid_in_root_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId { @@ -306,6 +327,9 @@ impl<'a> TraverseCtx<'a> { /// Generate UID based on node. /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". + /// /// This is a shortcut for `ctx.scoping.generate_uid_based_on_node`. #[inline] pub fn generate_uid_based_on_node( @@ -319,6 +343,9 @@ impl<'a> TraverseCtx<'a> { /// Generate UID in current scope based on node. /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". + /// /// This is a shortcut for `ctx.scoping.generate_uid_in_current_scope_based_on_node`. #[inline] pub fn generate_uid_in_current_scope_based_on_node( diff --git a/crates/oxc_traverse/src/context/scoping.rs b/crates/oxc_traverse/src/context/scoping.rs index f0d9d70f41cad..acb66c3e19e40 100644 --- a/crates/oxc_traverse/src/context/scoping.rs +++ b/crates/oxc_traverse/src/context/scoping.rs @@ -140,10 +140,9 @@ impl TraverseScoping { new_scope_id } - /// Generate UID. + /// Generate UID var name. /// /// Finds a unique variable name which does clash with any other variables used in the program. - /// Generates a binding for it in scope provided. /// /// Based on Babel's `scope.generateUid` logic. /// @@ -216,9 +215,27 @@ impl TraverseScoping { /// what was found in AST. /// i.e. if source contains identifiers `_foo` and `__bar`, create UIDs names `___0`, `___1`, /// `___2` etc. They'll all be unique within the program. + #[allow(clippy::missing_panics_doc)] + pub fn generate_uid_name(&mut self, name: &str) -> CompactStr { + // If `uid_names` is not already populated, initialize it + if self.uid_names.is_none() { + self.init_uid_names(); + } + let uid_names = self.uid_names.as_mut().unwrap(); + + let base = get_uid_name_base(name); + let uid = get_unique_name(base, uid_names); + uid_names.insert(uid.clone()); + uid + } + + /// Generate UID in provided scope. + /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". pub fn generate_uid(&mut self, name: &str, scope_id: ScopeId, flags: SymbolFlags) -> SymbolId { // Get name for UID - let name = self.find_uid_name(name); + let name = self.generate_uid_name(name); // Add binding to scope let symbol_id = @@ -228,11 +245,17 @@ impl TraverseScoping { } /// Generate UID in current scope. + /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". pub fn generate_uid_in_current_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId { self.generate_uid(name, self.current_scope_id, flags) } /// Generate UID in root scope. + /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". pub fn generate_uid_in_root_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId { self.generate_uid(name, self.scopes.root_scope_id(), flags) } @@ -243,6 +266,9 @@ impl TraverseScoping { /// /// Based on Babel's `scope.generateUidBasedOnNode` logic. /// + /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". pub fn generate_uid_based_on_node<'a, T>( &mut self, node: &T, @@ -264,6 +290,9 @@ impl TraverseScoping { } /// Generate UID in current scope based on node. + /// + /// See also comments on [`TraverseScoping::generate_uid_name`] for important information + /// on how UIDs are generated. There are some potential "gotchas". pub fn generate_uid_in_current_scope_based_on_node<'a, T>( &mut self, node: &T, @@ -453,20 +482,6 @@ impl TraverseScoping { self.current_scope_id = scope_id; } - /// Find a variable name which can be used as a UID - fn find_uid_name(&mut self, name: &str) -> CompactStr { - // If `uid_names` is not already populated, initialize it - if self.uid_names.is_none() { - self.init_uid_names(); - } - let uid_names = self.uid_names.as_mut().unwrap(); - - let base = get_uid_name_base(name); - let uid = get_unique_name(base, uid_names); - uid_names.insert(uid.clone()); - uid - } - /// Initialize `uid_names`. /// /// Iterate through all symbols and unresolved references in AST and identify any var names