Skip to content

Commit

Permalink
refactor(transformer): do not use AstBuilder::*_from_* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 1, 2024
1 parent 203cd16 commit f4af189
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 231 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<'a> HelperLoaderStore<'a> {
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), HELPER_VAR);
let ident =
ctx.create_reference_id(SPAN, Atom::from(HELPER_VAR), symbol_id, ReferenceFlags::Read);
let object = ctx.ast.expression_from_identifier_reference(ident);
let object = Expression::Identifier(ctx.alloc(ident));
let property = ctx.ast.identifier_name(SPAN, Atom::from(helper.name()));
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}
Expand Down
10 changes: 4 additions & 6 deletions crates/oxc_transformer/src/common/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,13 @@ impl<'a> ModuleImportsStore<'a> {
),
}));

let import_stmt = ctx.ast.module_declaration_import_declaration(
Statement::from(ctx.ast.module_declaration_import_declaration(
SPAN,
Some(specifiers),
ctx.ast.string_literal(SPAN, source),
NONE,
ImportOrExportKind::Value,
);
ctx.ast.statement_module_declaration(import_stmt)
))
}

fn get_require(
Expand All @@ -234,7 +233,7 @@ impl<'a> ModuleImportsStore<'a> {
require_symbol_id,
ReferenceFlags::read(),
);
let callee = ctx.ast.expression_from_identifier_reference(ident);
let callee = Expression::Identifier(ctx.alloc(ident));

let args = {
let arg = Argument::from(ctx.ast.expression_string_literal(SPAN, source));
Expand All @@ -247,7 +246,6 @@ impl<'a> ModuleImportsStore<'a> {
let decl = ctx.ast.variable_declarator(SPAN, var_kind, id, Some(init), false);
ctx.ast.vec1(decl)
};
let var_decl = ctx.ast.declaration_variable(SPAN, var_kind, decl, false);
ctx.ast.statement_declaration(var_decl)
Statement::from(ctx.ast.declaration_variable(SPAN, var_kind, decl, false))
}
}
12 changes: 6 additions & 6 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 as ArenaVec;
use oxc_allocator::{Box as ArenaBox, Vec as ArenaVec};
use oxc_ast::ast::*;
use oxc_data_structures::stack::SparseStack;
use oxc_span::SPAN;
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
) {
if let JSXElementName::ThisExpression(this) = element_name {
if let Some(ident) = self.get_this_identifier(this.span, ctx) {
*element_name = ctx.ast.jsx_element_name_from_identifier_reference(ident);
*element_name = JSXElementName::IdentifierReference(ident);
}
};
}
Expand All @@ -235,15 +235,15 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
) {
if let JSXMemberExpressionObject::ThisExpression(this) = object {
if let Some(ident) = self.get_this_identifier(this.span, ctx) {
*object = ctx.ast.jsx_member_expression_object_from_identifier_reference(ident);
*object = JSXMemberExpressionObject::IdentifierReference(ident);
}
}
}

fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::ThisExpression(this) = expr {
if let Some(ident) = self.get_this_identifier(this.span, ctx) {
*expr = ctx.ast.expression_from_identifier_reference(ident);
*expr = Expression::Identifier(ident);
}
}
}
Expand All @@ -266,7 +266,7 @@ impl<'a> ArrowFunctions<'a> {
&mut self,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> Option<IdentifierReference<'a>> {
) -> Option<ArenaBox<'a, IdentifierReference<'a>>> {
// Find arrow function we are currently in (if we are)
let arrow_scope_id = Self::get_arrow_function_scope(ctx)?;

Expand All @@ -289,7 +289,7 @@ impl<'a> ArrowFunctions<'a> {
.unwrap();
ctx.generate_uid("this", target_scope_id, SymbolFlags::FunctionScopedVariable)
});
Some(this_var.create_spanned_read_reference(span, ctx))
Some(ctx.ast.alloc(this_var.create_spanned_read_reference(span, ctx)))
}

/// Find arrow function we are currently in, if it's between current node, and where `this` is bound.
Expand Down
63 changes: 31 additions & 32 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,30 +155,31 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {

// Make sure side-effects of evaluating `left` only happen once
let reference = ctx.scoping.symbols_mut().get_reference_mut(ident.reference_id().unwrap());
let pow_left = if let Some(symbol_id) = reference.symbol_id() {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
// `left **= right` is being transformed to `left = Math.pow(left, right)`,
// so if `left` is no longer being read from, update its `ReferenceFlags`.
if matches!(ctx.ancestry.parent(), Ancestor::ExpressionStatementExpression(_)) {
*reference.flags_mut() = ReferenceFlags::Write;
}
let pow_left =
if let Some(symbol_id) = reference.symbol_id() {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
// `left **= right` is being transformed to `left = Math.pow(left, right)`,
// so if `left` is no longer being read from, update its `ReferenceFlags`.
if matches!(ctx.ancestry.parent(), Ancestor::ExpressionStatementExpression(_)) {
*reference.flags_mut() = ReferenceFlags::Write;
}

ctx.ast.expression_from_identifier_reference(ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
))
} else {
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
let reference = ctx.ast.expression_from_identifier_reference(
ctx.create_unbound_reference_id(SPAN, ident.name.clone(), ReferenceFlags::Read),
);
let binding = self.create_temp_var(reference, &mut temp_var_inits, ctx);
binding.create_read_expression(ctx)
};
Expression::Identifier(ctx.ast.alloc(ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
)))
} else {
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
let reference = Expression::Identifier(ctx.ast.alloc(
ctx.create_unbound_reference_id(SPAN, ident.name.clone(), ReferenceFlags::Read),
));
let binding = self.create_temp_var(reference, &mut temp_var_inits, ctx);
binding.create_read_expression(ctx)
};

(pow_left, temp_var_inits)
}
Expand Down Expand Up @@ -496,14 +497,12 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
if let Some(symbol_id) = symbol_id {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
return ctx.ast.expression_from_identifier_reference(
ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
),
);
return Expression::Identifier(ctx.ast.alloc(ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
)));
}
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
Expand Down Expand Up @@ -551,7 +550,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
let math_symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "Math");
let ident_math =
ctx.create_reference_id(SPAN, Atom::from("Math"), math_symbol_id, ReferenceFlags::Read);
let object = ctx.ast.expression_from_identifier_reference(ident_math);
let object = Expression::Identifier(ctx.alloc(ident_math));
let property = ctx.ast.identifier_name(SPAN, "pow");
let callee =
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false));
Expand Down
38 changes: 18 additions & 20 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,16 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
id.symbol_id.get().unwrap(),
ReferenceFlags::Read,
);
let statement = Statement::from(ctx.ast.declaration_from_function(caller_function));
let statement = Statement::FunctionDeclaration(caller_function);
statements.push(statement);
let argument = Some(ctx.ast.expression_from_identifier_reference(reference));
let argument = Some(Expression::Identifier(ctx.alloc(reference)));
statements.push(ctx.ast.statement_return(SPAN, argument));
} else {
// If the function doesn't have an id, then we need to return the function itself.
// `function() { ... }` -> `return function() { ... };`
let statement_return = ctx.ast.statement_return(
SPAN,
Some(ctx.ast.expression_from_function(caller_function)),
);
let statement_return = ctx
.ast
.statement_return(SPAN, Some(Expression::FunctionExpression(caller_function)));
statements.push(statement_return);
}
debug_assert!(wrapper_function.body.is_none());
Expand All @@ -323,7 +322,8 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
}

// Construct the IIFE
let callee = ctx.ast.expression_from_function(ctx.ast.move_function(wrapper_function));
let callee =
Expression::FunctionExpression(ctx.alloc(ctx.ast.move_function(wrapper_function)));
ctx.ast.expression_call(SPAN, callee, NONE, ctx.ast.vec(), false)
}

Expand Down Expand Up @@ -385,7 +385,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
let params = Self::create_empty_params(ctx);
let id = Some(bound_ident.create_binding_identifier(ctx));
let caller_function = Self::create_function(id, params, body, scope_id, ctx);
Statement::from(ctx.ast.declaration_from_function(caller_function))
Statement::FunctionDeclaration(caller_function)
}
}

Expand Down Expand Up @@ -439,7 +439,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
.create_binding_identifier(ctx)
});
let function = Self::create_function(id, params, body, scope_id, ctx);
let argument = Some(ctx.ast.expression_from_function(function));
let argument = Some(Expression::FunctionExpression(function));
ctx.ast.statement_return(SPAN, argument)
};

Expand All @@ -459,7 +459,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
let params = Self::create_empty_params(ctx);
let wrapper_function = Self::create_function(None, params, body, wrapper_scope_id, ctx);
// Construct the IIFE
let callee = ctx.ast.expression_from_function(wrapper_function);
let callee = Expression::FunctionExpression(wrapper_function);
ctx.ast.expression_call(SPAN, callee, NONE, ctx.ast.vec(), false)
}
}
Expand Down Expand Up @@ -493,13 +493,13 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
body: ArenaBox<'a, FunctionBody<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Function<'a> {
) -> ArenaBox<'a, Function<'a>> {
let r#type = if id.is_some() {
FunctionType::FunctionDeclaration
} else {
FunctionType::FunctionExpression
};
ctx.ast.function_with_scope_id(
ctx.ast.alloc_function_with_scope_id(
r#type,
SPAN,
id,
Expand Down Expand Up @@ -528,14 +528,14 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "arguments");
let arguments_ident =
ctx.create_reference_id(SPAN, Atom::from("arguments"), symbol_id, ReferenceFlags::Read);
let arguments_ident = ctx.ast.expression_from_identifier_reference(arguments_ident);
let arguments_ident = Argument::Identifier(ctx.alloc(arguments_ident));

// (this, arguments)
let mut arguments = ctx.ast.vec_with_capacity(2);
arguments.push(ctx.ast.argument_expression(ctx.ast.expression_this(SPAN)));
arguments.push(ctx.ast.argument_expression(arguments_ident));
arguments.push(Argument::from(ctx.ast.expression_this(SPAN)));
arguments.push(arguments_ident);
// _ref.apply
let callee = ctx.ast.expression_member(ctx.ast.member_expression_static(
let callee = Expression::from(ctx.ast.member_expression_static(
SPAN,
bound_ident.create_read_expression(ctx),
ctx.ast.identifier_name(SPAN, "apply"),
Expand Down Expand Up @@ -565,9 +565,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
) -> Expression<'a> {
let mut function = Self::create_function(None, params, body, scope_id, ctx);
function.generator = true;
let function_expression = ctx.ast.expression_from_function(function);
let argument = ctx.ast.argument_expression(function_expression);
let arguments = ctx.ast.vec1(argument);
let arguments = ctx.ast.vec1(Argument::FunctionExpression(function));
self.ctx.helper_call_expr(self.helper, arguments, ctx)
}

Expand Down Expand Up @@ -595,7 +593,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
Some(init),
false,
));
ctx.ast.statement_declaration(ctx.ast.declaration_variable(
Statement::from(ctx.ast.declaration_variable(
SPAN,
VariableDeclarationKind::Var,
declarations,
Expand Down
Loading

0 comments on commit f4af189

Please sign in to comment.