mirror of
https://github.com/danbulant/oxc
synced 2026-05-20 04:38:54 +00:00
feat(minifier): minimize logical exprs (#8209)
This commit is contained in:
parent
cd349a312d
commit
cd274eeb02
2 changed files with 62 additions and 9 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -257,6 +260,51 @@ 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 consequent_lit.value {
|
||||
if ctx.scopes().find_binding(ctx.current_scope_id(), &test_ident.name).is_some()
|
||||
{
|
||||
return Some(ctx.ast.expression_boolean_literal(expr.span, true));
|
||||
}
|
||||
} else {
|
||||
return Some(ctx.ast.move_expression(&mut expr.left));
|
||||
}
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn try_minimize_conditional(
|
||||
expr: &mut ConditionalExpression<'a>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
|
|
@ -669,10 +717,15 @@ mod test {
|
|||
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) && y()", "x && y()");
|
||||
// fold("(x && false) && y()", "0&&y()");
|
||||
// fold("(x || true) && y()", "1&&y()");
|
||||
// fold("(x || false) && y()", "x&&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()", "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]
|
||||
|
|
|
|||
|
|
@ -15,13 +15,13 @@ Original | minified | minified | gzip | gzip | Fixture
|
|||
|
||||
1.01 MB | 460.75 kB | 458.89 kB | 126.88 kB | 126.71 kB | bundle.min.js
|
||||
|
||||
1.25 MB | 653.17 kB | 646.76 kB | 163.58 kB | 163.73 kB | three.js
|
||||
1.25 MB | 653.17 kB | 646.76 kB | 163.57 kB | 163.73 kB | three.js
|
||||
|
||||
2.14 MB | 726.71 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js
|
||||
2.14 MB | 726.70 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js
|
||||
|
||||
3.20 MB | 1.01 MB | 1.01 MB | 332.14 kB | 331.56 kB | echarts.js
|
||||
3.20 MB | 1.01 MB | 1.01 MB | 332.13 kB | 331.56 kB | echarts.js
|
||||
|
||||
6.69 MB | 2.32 MB | 2.31 MB | 493.00 kB | 488.28 kB | antd.js
|
||||
6.69 MB | 2.32 MB | 2.31 MB | 492.99 kB | 488.28 kB | antd.js
|
||||
|
||||
10.95 MB | 3.51 MB | 3.49 MB | 910.09 kB | 915.50 kB | typescript.js
|
||||
10.95 MB | 3.51 MB | 3.49 MB | 910.06 kB | 915.50 kB | typescript.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue