Skip to content

Commit

Permalink
refactor(transformer/es2016): move all entry points to implementation…
Browse files Browse the repository at this point in the history
… of Traverse trait (#5085)

follow-up #4881
  • Loading branch information
Dunqing committed Aug 22, 2024
1 parent 178d1bd commit a9fcf29
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
50 changes: 27 additions & 23 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use oxc_ast::ast::*;
use oxc_semantic::{ReferenceFlags, SymbolFlags};
use oxc_span::SPAN;
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator};
use oxc_traverse::TraverseCtx;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::context::Ctx;

Expand All @@ -62,35 +62,18 @@ impl<'a> ExponentiationOperator<'a> {
pub fn new(ctx: Ctx<'a>) -> Self {
Self { _ctx: ctx, var_declarations: vec![] }
}
}

fn clone_identifier_reference(
ident: &IdentifierReference<'a>,
ctx: &mut TraverseCtx<'a>,
) -> IdentifierReference<'a> {
let reference = ctx.symbols().get_reference(ident.reference_id.get().unwrap());
let symbol_id = reference.symbol_id();
let flags = reference.flags();
ctx.create_reference_id(ident.span, ident.name.clone(), symbol_id, flags)
}

fn clone_expression(expr: &Expression<'a>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
match expr {
Expression::Identifier(ident) => ctx
.ast
.expression_from_identifier_reference(Self::clone_identifier_reference(ident, ctx)),
_ => expr.clone_in(ctx.ast.allocator),
}
}

pub fn transform_statements(
impl<'a> Traverse<'a> for ExponentiationOperator<'a> {
fn enter_statements(
&mut self,
_statements: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.var_declarations.push(ctx.ast.vec());
}

pub fn transform_statements_on_exit(
fn exit_statements(
&mut self,
statements: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
Expand All @@ -109,7 +92,7 @@ impl<'a> ExponentiationOperator<'a> {
}
}

pub fn transform_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
// left ** right
if let Expression::BinaryExpression(binary_expr) = expr {
if binary_expr.operator == BinaryOperator::Exponential {
Expand Down Expand Up @@ -141,6 +124,27 @@ impl<'a> ExponentiationOperator<'a> {
}
}
}
}

impl<'a> ExponentiationOperator<'a> {
fn clone_identifier_reference(
ident: &IdentifierReference<'a>,
ctx: &mut TraverseCtx<'a>,
) -> IdentifierReference<'a> {
let reference = ctx.symbols().get_reference(ident.reference_id.get().unwrap());
let symbol_id = reference.symbol_id();
let flags = reference.flags();
ctx.create_reference_id(ident.span, ident.name.clone(), symbol_id, flags)
}

fn clone_expression(expr: &Expression<'a>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
match expr {
Expression::Identifier(ident) => ctx
.ast
.expression_from_identifier_reference(Self::clone_identifier_reference(ident, ctx)),
_ => expr.clone_in(ctx.ast.allocator),
}
}

/// `left ** right` -> `Math.pow(left, right)`
fn math_pow(
Expand Down
16 changes: 9 additions & 7 deletions crates/oxc_transformer/src/es2016/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use exponentiation_operator::ExponentiationOperator;
pub use options::ES2016Options;
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_traverse::TraverseCtx;
use oxc_traverse::{Traverse, TraverseCtx};
use std::rc::Rc;

use crate::context::Ctx;
Expand All @@ -23,30 +23,32 @@ impl<'a> ES2016<'a> {
pub fn new(options: ES2016Options, ctx: Ctx<'a>) -> Self {
Self { exponentiation_operator: ExponentiationOperator::new(Rc::clone(&ctx)), ctx, options }
}
}

pub fn transform_statements(
impl<'a> Traverse<'a> for ES2016<'a> {
fn enter_statements(
&mut self,
statements: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.exponentiation_operator {
self.exponentiation_operator.transform_statements(statements, ctx);
self.exponentiation_operator.enter_statements(statements, ctx);
}
}

pub fn transform_statements_on_exit(
fn exit_statements(
&mut self,
statements: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.exponentiation_operator {
self.exponentiation_operator.transform_statements_on_exit(statements, ctx);
self.exponentiation_operator.exit_statements(statements, ctx);
}
}

pub fn transform_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.exponentiation_operator {
self.exponentiation_operator.transform_expression(expr, ctx);
self.exponentiation_operator.enter_expression(expr, ctx);
}
}
}
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x1_react.transform_expression(expr, ctx);
self.x2_es2021.enter_expression(expr, ctx);
self.x2_es2020.enter_expression(expr, ctx);
self.x2_es2016.transform_expression(expr, ctx);
self.x2_es2016.enter_expression(expr, ctx);
self.x3_es2015.transform_expression(expr);
}

Expand Down Expand Up @@ -260,7 +260,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x1_react.transform_statements(stmts, ctx);
self.x2_es2021.enter_statements(stmts, ctx);
self.x2_es2020.enter_statements(stmts, ctx);
self.x2_es2016.transform_statements(stmts, ctx);
self.x2_es2016.enter_statements(stmts, ctx);
self.x3_es2015.enter_statements(stmts);
}

Expand All @@ -269,7 +269,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x1_react.transform_statements_on_exit(stmts, ctx);
self.x2_es2021.exit_statements(stmts, ctx);
self.x2_es2020.exit_statements(stmts, ctx);
self.x2_es2016.transform_statements_on_exit(stmts, ctx);
self.x2_es2016.exit_statements(stmts, ctx);
self.x3_es2015.exit_statements(stmts);
}

Expand Down

0 comments on commit a9fcf29

Please sign in to comment.