mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(minifier): reverse negated conditional exprs (#8205)
This commit is contained in:
parent
cb709c9d83
commit
4c2059af29
4 changed files with 48 additions and 36 deletions
|
|
@ -257,26 +257,39 @@ impl<'a> PeepholeMinimizeConditions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `a ? a : b` -> `a || b`
|
|
||||||
fn try_minimize_conditional(
|
fn try_minimize_conditional(
|
||||||
expr: &mut ConditionalExpression<'a>,
|
expr: &mut ConditionalExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
let Expression::Identifier(test_ident) = &expr.test else { return None };
|
// `a ? a : b` -> `a || b`
|
||||||
let Expression::Identifier(consequent_ident) = &expr.consequent else { return None };
|
if let (Expression::Identifier(test_ident), Expression::Identifier(consequent_ident)) =
|
||||||
|
(&expr.test, &expr.consequent)
|
||||||
if test_ident.name != consequent_ident.name {
|
{
|
||||||
return None;
|
if test_ident.name == consequent_ident.name {
|
||||||
}
|
|
||||||
|
|
||||||
let ident = ctx.ast.move_expression(&mut expr.test);
|
let ident = ctx.ast.move_expression(&mut expr.test);
|
||||||
|
|
||||||
Some(ctx.ast.expression_logical(
|
return Some(ctx.ast.expression_logical(
|
||||||
expr.span,
|
expr.span,
|
||||||
ident,
|
ident,
|
||||||
LogicalOperator::Or,
|
LogicalOperator::Or,
|
||||||
ctx.ast.move_expression(&mut expr.alternate),
|
ctx.ast.move_expression(&mut expr.alternate),
|
||||||
))
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// `!x ? foo() : bar()` -> `x ? bar() : foo()`
|
||||||
|
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);
|
||||||
|
let consequent = ctx.ast.move_expression(&mut expr.consequent);
|
||||||
|
let alternate = ctx.ast.move_expression(&mut expr.alternate);
|
||||||
|
return Some(
|
||||||
|
ctx.ast.expression_conditional(expr.span, test, alternate, consequent),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -696,7 +709,7 @@ mod test {
|
||||||
fold_same("x() ? x() : y()");
|
fold_same("x() ? x() : y()");
|
||||||
fold_same("x?.() ? x?.() : y()");
|
fold_same("x?.() ? x?.() : y()");
|
||||||
|
|
||||||
// fold("!x ? foo() : bar()", "x ? bar() : foo()");
|
fold("!x ? foo() : bar()", "x ? bar() : foo()");
|
||||||
// fold("while(!(x ? y : z)) foo();", "while(x ? !y : !z) foo();");
|
// fold("while(!(x ? y : z)) foo();", "while(x ? !y : !z) foo();");
|
||||||
// fold("(x ? !y : !z) ? foo() : bar()", "(x ? y : z) ? bar() : foo()");
|
// fold("(x ? !y : !z) ? foo() : bar()", "(x ? y : z) ? bar() : foo()");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -394,8 +394,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
||||||
expr: &mut ConditionalExpression<'a>,
|
expr: &mut ConditionalExpression<'a>,
|
||||||
ctx: Ctx<'a, 'b>,
|
ctx: Ctx<'a, 'b>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
match ctx.get_boolean_value(&expr.test) {
|
|
||||||
Some(true) => {
|
|
||||||
// Bail `let o = { f() { assert.ok(this !== o); } }; (true ? o.f : false)(); (true ? o.f : false)``;`
|
// Bail `let o = { f() { assert.ok(this !== o); } }; (true ? o.f : false)(); (true ? o.f : false)``;`
|
||||||
let parent = ctx.ancestry.parent();
|
let parent = ctx.ancestry.parent();
|
||||||
if parent.is_tagged_template_expression()
|
if parent.is_tagged_template_expression()
|
||||||
|
|
@ -403,8 +401,9 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
||||||
{
|
{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some(ctx.ast.move_expression(&mut expr.consequent))
|
|
||||||
}
|
match ctx.get_boolean_value(&expr.test) {
|
||||||
|
Some(true) => Some(ctx.ast.move_expression(&mut expr.consequent)),
|
||||||
Some(false) => Some(ctx.ast.move_expression(&mut expr.alternate)),
|
Some(false) => Some(ctx.ast.move_expression(&mut expr.alternate)),
|
||||||
None => None,
|
None => None,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,8 +51,8 @@ fn tagged_template() {
|
||||||
test_same("(1, o.f)``");
|
test_same("(1, o.f)``");
|
||||||
test_same("(!0 && o.f)()");
|
test_same("(!0 && o.f)()");
|
||||||
test_same("(!0 && o.f)``");
|
test_same("(!0 && o.f)``");
|
||||||
test_same("(!0 ? o.f : !1)()");
|
test("(!0 ? o.f : !1)()", "(0 ? !1: o.f)()");
|
||||||
test_same("(!0 ? o.f : !1)``");
|
test("(!0 ? o.f : !1)``", "(0 ? !1: o.f)``");
|
||||||
|
|
||||||
test("foo(true && o.f)", "foo(o.f)");
|
test("foo(true && o.f)", "foo(o.f)");
|
||||||
test("foo(true ? o.f : false)", "foo(o.f)");
|
test("foo(true ? o.f : false)", "foo(o.f)");
|
||||||
|
|
|
||||||
|
|
@ -3,25 +3,25 @@ Original | minified | minified | gzip | gzip | Fixture
|
||||||
-------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------
|
||||||
72.14 kB | 23.71 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js
|
72.14 kB | 23.71 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js
|
||||||
|
|
||||||
173.90 kB | 59.91 kB | 59.82 kB | 19.45 kB | 19.33 kB | moment.js
|
173.90 kB | 59.90 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js
|
||||||
|
|
||||||
287.63 kB | 90.40 kB | 90.07 kB | 32.12 kB | 31.95 kB | jquery.js
|
287.63 kB | 90.39 kB | 90.07 kB | 32.12 kB | 31.95 kB | jquery.js
|
||||||
|
|
||||||
342.15 kB | 118.50 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js
|
342.15 kB | 118.48 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js
|
||||||
|
|
||||||
544.10 kB | 71.85 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
|
544.10 kB | 71.84 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
|
||||||
|
|
||||||
555.77 kB | 273.49 kB | 270.13 kB | 90.96 kB | 90.80 kB | d3.js
|
555.77 kB | 273.48 kB | 270.13 kB | 90.94 kB | 90.80 kB | d3.js
|
||||||
|
|
||||||
1.01 MB | 460.80 kB | 458.89 kB | 126.93 kB | 126.71 kB | bundle.min.js
|
1.01 MB | 460.76 kB | 458.89 kB | 126.88 kB | 126.71 kB | bundle.min.js
|
||||||
|
|
||||||
1.25 MB | 653.19 kB | 646.76 kB | 163.58 kB | 163.73 kB | three.js
|
1.25 MB | 653.18 kB | 646.76 kB | 163.57 kB | 163.73 kB | three.js
|
||||||
|
|
||||||
2.14 MB | 726.75 kB | 724.14 kB | 180.29 kB | 181.07 kB | victory.js
|
2.14 MB | 726.72 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js
|
||||||
|
|
||||||
3.20 MB | 1.01 MB | 1.01 MB | 332.21 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.07 kB | 488.28 kB | antd.js
|
6.69 MB | 2.32 MB | 2.31 MB | 493.04 kB | 488.28 kB | antd.js
|
||||||
|
|
||||||
10.95 MB | 3.51 MB | 3.49 MB | 910.37 kB | 915.50 kB | typescript.js
|
10.95 MB | 3.51 MB | 3.49 MB | 910.11 kB | 915.50 kB | typescript.js
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue