diff --git a/crates/oxc_minifier/src/ast_passes/mod.rs b/crates/oxc_minifier/src/ast_passes/mod.rs index 4d31e84337aa5..ecf15f31c903d 100644 --- a/crates/oxc_minifier/src/ast_passes/mod.rs +++ b/crates/oxc_minifier/src/ast_passes/mod.rs @@ -22,6 +22,8 @@ pub use peephole_substitute_alternate_syntax::PeepholeSubstituteAlternateSyntax; pub use remove_syntax::RemoveSyntax; pub use statement_fusion::StatementFusion; +use crate::CompressOptions; + pub trait CompressorPass<'a>: Traverse<'a> { fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>); } @@ -287,3 +289,49 @@ impl<'a> Traverse<'a> for PeepholeOptimizations { self.x3_peephole_substitute_alternate_syntax.enter_binary_expression(expr, ctx); } } + +pub struct DeadCodeElimination { + x0_remove_syntax: RemoveSyntax, + x1_peephole_fold_constants: PeepholeFoldConstants, + x2_peephole_remove_dead_code: PeepholeRemoveDeadCode, +} + +impl DeadCodeElimination { + pub fn new() -> Self { + Self { + x0_remove_syntax: RemoveSyntax::new(CompressOptions::all_false()), + x1_peephole_fold_constants: PeepholeFoldConstants::new(), + x2_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(), + } + } +} + +impl<'a> CompressorPass<'a> for DeadCodeElimination { + fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) { + traverse_mut_with_ctx(self, program, ctx); + } +} + +impl<'a> Traverse<'a> for DeadCodeElimination { + fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { + self.x2_peephole_remove_dead_code.enter_statement(stmt, ctx); + } + + fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { + self.x2_peephole_remove_dead_code.exit_statement(stmt, ctx); + } + + fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { + self.x2_peephole_remove_dead_code.exit_program(program, ctx); + } + + fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { + self.x2_peephole_remove_dead_code.exit_statements(stmts, ctx); + } + + fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { + self.x0_remove_syntax.exit_expression(expr, ctx); + self.x1_peephole_fold_constants.exit_expression(expr, ctx); + self.x2_peephole_remove_dead_code.exit_expression(expr, ctx); + } +} diff --git a/crates/oxc_minifier/src/compressor.rs b/crates/oxc_minifier/src/compressor.rs index dc1c6def1d8a3..174062ee15136 100644 --- a/crates/oxc_minifier/src/compressor.rs +++ b/crates/oxc_minifier/src/compressor.rs @@ -5,8 +5,8 @@ use oxc_traverse::ReusableTraverseCtx; use crate::{ ast_passes::{ - CollapsePass, LatePeepholeOptimizations, PeepholeFoldConstants, PeepholeOptimizations, - PeepholeRemoveDeadCode, RemoveSyntax, + CollapsePass, DeadCodeElimination, LatePeepholeOptimizations, PeepholeOptimizations, + RemoveSyntax, }, CompressOptions, CompressorPass, }; @@ -44,10 +44,7 @@ impl<'a> Compressor<'a> { pub fn dead_code_elimination(self, program: &mut Program<'a>) { let (symbols, scopes) = SemanticBuilder::new().build(program).semantic.into_symbol_table_and_scope_tree(); - let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator); - RemoveSyntax::new(self.options).build(program, &mut ctx); - PeepholeFoldConstants::new().build(program, &mut ctx); - PeepholeRemoveDeadCode::new().build(program, &mut ctx); + self.dead_code_elimination_with_symbols_and_scopes(symbols, scopes, program); } pub fn dead_code_elimination_with_symbols_and_scopes( @@ -57,8 +54,6 @@ impl<'a> Compressor<'a> { program: &mut Program<'a>, ) { let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator); - RemoveSyntax::new(self.options).build(program, &mut ctx); - PeepholeFoldConstants::new().build(program, &mut ctx); - PeepholeRemoveDeadCode::new().build(program, &mut ctx); + DeadCodeElimination::new().build(program, &mut ctx); } }