diff --git a/crates/oxc_transformer/src/es2016/exponentiation_operator.rs b/crates/oxc_transformer/src/es2016/exponentiation_operator.rs index 39dbd68abec68..70a8c24010655 100644 --- a/crates/oxc_transformer/src/es2016/exponentiation_operator.rs +++ b/crates/oxc_transformer/src/es2016/exponentiation_operator.rs @@ -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; @@ -62,27 +62,10 @@ 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>, @@ -90,7 +73,7 @@ impl<'a> ExponentiationOperator<'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>, @@ -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 { @@ -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( diff --git a/crates/oxc_transformer/src/es2016/mod.rs b/crates/oxc_transformer/src/es2016/mod.rs index a19d894546779..9ec05ea231ee8 100644 --- a/crates/oxc_transformer/src/es2016/mod.rs +++ b/crates/oxc_transformer/src/es2016/mod.rs @@ -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; @@ -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); } } } diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 3d7f98a279dd8..5c45690b74b90 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -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); } @@ -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); } @@ -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); }