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 d5ca1697dd2da..908f019afe18d 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 @@ -33,9 +33,7 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode { Statement::BlockStatement(s) => Self::try_optimize_block(s, ctx), Statement::IfStatement(s) => self.try_fold_if(s, ctx), Statement::ForStatement(s) => self.try_fold_for(s, ctx), - Statement::ExpressionStatement(s) => { - Self::try_fold_iife(s, ctx).or_else(|| Self::try_fold_expression_stmt(s, ctx)) - } + Statement::ExpressionStatement(s) => Self::try_fold_iife(s, ctx), Statement::TryStatement(s) => Self::try_fold_try(s, ctx), Statement::LabeledStatement(s) => Self::try_fold_labeled(s, ctx), _ => None, @@ -43,6 +41,13 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode { *stmt = new_stmt; self.changed = true; } + + if let Statement::ExpressionStatement(s) = stmt { + if let Some(new_stmt) = Self::try_fold_expression_stmt(s, ctx) { + *stmt = new_stmt; + self.changed = true; + } + } } fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { @@ -746,6 +751,8 @@ mod test { fn test_fold_if_statement() { test("if (foo) {}", "foo"); test("if (foo) {} else {}", "foo"); + test("if (false) {}", ""); + test("if (true) {}", ""); } #[test]