mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(traverse): generate_uid_name method (#5839)
Rename `find_uid_name` to `generate_uid_name` and make it a public method.
This commit is contained in:
parent
40cdad572c
commit
635e9187d3
2 changed files with 59 additions and 17 deletions
|
|
@ -280,8 +280,23 @@ impl<'a> TraverseCtx<'a> {
|
||||||
self.scoping.insert_scope_below_expression(expr, flags)
|
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.
|
/// 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`.
|
/// This is a shortcut for `ctx.scoping.generate_uid`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn generate_uid(&mut self, name: &str, scope_id: ScopeId, flags: SymbolFlags) -> SymbolId {
|
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.
|
/// 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`.
|
/// This is a shortcut for `ctx.scoping.generate_uid_in_current_scope`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn generate_uid_in_current_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId {
|
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.
|
/// 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`.
|
/// This is a shortcut for `ctx.scoping.generate_uid_in_root_scope`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn generate_uid_in_root_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId {
|
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.
|
/// 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`.
|
/// This is a shortcut for `ctx.scoping.generate_uid_based_on_node`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn generate_uid_based_on_node(
|
pub fn generate_uid_based_on_node(
|
||||||
|
|
@ -319,6 +343,9 @@ impl<'a> TraverseCtx<'a> {
|
||||||
|
|
||||||
/// Generate UID in current scope based on node.
|
/// 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`.
|
/// This is a shortcut for `ctx.scoping.generate_uid_in_current_scope_based_on_node`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn generate_uid_in_current_scope_based_on_node(
|
pub fn generate_uid_in_current_scope_based_on_node(
|
||||||
|
|
|
||||||
|
|
@ -140,10 +140,9 @@ impl TraverseScoping {
|
||||||
new_scope_id
|
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.
|
/// 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.
|
/// Based on Babel's `scope.generateUid` logic.
|
||||||
/// <https://github.com/babel/babel/blob/3b1a3c0be9df65140260a316c1a21adcf948645d/packages/babel-traverse/src/scope/index.ts#L501-L523>
|
/// <https://github.com/babel/babel/blob/3b1a3c0be9df65140260a316c1a21adcf948645d/packages/babel-traverse/src/scope/index.ts#L501-L523>
|
||||||
|
|
@ -216,9 +215,27 @@ impl TraverseScoping {
|
||||||
/// what was found in AST.
|
/// what was found in AST.
|
||||||
/// i.e. if source contains identifiers `_foo` and `__bar`, create UIDs names `___0`, `___1`,
|
/// i.e. if source contains identifiers `_foo` and `__bar`, create UIDs names `___0`, `___1`,
|
||||||
/// `___2` etc. They'll all be unique within the program.
|
/// `___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 {
|
pub fn generate_uid(&mut self, name: &str, scope_id: ScopeId, flags: SymbolFlags) -> SymbolId {
|
||||||
// Get name for UID
|
// Get name for UID
|
||||||
let name = self.find_uid_name(name);
|
let name = self.generate_uid_name(name);
|
||||||
|
|
||||||
// Add binding to scope
|
// Add binding to scope
|
||||||
let symbol_id =
|
let symbol_id =
|
||||||
|
|
@ -228,11 +245,17 @@ impl TraverseScoping {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate UID in current scope.
|
/// 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 {
|
pub fn generate_uid_in_current_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId {
|
||||||
self.generate_uid(name, self.current_scope_id, flags)
|
self.generate_uid(name, self.current_scope_id, flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate UID in root scope.
|
/// 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 {
|
pub fn generate_uid_in_root_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId {
|
||||||
self.generate_uid(name, self.scopes.root_scope_id(), flags)
|
self.generate_uid(name, self.scopes.root_scope_id(), flags)
|
||||||
}
|
}
|
||||||
|
|
@ -243,6 +266,9 @@ impl TraverseScoping {
|
||||||
///
|
///
|
||||||
/// Based on Babel's `scope.generateUidBasedOnNode` logic.
|
/// Based on Babel's `scope.generateUidBasedOnNode` logic.
|
||||||
/// <https://github.com/babel/babel/blob/419644f27c5c59deb19e71aaabd417a3bc5483ca/packages/babel-traverse/src/scope/index.ts#L543>
|
/// <https://github.com/babel/babel/blob/419644f27c5c59deb19e71aaabd417a3bc5483ca/packages/babel-traverse/src/scope/index.ts#L543>
|
||||||
|
///
|
||||||
|
/// 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>(
|
pub fn generate_uid_based_on_node<'a, T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
node: &T,
|
node: &T,
|
||||||
|
|
@ -264,6 +290,9 @@ impl TraverseScoping {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate UID in current scope based on node.
|
/// 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>(
|
pub fn generate_uid_in_current_scope_based_on_node<'a, T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
node: &T,
|
node: &T,
|
||||||
|
|
@ -453,20 +482,6 @@ impl TraverseScoping {
|
||||||
self.current_scope_id = scope_id;
|
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`.
|
/// Initialize `uid_names`.
|
||||||
///
|
///
|
||||||
/// Iterate through all symbols and unresolved references in AST and identify any var names
|
/// Iterate through all symbols and unresolved references in AST and identify any var names
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue