feat(minifier): remove if(false){} in a single pass (#8421)

This commit is contained in:
Boshen 2025-01-10 16:22:09 +00:00
parent d4ca8d4f5c
commit 0e7bab887f

View file

@ -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]