diff --git a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs index 55a1dd10f..b80a526a5 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs @@ -24,7 +24,8 @@ impl<'a> CompressorPass<'a> for PeepholeRemoveDeadCode { } impl<'a> Traverse<'a> for PeepholeRemoveDeadCode { - fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { + fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { + self.compress_block(stmt, Ctx(ctx)); let ctx = Ctx(ctx); if let Some(new_stmt) = match stmt { Statement::IfStatement(if_stmt) => self.try_fold_if(if_stmt, ctx), @@ -40,10 +41,6 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode { } } - fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { - self.compress_block(stmt, Ctx(ctx)); - } - fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { if stmts.iter().any(|stmt| matches!(stmt, Statement::EmptyStatement(_))) { stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_))); @@ -159,22 +156,26 @@ impl<'a, 'b> PeepholeRemoveDeadCode { ctx: Ctx<'a, 'b>, ) -> Option> { // Descend and remove `else` blocks first. - if let Some(Statement::IfStatement(alternate)) = &mut if_stmt.alternate { - if let Some(new_stmt) = self.try_fold_if(alternate, ctx) { - if matches!(new_stmt, Statement::EmptyStatement(_)) { - if_stmt.alternate = None; + match &mut if_stmt.alternate { + Some(Statement::IfStatement(alternate)) => { + if let Some(new_stmt) = self.try_fold_if(alternate, ctx) { + if matches!(new_stmt, Statement::EmptyStatement(_)) { + if_stmt.alternate = None; + } else { + if_stmt.alternate = Some(new_stmt); + } self.changed = true; - } else { - if_stmt.alternate = Some(new_stmt); } } + Some(Statement::EmptyStatement(_)) => { + if_stmt.alternate = None; + self.changed = true; + } + _ => {} } match ctx.get_boolean_value(&if_stmt.test) { - Some(true) => { - // self.changed = true; - Some(ctx.ast.move_statement(&mut if_stmt.consequent)) - } + Some(true) => Some(ctx.ast.move_statement(&mut if_stmt.consequent)), Some(false) => { Some(if let Some(alternate) = &mut if_stmt.alternate { ctx.ast.move_statement(alternate) @@ -186,7 +187,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode { .get_variable_declaration_statement() .unwrap_or_else(|| ctx.ast.statement_empty(SPAN)) }) - // self.changed = true; } None => None, } diff --git a/crates/oxc_minifier/src/lib.rs b/crates/oxc_minifier/src/lib.rs index 67f92ea9f..f523cb481 100644 --- a/crates/oxc_minifier/src/lib.rs +++ b/crates/oxc_minifier/src/lib.rs @@ -13,9 +13,10 @@ use oxc_allocator::Allocator; use oxc_ast::ast::Program; use oxc_mangler::Mangler; -pub use crate::{ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions}; pub use oxc_mangler::MangleOptions; +pub use crate::{ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions}; + #[derive(Debug, Clone, Copy)] pub struct MinifierOptions { pub mangle: Option,