Skip to content

Commit

Permalink
refactor(traverse): generate_uid_in_root_scope method
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jun 10, 2024
1 parent 2c87f13 commit 2cc4913
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl ScopeTree {
self.parent_ids.iter_enumerated().map(|(scope_id, _)| scope_id)
}

#[inline]
pub fn root_scope_id(&self) -> ScopeId {
ScopeId::new(0)
}
Expand Down
6 changes: 2 additions & 4 deletions crates/oxc_transformer/src/react/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ impl<'a> AutomaticScriptBindings<'a> {
front: bool,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
let root_scope_id = ctx.scopes().root_scope_id();
let symbol_id =
ctx.generate_uid(variable_name, root_scope_id, SymbolFlags::FunctionScopedVariable);
ctx.generate_uid_in_root_scope(variable_name, SymbolFlags::FunctionScopedVariable);
let variable_name = ctx.ast.new_atom(&ctx.symbols().names[symbol_id]);

let import = NamedImport::new(variable_name.clone(), None, symbol_id);
Expand Down Expand Up @@ -223,8 +222,7 @@ impl<'a> AutomaticModuleBindings<'a> {
source: Atom<'a>,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
let root_scope_id = ctx.scopes().root_scope_id();
let symbol_id = ctx.generate_uid(name, root_scope_id, SymbolFlags::FunctionScopedVariable);
let symbol_id = ctx.generate_uid_in_root_scope(name, SymbolFlags::FunctionScopedVariable);
let local = ctx.ast.new_atom(&ctx.symbols().names[symbol_id]);

let import = NamedImport::new(Atom::from(name), Some(local.clone()), symbol_id);
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ impl<'a> TraverseCtx<'a> {
self.scoping.generate_uid_in_current_scope(name, flags)
}

/// Generate UID in root scope.
///
/// This is a shortcut for `ctx.scoping.generate_uid_in_root_scope`.
pub fn generate_uid_in_root_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId {
self.scoping.generate_uid_in_root_scope(name, flags)
}

/// Create a reference bound to a `SymbolId`.
///
/// This is a shortcut for `ctx.scoping.create_bound_reference`.
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ impl TraverseScoping {
self.generate_uid(name, self.current_scope_id, flags)
}

/// Generate UID in root scope.
pub fn generate_uid_in_root_scope(&mut self, name: &str, flags: SymbolFlags) -> SymbolId {
self.generate_uid(name, self.scopes.root_scope_id(), flags)
}

/// Create a reference bound to a `SymbolId`
pub fn create_bound_reference(
&mut self,
Expand Down

0 comments on commit 2cc4913

Please sign in to comment.