Skip to content

Commit

Permalink
feat(minifier): remove if(false){} in a single pass (#8421)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 10, 2025
1 parent d4ca8d4 commit 0e7bab8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ 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,
} {
*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>) {
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 0e7bab8

Please sign in to comment.