Skip to content
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
7 changes: 5 additions & 2 deletions crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use oxc_traverse::ReusableTraverseCtx;

use crate::{
CompressOptions,
ctx::MinifierState,
peephole::{
DeadCodeElimination, LatePeepholeOptimizations, Normalize, NormalizeOptions,
PeepholeOptimizations,
Expand All @@ -27,7 +28,8 @@ impl<'a> Compressor<'a> {
}

pub fn build_with_scoping(self, scoping: Scoping, program: &mut Program<'a>) {
let mut ctx = ReusableTraverseCtx::new(scoping, self.allocator);
let state = MinifierState::default();
let mut ctx = ReusableTraverseCtx::new(state, scoping, self.allocator);
let normalize_options =
NormalizeOptions { convert_while_to_fors: true, convert_const_to_let: true };
Normalize::new(normalize_options, self.options).build(program, &mut ctx);
Expand All @@ -42,7 +44,8 @@ impl<'a> Compressor<'a> {
}

pub fn dead_code_elimination_with_scoping(self, scoping: Scoping, program: &mut Program<'a>) {
let mut ctx = ReusableTraverseCtx::new(scoping, self.allocator);
let state = MinifierState::default();
let mut ctx = ReusableTraverseCtx::new(state, scoping, self.allocator);
let normalize_options =
NormalizeOptions { convert_while_to_fors: false, convert_const_to_let: false };
Normalize::new(normalize_options, self.options).build(program, &mut ctx);
Expand Down
10 changes: 8 additions & 2 deletions crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{marker::PhantomData, ops::Deref};

use oxc_ast::{AstBuilder, ast::*};
use oxc_ecmascript::constant_evaluation::{
Expand All @@ -7,7 +7,13 @@ use oxc_ecmascript::constant_evaluation::{
use oxc_ecmascript::side_effects::{MayHaveSideEffects, PropertyReadSideEffects};
use oxc_semantic::{IsGlobalReference, Scoping};
use oxc_span::format_atom;
use oxc_traverse::TraverseCtx;

#[derive(Default)]
pub struct MinifierState<'a> {
data: PhantomData<&'a ()>,
}

pub type TraverseCtx<'a> = oxc_traverse::TraverseCtx<'a, MinifierState<'a>>;

#[derive(Clone, Copy)]
pub struct Ctx<'a, 'b>(pub &'b TraverseCtx<'a>);
Expand Down
37 changes: 28 additions & 9 deletions crates/oxc_minifier/src/peephole/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_data_structures::stack::NonEmptyStack;
use oxc_syntax::{es_target::ESTarget, scope::ScopeId};
use oxc_traverse::{ReusableTraverseCtx, Traverse, TraverseCtx, traverse_mut_with_ctx};
use oxc_traverse::{ReusableTraverseCtx, Traverse, traverse_mut_with_ctx};

use crate::{ctx::Ctx, options::CompressOptionsKeepNames};
use crate::{
ctx::{Ctx, MinifierState, TraverseCtx},
options::CompressOptionsKeepNames,
};

pub use self::normalize::{Normalize, NormalizeOptions};

Expand Down Expand Up @@ -64,11 +67,19 @@ impl<'a> PeepholeOptimizations {
}
}

pub fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
pub fn build(
&mut self,
program: &mut Program<'a>,
ctx: &mut ReusableTraverseCtx<'a, MinifierState<'a>>,
) {
traverse_mut_with_ctx(self, program, ctx);
}

pub fn run_in_loop(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
pub fn run_in_loop(
&mut self,
program: &mut Program<'a>,
ctx: &mut ReusableTraverseCtx<'a, MinifierState<'a>>,
) {
loop {
self.build(program, ctx);
if self.functions_changed.is_empty() {
Expand Down Expand Up @@ -137,7 +148,7 @@ impl<'a> PeepholeOptimizations {
}
}

impl<'a> Traverse<'a> for PeepholeOptimizations {
impl<'a> Traverse<'a, MinifierState<'a>> for PeepholeOptimizations {
fn enter_program(&mut self, program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
self.enter_program_or_function(program.scope_id());
}
Expand Down Expand Up @@ -391,12 +402,16 @@ impl<'a> LatePeepholeOptimizations {
Self { target }
}

pub fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
pub fn build(
&mut self,
program: &mut Program<'a>,
ctx: &mut ReusableTraverseCtx<'a, MinifierState<'a>>,
) {
traverse_mut_with_ctx(self, program, ctx);
}
}

impl<'a> Traverse<'a> for LatePeepholeOptimizations {
impl<'a> Traverse<'a, MinifierState<'a>> for LatePeepholeOptimizations {
fn exit_member_expression(
&mut self,
expr: &mut MemberExpression<'a>,
Expand Down Expand Up @@ -440,12 +455,16 @@ impl<'a> DeadCodeElimination {
}
}

pub fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
pub fn build(
&mut self,
program: &mut Program<'a>,
ctx: &mut ReusableTraverseCtx<'a, MinifierState<'a>>,
) {
traverse_mut_with_ctx(self, program, ctx);
}
}

impl<'a> Traverse<'a> for DeadCodeElimination {
impl<'a> Traverse<'a, MinifierState<'a>> for DeadCodeElimination {
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let mut state = State::default();
self.inner.remove_dead_code_exit_statement(stmt, &mut state, Ctx(ctx));
Expand Down
15 changes: 11 additions & 4 deletions crates/oxc_minifier/src/peephole/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use oxc_ecmascript::constant_evaluation::{DetermineValueType, ValueType};
use oxc_semantic::IsGlobalReference;
use oxc_span::GetSpan;
use oxc_syntax::scope::ScopeFlags;
use oxc_traverse::{Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx, traverse_mut_with_ctx};
use oxc_traverse::{Ancestor, ReusableTraverseCtx, Traverse, traverse_mut_with_ctx};

use crate::{CompressOptions, ctx::Ctx};
use crate::{
CompressOptions,
ctx::{Ctx, MinifierState, TraverseCtx},
};

#[derive(Default)]
pub struct NormalizeOptions {
Expand Down Expand Up @@ -39,12 +42,16 @@ pub struct Normalize {
}

impl<'a> Normalize {
pub fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
pub fn build(
&mut self,
program: &mut Program<'a>,
ctx: &mut ReusableTraverseCtx<'a, MinifierState<'a>>,
) {
traverse_mut_with_ctx(self, program, ctx);
}
}

impl<'a> Traverse<'a> for Normalize {
impl<'a> Traverse<'a, MinifierState<'a>> for Normalize {
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, _ctx: &mut TraverseCtx<'a>) {
stmts.retain(|stmt| {
!(matches!(stmt, Statement::EmptyStatement(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ use oxc_syntax::{
scope::{ScopeFlags, ScopeId},
symbol::SymbolFlags,
};
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse};

use crate::{EnvOptions, utils::ast_builder::wrap_expression_in_arrow_function_iife};
use crate::{context::TraverseCtx, state::TransformState};

type FxIndexMap<K, V> = IndexMap<K, V, FxBuildHasher>;

Expand Down Expand Up @@ -172,7 +173,7 @@ impl ArrowFunctionConverter<'_> {
}
}

impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> {
impl<'a> Traverse<'a, TransformState<'a>> for ArrowFunctionConverter<'a> {
// Note: No visitors for `TSModuleBlock` because `this` is not legal in TS module blocks.
// <https://www.typescriptlang.org/play/?#code/HYQwtgpgzgDiDGEAEAxA9mpBvAsAKCSXjWCgBckANJAXiQAoBKWgPiTIAsBLKAbnwC++fGDQATAK4AbZACEQAJ2z5CxUhWp0mrdtz6D8QA>

Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_transformer/src/common/computed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

use oxc_ast::ast::Expression;
use oxc_semantic::SymbolFlags;
use oxc_traverse::TraverseCtx;

use crate::{TransformCtx, utils::ast_builder::create_assignment};
use crate::{
context::{TransformCtx, TraverseCtx},
utils::ast_builder::create_assignment,
};

impl<'a> TransformCtx<'a> {
/// Check if temp var is required for `key`.
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/common/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use oxc_allocator::CloneIn;
use oxc_ast::ast::{AssignmentOperator, Expression};
use oxc_span::SPAN;
use oxc_syntax::reference::ReferenceFlags;
use oxc_traverse::{BoundIdentifier, TraverseCtx};
use oxc_traverse::BoundIdentifier;

use crate::TransformCtx;
use crate::context::{TransformCtx, TraverseCtx};

impl<'a> TransformCtx<'a> {
/// Duplicate expression to be used twice.
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ use oxc_ast::{
};
use oxc_semantic::{ReferenceFlags, SymbolFlags};
use oxc_span::{Atom, SPAN, Span};
use oxc_traverse::{BoundIdentifier, TraverseCtx};
use oxc_traverse::BoundIdentifier;

use crate::TransformCtx;
use crate::context::{TransformCtx, TraverseCtx};

/// Defines the mode for loading helper functions.
#[derive(Default, Clone, Copy, Debug, Deserialize)]
Expand Down
10 changes: 7 additions & 3 deletions crates/oxc_transformer/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
use arrow_function_converter::ArrowFunctionConverter;
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use oxc_traverse::Traverse;

use crate::{EnvOptions, TransformCtx};
use crate::{
EnvOptions,
context::{TransformCtx, TraverseCtx},
state::TransformState,
};

pub mod arrow_function_converter;
mod computed_key;
Expand Down Expand Up @@ -41,7 +45,7 @@ impl<'a, 'ctx> Common<'a, 'ctx> {
}
}

impl<'a> Traverse<'a> for Common<'a, '_> {
impl<'a> Traverse<'a, TransformState<'a>> for Common<'a, '_> {
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.module_imports.exit_program(program, ctx);
self.var_declarations.exit_program(program, ctx);
Expand Down
9 changes: 6 additions & 3 deletions crates/oxc_transformer/src/common/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ use oxc_ast::{NONE, ast::*};
use oxc_semantic::ReferenceFlags;
use oxc_span::{Atom, SPAN};
use oxc_syntax::symbol::SymbolId;
use oxc_traverse::{BoundIdentifier, Traverse, TraverseCtx};
use oxc_traverse::{BoundIdentifier, Traverse};

use crate::TransformCtx;
use crate::{
context::{TransformCtx, TraverseCtx},
state::TransformState,
};

pub struct ModuleImports<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,
Expand All @@ -54,7 +57,7 @@ impl<'a, 'ctx> ModuleImports<'a, 'ctx> {
}
}

impl<'a> Traverse<'a> for ModuleImports<'a, '_> {
impl<'a> Traverse<'a, TransformState<'a>> for ModuleImports<'a, '_> {
fn exit_program(&mut self, _program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.ctx.module_imports.insert_into_program(self.ctx, ctx);
}
Expand Down
9 changes: 6 additions & 3 deletions crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ use rustc_hash::FxHashMap;

use oxc_allocator::{Address, GetAddress, Vec as ArenaVec};
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use oxc_traverse::Traverse;

use crate::TransformCtx;
use crate::{
context::{TransformCtx, TraverseCtx},
state::TransformState,
};

/// Transform that inserts any statements which have been requested insertion via `StatementInjectorStore`
pub struct StatementInjector<'a, 'ctx> {
Expand All @@ -33,7 +36,7 @@ impl<'a, 'ctx> StatementInjector<'a, 'ctx> {
}
}

impl<'a> Traverse<'a> for StatementInjector<'a, '_> {
impl<'a> Traverse<'a, TransformState<'a>> for StatementInjector<'a, '_> {
fn exit_statements(
&mut self,
statements: &mut ArenaVec<'a, Statement<'a>>,
Expand Down
9 changes: 6 additions & 3 deletions crates/oxc_transformer/src/common/top_level_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
use std::cell::RefCell;

use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use oxc_traverse::Traverse;

use crate::TransformCtx;
use crate::{
context::{TransformCtx, TraverseCtx},
state::TransformState,
};

/// Transform that inserts any statements which have been requested insertion via `TopLevelStatementsStore`
/// to top of the program.
Expand All @@ -33,7 +36,7 @@ impl<'a, 'ctx> TopLevelStatements<'a, 'ctx> {
}
}

impl<'a> Traverse<'a> for TopLevelStatements<'a, '_> {
impl<'a> Traverse<'a, TransformState<'a>> for TopLevelStatements<'a, '_> {
fn exit_program(&mut self, program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
self.ctx.top_level_statements.insert_into_program(program);
}
Expand Down
11 changes: 6 additions & 5 deletions crates/oxc_transformer/src/common/var_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ use oxc_allocator::Vec as ArenaVec;
use oxc_ast::ast::*;
use oxc_data_structures::stack::SparseStack;
use oxc_span::SPAN;
use oxc_traverse::{
Ancestor, BoundIdentifier, Traverse, TraverseCtx, ast_operations::GatherNodeParts,
};
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, ast_operations::GatherNodeParts};

use crate::TransformCtx;
use crate::{
context::{TransformCtx, TraverseCtx},
state::TransformState,
};

/// Transform that maintains the stack of `Vec<VariableDeclarator>`s, and adds a `var` statement
/// to top of a statement block if another transform has requested that.
Expand All @@ -40,7 +41,7 @@ impl<'a, 'ctx> VarDeclarations<'a, 'ctx> {
}
}

impl<'a> Traverse<'a> for VarDeclarations<'a, '_> {
impl<'a> Traverse<'a, TransformState<'a>> for VarDeclarations<'a, '_> {
fn enter_statements(
&mut self,
_stmts: &mut ArenaVec<'a, Statement<'a>>,
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ use crate::{
statement_injector::StatementInjectorStore, top_level_statements::TopLevelStatementsStore,
var_declarations::VarDeclarationsStore,
},
state::TransformState,
};

pub type TraverseCtx<'a> = oxc_traverse::TraverseCtx<'a, TransformState<'a>>;

pub struct TransformCtx<'a> {
errors: RefCell<Vec<OxcDiagnostic>>,

Expand Down
11 changes: 8 additions & 3 deletions crates/oxc_transformer/src/decorator/legacy/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,14 @@ use oxc_allocator::{Box as ArenaBox, TakeIn};
use oxc_ast::ast::*;
use oxc_semantic::ReferenceFlags;
use oxc_span::{ContentEq, SPAN};
use oxc_traverse::{MaybeBoundIdentifier, Traverse, TraverseCtx};
use oxc_traverse::{MaybeBoundIdentifier, Traverse};

use crate::{Helper, context::TransformCtx, utils::ast_builder::create_property_access};
use crate::{
Helper,
context::{TransformCtx, TraverseCtx},
state::TransformState,
utils::ast_builder::create_property_access,
};

pub struct LegacyDecoratorMetadata<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,
Expand All @@ -105,7 +110,7 @@ impl<'a, 'ctx> LegacyDecoratorMetadata<'a, 'ctx> {
}
}

impl<'a> Traverse<'a> for LegacyDecoratorMetadata<'a, '_> {
impl<'a> Traverse<'a, TransformState<'a>> for LegacyDecoratorMetadata<'a, '_> {
fn enter_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
if class.is_expression() || class.declare {
return;
Expand Down
Loading
Loading