mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(minifier): dce all conditional expressions (#4135)
This commit is contained in:
parent
cb1af043b8
commit
c6c16a5fc9
2 changed files with 11 additions and 9 deletions
|
|
@ -44,20 +44,16 @@ impl<'a> RemoveDeadCode<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_conditional(&mut self, stmt: &mut Statement<'a>) {
|
pub fn remove_conditional(&mut self, expr: &mut Expression<'a>) {
|
||||||
let Statement::ExpressionStatement(expression_stmt) = stmt else { return };
|
let Expression::ConditionalExpression(conditional_expr) = expr else {
|
||||||
let Expression::ConditionalExpression(conditional_expr) = &mut expression_stmt.expression
|
|
||||||
else {
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
match self.test_expression(&mut conditional_expr.test) {
|
match self.test_expression(&mut conditional_expr.test) {
|
||||||
Some(true) => {
|
Some(true) => {
|
||||||
expression_stmt.expression =
|
*expr = self.ast.move_expression(&mut conditional_expr.consequent);
|
||||||
self.ast.move_expression(&mut conditional_expr.consequent);
|
|
||||||
}
|
}
|
||||||
Some(false) => {
|
Some(false) => {
|
||||||
expression_stmt.expression =
|
*expr = self.ast.move_expression(&mut conditional_expr.alternate);
|
||||||
self.ast.move_expression(&mut conditional_expr.alternate);
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -67,7 +63,10 @@ impl<'a> RemoveDeadCode<'a> {
|
||||||
impl<'a> VisitMut<'a> for RemoveDeadCode<'a> {
|
impl<'a> VisitMut<'a> for RemoveDeadCode<'a> {
|
||||||
fn visit_statement(&mut self, stmt: &mut Statement<'a>) {
|
fn visit_statement(&mut self, stmt: &mut Statement<'a>) {
|
||||||
self.remove_if(stmt);
|
self.remove_if(stmt);
|
||||||
self.remove_conditional(stmt);
|
|
||||||
walk_mut::walk_statement(self, stmt);
|
walk_mut::walk_statement(self, stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_expression(&mut self, expr: &mut Expression<'a>) {
|
||||||
|
self.remove_conditional(expr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,9 @@ fn remove_dead_code() {
|
||||||
test("!!false ? foo : bar;", "bar");
|
test("!!false ? foo : bar;", "bar");
|
||||||
test("!!true ? foo : bar;", "foo");
|
test("!!true ? foo : bar;", "foo");
|
||||||
|
|
||||||
|
test("const foo = true ? A : B", "const foo=A");
|
||||||
|
test("const foo = false ? A : B", "const foo=B");
|
||||||
|
|
||||||
// Shadowed `undefined` as a variable should not be erased.
|
// Shadowed `undefined` as a variable should not be erased.
|
||||||
test(
|
test(
|
||||||
"function foo(undefined) { if (!undefined) { } }",
|
"function foo(undefined) { if (!undefined) { } }",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue