mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(minifier): minimize (x = 1) === 1 -> (x = 1) == 1 (#8310)
This commit is contained in:
parent
4b68cc0972
commit
e3ff81ef82
2 changed files with 31 additions and 9 deletions
|
|
@ -541,8 +541,27 @@ impl<'a> PeepholeMinimizeConditions {
|
|||
e: &mut BinaryExpression<'a>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
) -> Option<Expression<'a>> {
|
||||
if !e.operator.is_equality() {
|
||||
return None;
|
||||
}
|
||||
let left = ValueType::from(&e.left);
|
||||
let right = ValueType::from(&e.right);
|
||||
if left.is_undetermined() || right.is_undetermined() {
|
||||
return None;
|
||||
}
|
||||
if left == right {
|
||||
match e.operator {
|
||||
BinaryOperator::StrictInequality => {
|
||||
e.operator = BinaryOperator::Inequality;
|
||||
}
|
||||
BinaryOperator::StrictEquality => {
|
||||
e.operator = BinaryOperator::Equality;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
match &mut e.right {
|
||||
Expression::BooleanLiteral(b) if ValueType::from(&e.left).is_boolean() => {
|
||||
Expression::BooleanLiteral(b) if left.is_boolean() => {
|
||||
match e.operator {
|
||||
BinaryOperator::Inequality | BinaryOperator::StrictInequality => {
|
||||
e.operator = BinaryOperator::Equality;
|
||||
|
|
@ -562,9 +581,9 @@ impl<'a> PeepholeMinimizeConditions {
|
|||
})
|
||||
}
|
||||
Expression::NumericLiteral(lit)
|
||||
if lit.value == 0.0
|
||||
if lit.value == 0.0
|
||||
&& !e.left.is_literal() // let constant folding do the work
|
||||
&& ValueType::from(&e.left).is_number()
|
||||
&& left.is_number()
|
||||
&& Self::is_in_boolean_context(ctx) =>
|
||||
{
|
||||
match e.operator {
|
||||
|
|
@ -1733,6 +1752,9 @@ mod test {
|
|||
test("if(x >> y !== 0){}", "if(x >> y){}");
|
||||
test("if((-0 != +0) !== false){}", "if (-0 != +0) {}");
|
||||
test_same("foo(x >> y == 0)");
|
||||
|
||||
test("(x = 1) === 1", "(x = 1) == 1");
|
||||
test("(x = 1) !== 1", "(x = 1) != 1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -3,25 +3,25 @@ Original | minified | minified | gzip | gzip | Fixture
|
|||
-------------------------------------------------------------------------------------
|
||||
72.14 kB | 23.68 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js
|
||||
|
||||
173.90 kB | 59.87 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js
|
||||
173.90 kB | 59.86 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js
|
||||
|
||||
287.63 kB | 90.15 kB | 90.07 kB | 32.07 kB | 31.95 kB | jquery.js
|
||||
|
||||
342.15 kB | 118.22 kB | 118.14 kB | 44.52 kB | 44.37 kB | vue.js
|
||||
342.15 kB | 118.21 kB | 118.14 kB | 44.52 kB | 44.37 kB | vue.js
|
||||
|
||||
544.10 kB | 71.79 kB | 72.48 kB | 26.18 kB | 26.20 kB | lodash.js
|
||||
|
||||
555.77 kB | 273.15 kB | 270.13 kB | 90.95 kB | 90.80 kB | d3.js
|
||||
555.77 kB | 273.14 kB | 270.13 kB | 90.95 kB | 90.80 kB | d3.js
|
||||
|
||||
1.01 MB | 460.31 kB | 458.89 kB | 126.84 kB | 126.71 kB | bundle.min.js
|
||||
|
||||
1.25 MB | 652.68 kB | 646.76 kB | 163.53 kB | 163.73 kB | three.js
|
||||
|
||||
2.14 MB | 726.15 kB | 724.14 kB | 180.16 kB | 181.07 kB | victory.js
|
||||
2.14 MB | 726.14 kB | 724.14 kB | 180.16 kB | 181.07 kB | victory.js
|
||||
|
||||
3.20 MB | 1.01 MB | 1.01 MB | 331.88 kB | 331.56 kB | echarts.js
|
||||
3.20 MB | 1.01 MB | 1.01 MB | 331.89 kB | 331.56 kB | echarts.js
|
||||
|
||||
6.69 MB | 2.32 MB | 2.31 MB | 492.77 kB | 488.28 kB | antd.js
|
||||
|
||||
10.95 MB | 3.50 MB | 3.49 MB | 909.11 kB | 915.50 kB | typescript.js
|
||||
10.95 MB | 3.50 MB | 3.49 MB | 909.12 kB | 915.50 kB | typescript.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue