Skip to content

Commit

Permalink
refactor(transformer): rename VarDeclarationsStore methods (#6184)
Browse files Browse the repository at this point in the history
Pure refactor. Shorten the names of `VarDeclarationsStore`'s public methods and add new `insert_declarator` method.
  • Loading branch information
overlookmotel committed Sep 30, 2024
1 parent 81be545 commit 70d4c56
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions crates/oxc_transformer/src/common/var_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<'a> VarDeclarationsStore<'a> {
impl<'a> VarDeclarationsStore<'a> {
/// Add a `VariableDeclarator` to be inserted at top of current enclosing statement block,
/// given `name` and `symbol_id`.
pub fn insert_declarator(
pub fn insert(
&self,
name: Atom<'a>,
symbol_id: SymbolId,
Expand All @@ -92,19 +92,24 @@ impl<'a> VarDeclarationsStore<'a> {
let ident = BindingIdentifier::new_with_symbol_id(SPAN, name, symbol_id);
let ident = ctx.ast.binding_pattern_kind_from_binding_identifier(ident);
let ident = ctx.ast.binding_pattern(ident, NONE, false);
self.insert_declarator_binding_pattern(ident, init, ctx);
self.insert_binding_pattern(ident, init, ctx);
}

/// Add a `VariableDeclarator` to be inserted at top of current enclosing statement block,
/// given a `BindingPattern`.
pub fn insert_declarator_binding_pattern(
pub fn insert_binding_pattern(
&self,
ident: BindingPattern<'a>,
init: Option<Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
let declarator =
ctx.ast.variable_declarator(SPAN, VariableDeclarationKind::Var, ident, init, false);
self.insert_declarator(declarator, ctx);
}

/// Add a `VariableDeclarator` to be inserted at top of current enclosing statement block.
pub fn insert_declarator(&self, declarator: VariableDeclarator<'a>, ctx: &mut TraverseCtx<'a>) {
let mut declarators = self.declarators.borrow_mut();
declarators.last_mut_or_init(|| ctx.ast.vec()).push(declarator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
let symbol_name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));

// var _name;
self.ctx.var_declarations.insert_declarator(symbol_name.clone(), symbol_id, None, ctx);
self.ctx.var_declarations.insert(symbol_name.clone(), symbol_id, None, ctx);

let ident =
ctx.create_reference_id(SPAN, symbol_name, Some(symbol_id), ReferenceFlags::Read);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<'a, 'ctx> Traverse<'a> for NullishCoalescingOperator<'a, 'ctx> {
// `(x) => x;` -> `((x) => x)();`
new_expr = ctx.ast.expression_call(SPAN, arrow_function, NONE, ctx.ast.vec(), false);
} else {
self.ctx.var_declarations.insert_declarator_binding_pattern(id, None, ctx);
self.ctx.var_declarations.insert_binding_pattern(id, None, ctx);
}

*expr = new_expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl<'a, 'ctx> LogicalAssignmentOperators<'a, 'ctx> {
let symbol_name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));

// var _name;
self.ctx.var_declarations.insert_declarator(symbol_name.clone(), symbol_id, None, ctx);
self.ctx.var_declarations.insert(symbol_name.clone(), symbol_id, None, ctx);

// _name = name
Some(ctx.create_reference_id(SPAN, symbol_name, Some(symbol_id), ReferenceFlags::Write))
Expand Down

0 comments on commit 70d4c56

Please sign in to comment.