Skip to content

Commit

Permalink
feat(minifier): minimize logical exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 authored and Boshen committed Jan 1, 2025
1 parent 4c2059a commit 18fd030
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
65 changes: 58 additions & 7 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Some(folded_expr) = match expr {
Expression::UnaryExpression(e) => Self::try_minimize_not(e, ctx),
Expression::LogicalExpression(logical_expr) => {
Self::try_minimize_logical(logical_expr, ctx)
}
Expression::ConditionalExpression(conditional_expr) => {
Self::try_minimize_conditional(conditional_expr, ctx)
}
Expand Down Expand Up @@ -257,6 +260,49 @@ impl<'a> PeepholeMinimizeConditions {
}
}

fn try_minimize_logical(
expr: &mut LogicalExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
// `a && true` -> `a`
// `a && false` -> `false`
if expr.operator == LogicalOperator::And {
if let (
Expression::Identifier(test_ident),
Expression::BooleanLiteral(consequent_lit),
) = (&expr.left, &expr.right)
{
if consequent_lit.value {
return Some(ctx.ast.move_expression(&mut expr.left));
}
if ctx.scopes().find_binding(ctx.current_scope_id(), &test_ident.name).is_some() {
return Some(ctx.ast.expression_boolean_literal(expr.span, false));
}
return None;
}
}

// `a || true` -> `true`
// `a || false` -> `a`
if expr.operator == LogicalOperator::Or {
if let (
Expression::Identifier(test_ident),
Expression::BooleanLiteral(consequent_lit),
) = (&expr.left, &expr.right)
{
if ctx.scopes().find_binding(ctx.current_scope_id(), &test_ident.name).is_some() {
if !consequent_lit.value {
return Some(ctx.ast.move_expression(&mut expr.left));
}
return Some(ctx.ast.expression_boolean_literal(expr.span, true));
}
return None;
}
}

None
}

fn try_minimize_conditional(
expr: &mut ConditionalExpression<'a>,
ctx: &mut TraverseCtx<'a>,
Expand Down Expand Up @@ -591,14 +637,19 @@ mod test {
#[test]
#[ignore]
fn test_minimize_expr_condition() {
fold("(x ? true : false) && y()", "x&&y()");
fold("(x ? false : true) && y()", "(!x)&&y()");
fold("(x ? true : y) && y()", "(x || y)&&y()");
fold("(x ? y : false) && y()", "(x && y)&&y()");
fold("(x ? true : false) && y()", "!!x && y()");
fold("(x ? false : true) && y()", "!x && y()");
fold("(x ? true : y) && y()", "(x || y) && y()");
fold("(x ? y : false) && y()", "(x && y) && y()");
fold("var x; (x && true) && y()", "var x; x && y()");
fold("var x; (x && false) && y()", "var x; false && y()");
fold("(x && true) && y()", "x && y()");
fold("(x && false) && y()", "0&&y()");
fold("(x || true) && y()", "1&&y()");
fold("(x || false) && y()", "x&&y()");
fold("(x && false) && y()", "x && false && y()");
fold("var x; (x || true) && y()", "var x; true && y()");
fold("var x; (x || false) && y()", "var x; x && y()");

fold_same("(x || true) && y()");
fold("(x || false) && y()", "x && y()");
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Original | minified | minified | gzip | gzip | Fixture

1.25 MB | 653.18 kB | 646.76 kB | 163.57 kB | 163.73 kB | three.js

2.14 MB | 726.72 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js
2.14 MB | 726.71 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.13 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.12 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 493.04 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 493.02 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.11 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.08 kB | 915.50 kB | typescript.js

0 comments on commit 18fd030

Please sign in to comment.