mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(minifier): remove if(false){} in a single pass (#8421)
This commit is contained in:
parent
d4ca8d4f5c
commit
0e7bab887f
1 changed files with 10 additions and 3 deletions
|
|
@ -33,9 +33,7 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
|
||||||
Statement::BlockStatement(s) => Self::try_optimize_block(s, ctx),
|
Statement::BlockStatement(s) => Self::try_optimize_block(s, ctx),
|
||||||
Statement::IfStatement(s) => self.try_fold_if(s, ctx),
|
Statement::IfStatement(s) => self.try_fold_if(s, ctx),
|
||||||
Statement::ForStatement(s) => self.try_fold_for(s, ctx),
|
Statement::ForStatement(s) => self.try_fold_for(s, ctx),
|
||||||
Statement::ExpressionStatement(s) => {
|
Statement::ExpressionStatement(s) => Self::try_fold_iife(s, ctx),
|
||||||
Self::try_fold_iife(s, ctx).or_else(|| Self::try_fold_expression_stmt(s, ctx))
|
|
||||||
}
|
|
||||||
Statement::TryStatement(s) => Self::try_fold_try(s, ctx),
|
Statement::TryStatement(s) => Self::try_fold_try(s, ctx),
|
||||||
Statement::LabeledStatement(s) => Self::try_fold_labeled(s, ctx),
|
Statement::LabeledStatement(s) => Self::try_fold_labeled(s, ctx),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
@ -43,6 +41,13 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
|
||||||
*stmt = new_stmt;
|
*stmt = new_stmt;
|
||||||
self.changed = true;
|
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>) {
|
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() {
|
fn test_fold_if_statement() {
|
||||||
test("if (foo) {}", "foo");
|
test("if (foo) {}", "foo");
|
||||||
test("if (foo) {} else {}", "foo");
|
test("if (foo) {} else {}", "foo");
|
||||||
|
test("if (false) {}", "");
|
||||||
|
test("if (true) {}", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue