Skip to content

Commit 53e2bc0

Browse files
committed
feat(traverse): add TraverseScoping::rename_symbol method (#7871)
Add `TraverseScoping::rename_symbol` method. This renames the symbol, and also the binding for that symbol.
1 parent 4924073 commit 53e2bc0

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

crates/oxc_transformer/src/common/arrow_function_converter.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,7 @@ impl<'a> ArrowFunctionConverter<'a> {
905905
/// Rename the `arguments` symbol to a new name.
906906
fn rename_arguments_symbol(symbol_id: SymbolId, name: CompactStr, ctx: &mut TraverseCtx<'a>) {
907907
let scope_id = ctx.symbols().get_scope_id(symbol_id);
908-
ctx.symbols_mut().set_name(symbol_id, name.clone());
909-
ctx.scopes_mut().rename_binding(scope_id, symbol_id, "arguments", name);
908+
ctx.rename_symbol(symbol_id, scope_id, name);
910909
}
911910

912911
/// Transform the identifier reference for `arguments` if it's affected after transformation.

crates/oxc_traverse/src/context/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,21 @@ impl<'a> TraverseCtx<'a> {
589589
pub fn delete_reference_for_identifier(&mut self, ident: &IdentifierReference) {
590590
self.scoping.delete_reference_for_identifier(ident);
591591
}
592+
593+
/// Rename symbol.
594+
///
595+
/// Preserves original order of bindings for scope.
596+
///
597+
/// The following must be true for successful operation:
598+
/// * Binding exists in specified scope for `symbol_id`.
599+
/// * No binding already exists in scope for `new_name`.
600+
///
601+
/// Panics in debug mode if either of the above are not satisfied.
602+
///
603+
/// This is a shortcut for `ctx.scoping.rename_symbol`.
604+
pub fn rename_symbol(&mut self, symbol_id: SymbolId, scope_id: ScopeId, new_name: CompactStr) {
605+
self.scoping.rename_symbol(symbol_id, scope_id, new_name);
606+
}
592607
}
593608

594609
// Methods used internally within crate

crates/oxc_traverse/src/context/scoping.rs

+16
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,22 @@ impl TraverseScoping {
362362
pub fn delete_reference_for_identifier(&mut self, ident: &IdentifierReference) {
363363
self.delete_reference(ident.reference_id(), &ident.name);
364364
}
365+
366+
/// Rename symbol.
367+
///
368+
/// Preserves original order of bindings for scope.
369+
///
370+
/// The following must be true for successful operation:
371+
/// * Binding exists in specified scope for `symbol_id`.
372+
/// * No binding already exists in scope for `new_name`.
373+
///
374+
/// Panics in debug mode if either of the above are not satisfied.
375+
pub fn rename_symbol(&mut self, symbol_id: SymbolId, scope_id: ScopeId, new_name: CompactStr) {
376+
// Rename symbol
377+
let old_name = self.symbols.set_name(symbol_id, new_name.clone());
378+
// Rename binding
379+
self.scopes.rename_binding(scope_id, symbol_id, &old_name, new_name);
380+
}
365381
}
366382

367383
// Methods used internally within crate

0 commit comments

Comments
 (0)