Skip to content

Commit

Permalink
refactor(transformer): import oxc_allocator::Vec as ArenaVec (#6998)
Browse files Browse the repository at this point in the history
Part of #6996.
  • Loading branch information
overlookmotel committed Oct 29, 2024
1 parent 63e8bfe commit fc1af2e
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 40 deletions.
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use std::{borrow::Cow, cell::RefCell};
use rustc_hash::FxHashMap;
use serde::Deserialize;

use oxc_allocator::{String as ArenaString, Vec};
use oxc_allocator::{String as ArenaString, Vec as ArenaVec};
use oxc_ast::ast::{Argument, CallExpression, Expression, TSTypeParameterInstantiation};
use oxc_semantic::{ReferenceFlags, SymbolFlags};
use oxc_span::{Atom, SPAN};
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<'a> TransformCtx<'a> {
pub fn helper_call(
&self,
helper: Helper,
arguments: Vec<'a, Argument<'a>>,
arguments: ArenaVec<'a, Argument<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> CallExpression<'a> {
let callee = self.helper_load(helper, ctx);
Expand All @@ -192,7 +192,7 @@ impl<'a> TransformCtx<'a> {
pub fn helper_call_expr(
&self,
helper: Helper,
arguments: Vec<'a, Argument<'a>>,
arguments: ArenaVec<'a, Argument<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let callee = self.helper_load(helper, ctx);
Expand Down
14 changes: 11 additions & 3 deletions crates/oxc_transformer/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Utility transforms which are in common between other transforms.

use oxc_allocator::Vec;
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

Expand Down Expand Up @@ -42,11 +42,19 @@ impl<'a, 'ctx> Traverse<'a> for Common<'a, 'ctx> {
self.top_level_statements.exit_program(program, ctx);
}

fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn enter_statements(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.var_declarations.enter_statements(stmts, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn exit_statements(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.var_declarations.exit_statements(stmts, ctx);
self.statement_injector.exit_statements(stmts, ctx);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::cell::RefCell;

use oxc_allocator::{Address, GetAddress, Vec as AVec};
use oxc_allocator::{Address, GetAddress, Vec as ArenaVec};

use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
Expand All @@ -36,7 +36,7 @@ impl<'a, 'ctx> StatementInjector<'a, 'ctx> {
impl<'a, 'ctx> Traverse<'a> for StatementInjector<'a, 'ctx> {
fn exit_statements(
&mut self,
statements: &mut AVec<'a, Statement<'a>>,
statements: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.ctx.statement_injector.insert_into_statements(statements, ctx);
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<'a> StatementInjectorStore<'a> {
/// Insert statements immediately before / after the target statement.
fn insert_into_statements(
&self,
statements: &mut AVec<'a, Statement<'a>>,
statements: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
let mut insertions = self.insertions.borrow_mut();
Expand Down
14 changes: 9 additions & 5 deletions crates/oxc_transformer/src/common/var_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::cell::RefCell;

use oxc_allocator::Vec;
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::ast::*;
use oxc_data_structures::stack::SparseStack;
use oxc_span::SPAN;
Expand All @@ -39,13 +39,17 @@ impl<'a, 'ctx> VarDeclarations<'a, 'ctx> {
impl<'a, 'ctx> Traverse<'a> for VarDeclarations<'a, 'ctx> {
fn enter_statements(
&mut self,
_stmts: &mut Vec<'a, Statement<'a>>,
_stmts: &mut ArenaVec<'a, Statement<'a>>,
_ctx: &mut TraverseCtx<'a>,
) {
self.ctx.var_declarations.record_entering_statements();
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn exit_statements(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.ctx.var_declarations.insert_into_statements(stmts, ctx);
}

Expand All @@ -56,7 +60,7 @@ impl<'a, 'ctx> Traverse<'a> for VarDeclarations<'a, 'ctx> {

/// Store for `VariableDeclarator`s to be added to enclosing statement block.
pub struct VarDeclarationsStore<'a> {
stack: RefCell<SparseStack<Vec<'a, VariableDeclarator<'a>>>>,
stack: RefCell<SparseStack<ArenaVec<'a, VariableDeclarator<'a>>>>,
}

// Public methods
Expand Down Expand Up @@ -107,7 +111,7 @@ impl<'a> VarDeclarationsStore<'a> {

fn insert_into_statements(
&self,
stmts: &mut Vec<'a, Statement<'a>>,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
if matches!(ctx.parent(), Ancestor::ProgramBody(_)) {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@

use serde::Deserialize;

use oxc_allocator::Vec;
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::ast::*;
use oxc_data_structures::stack::SparseStack;
use oxc_span::SPAN;
Expand Down Expand Up @@ -405,7 +405,7 @@ impl<'a> ArrowFunctions<'a> {
#[expect(clippy::unused_self)]
fn insert_this_var_statement_at_the_top_of_statements(
&mut self,
statements: &mut Vec<'a, Statement<'a>>,
statements: &mut ArenaVec<'a, Statement<'a>>,
this_var: &BoundIdentifier<'a>,
ctx: &TraverseCtx<'a>,
) {
Expand Down
16 changes: 8 additions & 8 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! * Exponentiation operator TC39 proposal: <https://github.com/tc39/proposal-exponentiation-operator>
//! * Exponentiation operator specification: <https://tc39.es/ecma262/#sec-exp-operator>

use oxc_allocator::{CloneIn, Vec};
use oxc_allocator::{CloneIn, Vec as ArenaVec};
use oxc_ast::{ast::*, NONE};
use oxc_semantic::{ReferenceFlags, SymbolFlags};
use oxc_span::SPAN;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
ArenaVec<'a, Expression<'a>>,
) {
let mut temp_var_inits = ctx.ast.vec();

Expand Down Expand Up @@ -232,7 +232,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
ArenaVec<'a, Expression<'a>>,
) {
// Object part of 2nd member expression
// ```
Expand Down Expand Up @@ -326,7 +326,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
ArenaVec<'a, Expression<'a>>,
) {
// Object part of 2nd member expression
// ```
Expand Down Expand Up @@ -408,7 +408,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
ArenaVec<'a, Expression<'a>>,
) {
// Object part of 2nd member expression
// ```
Expand Down Expand Up @@ -482,7 +482,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
fn get_second_member_expression_object(
&mut self,
obj: &mut Expression<'a>,
temp_var_inits: &mut Vec<'a, Expression<'a>>,
temp_var_inits: &mut ArenaVec<'a, Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
// If the object reference that we need to save is locally declared, evaluating it multiple times
Expand Down Expand Up @@ -532,7 +532,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
/// If needs temp var initializers, replace expression `expr` with `(temp1, temp2, expr)`.
fn revise_expression(
expr: &mut Expression<'a>,
mut temp_var_inits: Vec<'a, Expression<'a>>,
mut temp_var_inits: ArenaVec<'a, Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
if !temp_var_inits.is_empty() {
Expand Down Expand Up @@ -566,7 +566,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
fn create_temp_var(
&mut self,
expr: Expression<'a>,
temp_var_inits: &mut Vec<'a, Expression<'a>>,
temp_var_inits: &mut ArenaVec<'a, Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
let binding = ctx.generate_uid_in_current_scope_based_on_node(
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/jsx/jsx_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
//!
//! * Babel plugin implementation: <https://github.com/babel/babel/tree/main/packages/babel-helper-builder-react-jsx>

use oxc_allocator::Vec;
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::{ast::*, AstBuilder, NONE};
use oxc_ecmascript::PropName;
use oxc_span::{Atom, GetSpan, Span, SPAN};
Expand Down Expand Up @@ -999,7 +999,7 @@ impl<'a, 'b> JSXElementOrFragment<'a, 'b> {
}
}

fn children(&self) -> &'b Vec<'a, JSXChild<'a>> {
fn children(&self) -> &'b ArenaVec<'a, JSXChild<'a>> {
match self {
Self::Element(e) => &e.children,
Self::Fragment(e) => &e.children,
Expand Down
14 changes: 11 additions & 3 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use std::path::Path;

use oxc_allocator::{Allocator, Vec};
use oxc_allocator::{Allocator, Vec as ArenaVec};
use oxc_ast::{ast::*, AstBuilder};
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{ScopeTree, SymbolTable};
Expand Down Expand Up @@ -356,7 +356,11 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
}
}

fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn enter_statements(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.common.enter_statements(stmts, ctx);
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.enter_statements(stmts, ctx);
Expand Down Expand Up @@ -387,7 +391,11 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
}
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn exit_statements(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.exit_statements(stmts, ctx);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/typescript/enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashMap;

use oxc_allocator::Vec;
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::{ast::*, visit::walk_mut, VisitMut, NONE};
use oxc_ecmascript::ToInt32;
use oxc_span::{Atom, Span, SPAN};
Expand Down Expand Up @@ -181,10 +181,10 @@ impl<'a> TypeScriptEnum<'a> {
#[allow(clippy::needless_pass_by_value)]
fn transform_ts_enum_members(
&mut self,
members: &mut Vec<'a, TSEnumMember<'a>>,
members: &mut ArenaVec<'a, TSEnumMember<'a>>,
param: &BindingIdentifier<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Vec<'a, Statement<'a>> {
) -> ArenaVec<'a, Statement<'a>> {
let create_identifier_reference = |ctx: &mut TraverseCtx<'a>| {
let ident = ctx.create_bound_reference_id(
param.span,
Expand Down
14 changes: 11 additions & 3 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_allocator::Vec;
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

Expand Down Expand Up @@ -197,11 +197,19 @@ impl<'a, 'ctx> Traverse<'a> for TypeScript<'a, 'ctx> {
self.annotations.enter_accessor_property(def, ctx);
}

fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn enter_statements(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.annotations.enter_statements(stmts, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn exit_statements(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.annotations.exit_statements(stmts, ctx);
}

Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_transformer/src/typescript/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashSet;

use oxc_allocator::{Box, Vec};
use oxc_allocator::{Box, Vec as ArenaVec};
use oxc_ast::{ast::*, NONE};
use oxc_ecmascript::BoundNames;
use oxc_span::{Atom, CompactStr, SPAN};
Expand Down Expand Up @@ -320,8 +320,8 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
fn transform_namespace(
arg_name: Atom<'a>,
real_name: Atom<'a>,
stmts: Vec<'a, Statement<'a>>,
directives: Vec<'a, Directive<'a>>,
stmts: ArenaVec<'a, Statement<'a>>,
directives: ArenaVec<'a, Directive<'a>>,
parent_export: Option<Expression<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
Expand Down Expand Up @@ -414,7 +414,7 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
decl: Declaration<'a>,
name: Atom<'a>,
names: &mut FxHashSet<Atom<'a>>,
new_stmts: &mut Vec<'a, Statement<'a>>,
new_stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &TraverseCtx<'a>,
) {
// This function is only called with a function, class, or enum declaration,
Expand Down Expand Up @@ -449,7 +449,7 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
mut var_decl: Box<'a, VariableDeclaration<'a>>,
name: Atom<'a>,
ctx: &TraverseCtx<'a>,
) -> Vec<'a, Statement<'a>> {
) -> ArenaVec<'a, Statement<'a>> {
let is_all_binding_identifier = var_decl
.declarations
.iter()
Expand Down

0 comments on commit fc1af2e

Please sign in to comment.