mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(minifier): fold true / false comparison. (#6225)
Input: ```js a == false ``` Previous: ```js a == !1 ``` Current: ```js a == 0 ``` Only handle it when it is non-plus, non-relation binary expressions. Align with [Closure Compiler](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520SIMPLE_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250Ax%2520%253C%253C%2520true%253B%250A%250Ax%2520%252B%2520true%253B%250A%250Ax%2520-%2520true%253B%250A%250Ax%2520%257C%2520true%253B%250A%250Ax%2520%2525%2520true%253B%250A%250Ay%2520!%253D%2520false%253B%250A%250Af()%2520%253D%253D%2520false%253B%250A%250Ax%2520instanceof%2520true%250A%250Ax%2520**%2520true%250A%250Ax%2520%2526%2520true%250A%250Ax%2520%255E%2520false%250A%250Ax%2520%253D%253D%2520(x%2520instanceof%2520false)%250A%250Ax%2520instanceof%2520(x%2520%253C%253C%2520true)%250A%250Ax%2520%253D%253D%2520fake(false)).
This commit is contained in:
parent
acab777c0a
commit
23b646484c
2 changed files with 42 additions and 18 deletions
|
|
@ -5,7 +5,7 @@ use oxc_syntax::{
|
|||
number::NumberBase,
|
||||
operator::{BinaryOperator, UnaryOperator},
|
||||
};
|
||||
use oxc_traverse::{Traverse, TraverseCtx};
|
||||
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};
|
||||
|
||||
use crate::{node_util::NodeUtil, CompressOptions, CompressorPass};
|
||||
|
||||
|
|
@ -89,7 +89,7 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
|
|||
}
|
||||
}
|
||||
|
||||
fn exit_binary_expression(
|
||||
fn enter_binary_expression(
|
||||
&mut self,
|
||||
expr: &mut BinaryExpression<'a>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
|
|
@ -168,16 +168,33 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
|||
fn compress_boolean(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) -> bool {
|
||||
let Expression::BooleanLiteral(lit) = expr else { return false };
|
||||
if self.options.booleans && !self.in_define_export {
|
||||
let parent = ctx.ancestry.parent();
|
||||
let no_unary = {
|
||||
if let Ancestor::BinaryExpressionRight(u) = parent {
|
||||
!matches!(
|
||||
u.operator(),
|
||||
BinaryOperator::Addition | BinaryOperator::Instanceof | BinaryOperator::In
|
||||
)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
// XOR: We should use `!neg` when it is not in binary expression.
|
||||
let num = ctx.ast.expression_numeric_literal(
|
||||
SPAN,
|
||||
if lit.value { 0.0 } else { 1.0 },
|
||||
if lit.value { "0" } else { "1" },
|
||||
if lit.value ^ no_unary { 0.0 } else { 1.0 },
|
||||
if lit.value ^ no_unary { "0" } else { "1" },
|
||||
NumberBase::Decimal,
|
||||
);
|
||||
*expr = ctx.ast.expression_unary(SPAN, UnaryOperator::LogicalNot, num);
|
||||
return true;
|
||||
*expr = if no_unary {
|
||||
num
|
||||
} else {
|
||||
ctx.ast.expression_unary(SPAN, UnaryOperator::LogicalNot, num)
|
||||
};
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Compress `typeof foo == "undefined"` into `typeof foo > "u"`
|
||||
|
|
@ -350,7 +367,6 @@ mod test {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn fold_true_false_comparison() {
|
||||
test("x == true", "x == 1");
|
||||
test("x == false", "x == 0");
|
||||
|
|
@ -359,6 +375,14 @@ mod test {
|
|||
test("x <= true", "x <= 1");
|
||||
test("x > true", "x > 1");
|
||||
test("x >= true", "x >= 1");
|
||||
|
||||
test("x instanceof true", "x instanceof !0");
|
||||
test("x + false", "x + !1");
|
||||
|
||||
// Order: should perform the nearest.
|
||||
test("x == x instanceof false", "x == x instanceof !1");
|
||||
test("x in x >> true", "x in x >> 1");
|
||||
test("x == fake(false)", "x == fake(!1)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -2,25 +2,25 @@ Original | Minified | esbuild | Gzip | esbuild
|
|||
|
||||
72.14 kB | 24.47 kB | 23.70 kB | 8.65 kB | 8.54 kB | react.development.js
|
||||
|
||||
173.90 kB | 61.69 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js
|
||||
173.90 kB | 61.68 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js
|
||||
|
||||
287.63 kB | 92.83 kB | 90.07 kB | 32.29 kB | 31.95 kB | jquery.js
|
||||
287.63 kB | 92.79 kB | 90.07 kB | 32.28 kB | 31.95 kB | jquery.js
|
||||
|
||||
342.15 kB | 124.14 kB | 118.14 kB | 44.81 kB | 44.37 kB | vue.js
|
||||
342.15 kB | 124.12 kB | 118.14 kB | 44.80 kB | 44.37 kB | vue.js
|
||||
|
||||
544.10 kB | 74.13 kB | 72.48 kB | 26.23 kB | 26.20 kB | lodash.js
|
||||
544.10 kB | 74.12 kB | 72.48 kB | 26.22 kB | 26.20 kB | lodash.js
|
||||
|
||||
555.77 kB | 278.70 kB | 270.13 kB | 91.39 kB | 90.80 kB | d3.js
|
||||
|
||||
1.01 MB | 470.11 kB | 458.89 kB | 126.97 kB | 126.71 kB | bundle.min.js
|
||||
1.01 MB | 470.09 kB | 458.89 kB | 126.96 kB | 126.71 kB | bundle.min.js
|
||||
|
||||
1.25 MB | 671.00 kB | 646.76 kB | 164.72 kB | 163.73 kB | three.js
|
||||
1.25 MB | 670.82 kB | 646.76 kB | 164.66 kB | 163.73 kB | three.js
|
||||
|
||||
2.14 MB | 756.69 kB | 724.14 kB | 182.87 kB | 181.07 kB | victory.js
|
||||
2.14 MB | 756.68 kB | 724.14 kB | 182.84 kB | 181.07 kB | victory.js
|
||||
|
||||
3.20 MB | 1.05 MB | 1.01 MB | 334.10 kB | 331.56 kB | echarts.js
|
||||
3.20 MB | 1.05 MB | 1.01 MB | 334.06 kB | 331.56 kB | echarts.js
|
||||
|
||||
6.69 MB | 2.44 MB | 2.31 MB | 498.93 kB | 488.28 kB | antd.js
|
||||
6.69 MB | 2.44 MB | 2.31 MB | 498.83 kB | 488.28 kB | antd.js
|
||||
|
||||
10.95 MB | 3.59 MB | 3.49 MB | 913.96 kB | 915.50 kB | typescript.js
|
||||
10.95 MB | 3.59 MB | 3.49 MB | 913.92 kB | 915.50 kB | typescript.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue