Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transformer/es2016): move all entry points to implementation of Traverse trait #5085

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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