mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(minifier): handle conditional expr with boolean lit (#8206)
This commit is contained in:
parent
4c2059af29
commit
3b4501183c
2 changed files with 45 additions and 14 deletions
|
|
@ -277,7 +277,7 @@ impl<'a> PeepholeMinimizeConditions {
|
|||
}
|
||||
}
|
||||
|
||||
// `!x ? foo() : bar()` -> `x ? bar() : foo()`
|
||||
// `!a ? b() : c()` -> `a ? c() : b()`
|
||||
if let Expression::UnaryExpression(test_expr) = &mut expr.test {
|
||||
if test_expr.operator.is_not() {
|
||||
let test = ctx.ast.move_expression(&mut test_expr.argument);
|
||||
|
|
@ -289,6 +289,38 @@ impl<'a> PeepholeMinimizeConditions {
|
|||
}
|
||||
}
|
||||
|
||||
// `a ? false : true` -> `!a`
|
||||
// `a ? true : false` -> `!!a`
|
||||
if let (
|
||||
Expression::Identifier(_),
|
||||
Expression::BooleanLiteral(consequent_lit),
|
||||
Expression::BooleanLiteral(alternate_lit),
|
||||
) = (&expr.test, &expr.consequent, &expr.alternate)
|
||||
{
|
||||
match (consequent_lit.value, alternate_lit.value) {
|
||||
(false, true) => {
|
||||
let ident = ctx.ast.move_expression(&mut expr.test);
|
||||
return Some(ctx.ast.expression_unary(
|
||||
expr.span,
|
||||
UnaryOperator::LogicalNot,
|
||||
ident,
|
||||
));
|
||||
}
|
||||
(true, false) => {
|
||||
let ident = ctx.ast.move_expression(&mut expr.test);
|
||||
return Some(ctx.ast.expression_unary(
|
||||
expr.span,
|
||||
UnaryOperator::LogicalNot,
|
||||
ctx.ast.expression_unary(expr.span, UnaryOperator::LogicalNot, ident),
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// `x ? true : y` -> `x || y`
|
||||
// TODO
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -589,16 +621,15 @@ 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) && y()", "x && y()");
|
||||
fold("(x && false) && y()", "0&&y()");
|
||||
fold("(x || true) && y()", "1&&y()");
|
||||
fold("(x || false) && y()", "x&&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("(x && true) && y()", "x && y()");
|
||||
// fold("(x && false) && y()", "0&&y()");
|
||||
// fold("(x || true) && y()", "1&&y()");
|
||||
// fold("(x || false) && y()", "x&&y()");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -13,15 +13,15 @@ Original | minified | minified | gzip | gzip | Fixture
|
|||
|
||||
555.77 kB | 273.48 kB | 270.13 kB | 90.94 kB | 90.80 kB | d3.js
|
||||
|
||||
1.01 MB | 460.76 kB | 458.89 kB | 126.88 kB | 126.71 kB | bundle.min.js
|
||||
1.01 MB | 460.75 kB | 458.89 kB | 126.88 kB | 126.71 kB | bundle.min.js
|
||||
|
||||
1.25 MB | 653.18 kB | 646.76 kB | 163.57 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.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
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue