Skip to content

Commit

Permalink
feat(minifier): minimize (x = 1) === 1 -> (x = 1) == 1 (#8310)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 7, 2025
1 parent 4b68cc0 commit e3ff81e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
28 changes: 25 additions & 3 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 6 additions & 6 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e3ff81e

Please sign in to comment.