mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(transformer/es2020): move all entry points to implementation of Traverse trait (#4973)
part of #4881
This commit is contained in:
parent
72ff2c67b9
commit
9df2f80cab
3 changed files with 39 additions and 39 deletions
|
|
@ -5,7 +5,7 @@ pub use nullish_coalescing_operator::NullishCoalescingOperator;
|
|||
pub use options::ES2020Options;
|
||||
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;
|
||||
|
|
@ -27,30 +27,32 @@ impl<'a> ES2020<'a> {
|
|||
options,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transform_statements(
|
||||
impl<'a> Traverse<'a> for ES2020<'a> {
|
||||
fn enter_statements(
|
||||
&mut self,
|
||||
statements: &mut Vec<'a, Statement<'a>>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
) {
|
||||
if self.options.nullish_coalescing_operator {
|
||||
self.nullish_coalescing_operator.transform_statements(statements, ctx);
|
||||
self.nullish_coalescing_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.nullish_coalescing_operator {
|
||||
self.nullish_coalescing_operator.transform_statements_on_exit(statements, ctx);
|
||||
self.nullish_coalescing_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.nullish_coalescing_operator {
|
||||
self.nullish_coalescing_operator.transform_expression(expr, ctx);
|
||||
self.nullish_coalescing_operator.enter_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@
|
|||
use std::cell::Cell;
|
||||
|
||||
use oxc_semantic::{ReferenceFlag, SymbolFlags};
|
||||
use oxc_traverse::TraverseCtx;
|
||||
use oxc_traverse::{Traverse, TraverseCtx};
|
||||
|
||||
use oxc_allocator::{CloneIn, Vec};
|
||||
use oxc_ast::ast::*;
|
||||
|
|
@ -69,33 +69,6 @@ impl<'a> NullishCoalescingOperator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn transform_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(
|
||||
&mut self,
|
||||
statements: &mut Vec<'a, Statement<'a>>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
) {
|
||||
if let Some(declarations) = self.var_declarations.pop() {
|
||||
if declarations.is_empty() {
|
||||
return;
|
||||
}
|
||||
let variable = ctx.ast.alloc_variable_declaration(
|
||||
SPAN,
|
||||
VariableDeclarationKind::Var,
|
||||
declarations,
|
||||
false,
|
||||
);
|
||||
statements.insert(0, Statement::VariableDeclaration(variable));
|
||||
}
|
||||
}
|
||||
|
||||
fn create_new_var_with_expression(
|
||||
&mut self,
|
||||
expr: &Expression<'a>,
|
||||
|
|
@ -130,8 +103,33 @@ impl<'a> NullishCoalescingOperator<'a> {
|
|||
|
||||
ctx.create_reference_id(SPAN, symbol_name, Some(symbol_id), ReferenceFlag::Read)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transform_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
impl<'a> Traverse<'a> for NullishCoalescingOperator<'a> {
|
||||
fn enter_statements(&mut self, _stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.var_declarations.push(ctx.ast.vec());
|
||||
}
|
||||
|
||||
fn exit_statements(
|
||||
&mut self,
|
||||
statements: &mut Vec<'a, Statement<'a>>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
) {
|
||||
if let Some(declarations) = self.var_declarations.pop() {
|
||||
if declarations.is_empty() {
|
||||
return;
|
||||
}
|
||||
let variable = ctx.ast.alloc_variable_declaration(
|
||||
SPAN,
|
||||
VariableDeclarationKind::Var,
|
||||
declarations,
|
||||
false,
|
||||
);
|
||||
statements.insert(0, Statement::VariableDeclaration(variable));
|
||||
}
|
||||
}
|
||||
|
||||
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
// left ?? right
|
||||
if !matches!(expr, Expression::LogicalExpression(logical_expr) if logical_expr.operator == LogicalOperator::Coalesce)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
|
|||
self.x0_typescript.transform_expression(expr);
|
||||
self.x1_react.transform_expression(expr, ctx);
|
||||
self.x2_es2021.transform_expression(expr, ctx);
|
||||
self.x2_es2020.transform_expression(expr, ctx);
|
||||
self.x2_es2020.enter_expression(expr, ctx);
|
||||
self.x2_es2016.transform_expression(expr, ctx);
|
||||
self.x3_es2015.transform_expression(expr);
|
||||
}
|
||||
|
|
@ -269,7 +269,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
|
|||
self.x0_typescript.transform_statements(stmts);
|
||||
self.x1_react.transform_statements(stmts, ctx);
|
||||
self.x2_es2021.transform_statements(stmts, ctx);
|
||||
self.x2_es2020.transform_statements(stmts, ctx);
|
||||
self.x2_es2020.enter_statements(stmts, ctx);
|
||||
self.x2_es2016.transform_statements(stmts, ctx);
|
||||
self.x3_es2015.enter_statements(stmts);
|
||||
}
|
||||
|
|
@ -278,7 +278,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
|
|||
self.x0_typescript.transform_statements_on_exit(stmts, ctx);
|
||||
self.x1_react.transform_statements_on_exit(stmts, ctx);
|
||||
self.x2_es2021.transform_statements_on_exit(stmts, ctx);
|
||||
self.x2_es2020.transform_statements_on_exit(stmts, ctx);
|
||||
self.x2_es2020.exit_statements(stmts, ctx);
|
||||
self.x2_es2016.transform_statements_on_exit(stmts, ctx);
|
||||
self.x3_es2015.exit_statements(stmts);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue