mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(minifier): fold foo === undefined || foo === null (#8063)
This PR implements folding `foo === undefined || foo === null` into `foo == null`. I checked the minified output diff this time, so hoping that there isn't a bug.
This commit is contained in:
parent
774babb7f2
commit
1932f1e0a0
2 changed files with 159 additions and 11 deletions
|
|
@ -79,11 +79,22 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
|
|||
|
||||
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
let ctx = Ctx(ctx);
|
||||
if let Expression::AssignmentExpression(assignment_expr) = expr {
|
||||
if let Some(new_expr) = Self::try_compress_assignment_expression(assignment_expr, ctx) {
|
||||
*expr = new_expr;
|
||||
self.changed = true;
|
||||
match expr {
|
||||
Expression::AssignmentExpression(assignment_expr) => {
|
||||
if let Some(new_expr) =
|
||||
Self::try_compress_assignment_expression(assignment_expr, ctx)
|
||||
{
|
||||
*expr = new_expr;
|
||||
self.changed = true;
|
||||
}
|
||||
}
|
||||
Expression::LogicalExpression(logical_expr) => {
|
||||
if let Some(new_expr) = Self::try_compress_is_null_or_undefined(logical_expr, ctx) {
|
||||
*expr = new_expr;
|
||||
self.changed = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.try_compress_boolean(expr, ctx);
|
||||
self.try_compress_undefined(expr, ctx);
|
||||
|
|
@ -265,6 +276,123 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
|
|||
self.changed = true;
|
||||
}
|
||||
|
||||
/// Compress `foo === null || foo === undefined` into `foo == null`.
|
||||
///
|
||||
/// `foo === null || foo === undefined` => `foo == null`
|
||||
/// `foo !== null && foo !== undefined` => `foo != null`
|
||||
///
|
||||
/// This compression assumes that `document.all` is a normal object.
|
||||
/// If that assumption does not hold, this compression is not allowed.
|
||||
/// - `document.all === null || document.all === undefined` is `false`
|
||||
/// - `document.all == null` is `true`
|
||||
fn try_compress_is_null_or_undefined(
|
||||
expr: &mut LogicalExpression<'a>,
|
||||
ctx: Ctx<'a, 'b>,
|
||||
) -> Option<Expression<'a>> {
|
||||
let op = expr.operator;
|
||||
if !matches!(op, LogicalOperator::Or | LogicalOperator::And) {
|
||||
return None;
|
||||
}
|
||||
#[allow(clippy::match_wildcard_for_single_variants)]
|
||||
let target_ops = match op {
|
||||
LogicalOperator::Or => (BinaryOperator::StrictEquality, BinaryOperator::Equality),
|
||||
LogicalOperator::And => (BinaryOperator::StrictInequality, BinaryOperator::Inequality),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
if let Some(new_expr) = Self::try_compress_is_null_or_undefined_for_left_and_right(
|
||||
&expr.left,
|
||||
&expr.right,
|
||||
expr.span,
|
||||
target_ops,
|
||||
ctx,
|
||||
) {
|
||||
return Some(new_expr);
|
||||
}
|
||||
|
||||
let Expression::LogicalExpression(left) = &mut expr.left else {
|
||||
return None;
|
||||
};
|
||||
if left.operator != op {
|
||||
return None;
|
||||
}
|
||||
|
||||
Self::try_compress_is_null_or_undefined_for_left_and_right(
|
||||
&left.right,
|
||||
&expr.right,
|
||||
Span::new(left.right.span().start, expr.span.end),
|
||||
target_ops,
|
||||
ctx,
|
||||
)
|
||||
.map(|new_expr| {
|
||||
ctx.ast.expression_logical(
|
||||
expr.span,
|
||||
ctx.ast.move_expression(&mut left.left),
|
||||
expr.operator,
|
||||
new_expr,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn try_compress_is_null_or_undefined_for_left_and_right(
|
||||
left: &Expression<'a>,
|
||||
right: &Expression<'a>,
|
||||
span: Span,
|
||||
(find_op, replace_op): (BinaryOperator, BinaryOperator),
|
||||
ctx: Ctx<'a, 'b>,
|
||||
) -> Option<Expression<'a>> {
|
||||
let pair = Self::commutative_pair(
|
||||
(&left, &right),
|
||||
|a| {
|
||||
if let Expression::BinaryExpression(op) = a {
|
||||
if op.operator == find_op {
|
||||
return Self::commutative_pair(
|
||||
(&op.left, &op.right),
|
||||
|a_a| a_a.is_null().then_some(a_a.span()),
|
||||
|a_b| {
|
||||
if let Expression::Identifier(id) = a_b {
|
||||
Some((a_b.span(), (*id).clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
None
|
||||
},
|
||||
|b| {
|
||||
if let Expression::BinaryExpression(op) = b {
|
||||
if op.operator == find_op {
|
||||
return Self::commutative_pair(
|
||||
(&op.left, &op.right),
|
||||
|b_a| b_a.evaluate_to_undefined().then_some(()),
|
||||
|b_b| {
|
||||
if let Expression::Identifier(id) = b_b {
|
||||
Some((*id).clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
)
|
||||
.map(|v| v.1);
|
||||
}
|
||||
}
|
||||
None
|
||||
},
|
||||
);
|
||||
let ((null_expr_span, (left_id_expr_span, left_id_ref)), right_id_ref) = pair?;
|
||||
if left_id_ref.name != right_id_ref.name {
|
||||
return None;
|
||||
}
|
||||
|
||||
let left_id_expr =
|
||||
ctx.ast.expression_identifier_reference(left_id_expr_span, left_id_ref.name);
|
||||
let null_expr = ctx.ast.expression_null_literal(null_expr_span);
|
||||
|
||||
Some(ctx.ast.expression_binary(span, left_id_expr, replace_op, null_expr))
|
||||
}
|
||||
|
||||
fn commutative_pair<A, F, G, RetF: 'a, RetG: 'a>(
|
||||
pair: (&A, &A),
|
||||
check_a: F,
|
||||
|
|
@ -969,4 +1097,24 @@ mod test {
|
|||
test("const foo = () => { return 'baz' }", "const foo = () => 'baz'");
|
||||
test_same("const foo = () => { foo; return 'baz' }");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fold_is_null_or_undefined() {
|
||||
test("foo === null || foo === undefined", "foo == null");
|
||||
test("foo === undefined || foo === null", "foo == null");
|
||||
test("foo === null || foo === void 0", "foo == null");
|
||||
test("foo === null || foo === void 0 || foo === 1", "foo == null || foo === 1");
|
||||
test("foo === 1 || foo === null || foo === void 0", "foo === 1 || foo == null");
|
||||
test_same("foo === void 0 || bar === null");
|
||||
test_same("foo !== 1 && foo === void 0 || foo === null");
|
||||
test_same("foo.a === void 0 || foo.a === null"); // cannot be folded because accessing foo.a might have a side effect
|
||||
|
||||
test("foo !== null && foo !== undefined", "foo != null");
|
||||
test("foo !== undefined && foo !== null", "foo != null");
|
||||
test("foo !== null && foo !== void 0", "foo != null");
|
||||
test("foo !== null && foo !== void 0 && foo !== 1", "foo != null && foo !== 1");
|
||||
test("foo !== 1 && foo !== null && foo !== void 0", "foo !== 1 && foo != null");
|
||||
test("foo !== 1 || foo !== void 0 && foo !== null", "foo !== 1 || foo != null");
|
||||
test_same("foo !== void 0 && bar !== null");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
| Oxc | ESBuild | Oxc | ESBuild |
|
||||
Original | minified | minified | gzip | gzip | Fixture
|
||||
-------------------------------------------------------------------------------------
|
||||
72.14 kB | 24.09 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js
|
||||
72.14 kB | 24.05 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js
|
||||
|
||||
173.90 kB | 61.61 kB | 59.82 kB | 19.55 kB | 19.33 kB | moment.js
|
||||
173.90 kB | 61.60 kB | 59.82 kB | 19.55 kB | 19.33 kB | moment.js
|
||||
|
||||
287.63 kB | 92.61 kB | 90.07 kB | 32.27 kB | 31.95 kB | jquery.js
|
||||
|
||||
342.15 kB | 121.79 kB | 118.14 kB | 44.59 kB | 44.37 kB | vue.js
|
||||
342.15 kB | 121.77 kB | 118.14 kB | 44.58 kB | 44.37 kB | vue.js
|
||||
|
||||
544.10 kB | 73.37 kB | 72.48 kB | 26.13 kB | 26.20 kB | lodash.js
|
||||
|
||||
|
|
@ -15,13 +15,13 @@ Original | minified | minified | gzip | gzip | Fixture
|
|||
|
||||
1.01 MB | 467.14 kB | 458.89 kB | 126.74 kB | 126.71 kB | bundle.min.js
|
||||
|
||||
1.25 MB | 662.68 kB | 646.76 kB | 164.00 kB | 163.73 kB | three.js
|
||||
1.25 MB | 662.66 kB | 646.76 kB | 164.00 kB | 163.73 kB | three.js
|
||||
|
||||
2.14 MB | 740.94 kB | 724.14 kB | 181.49 kB | 181.07 kB | victory.js
|
||||
2.14 MB | 740.54 kB | 724.14 kB | 181.37 kB | 181.07 kB | victory.js
|
||||
|
||||
3.20 MB | 1.02 MB | 1.01 MB | 332.09 kB | 331.56 kB | echarts.js
|
||||
|
||||
6.69 MB | 2.39 MB | 2.31 MB | 496.17 kB | 488.28 kB | antd.js
|
||||
6.69 MB | 2.39 MB | 2.31 MB | 495.63 kB | 488.28 kB | antd.js
|
||||
|
||||
10.95 MB | 3.55 MB | 3.49 MB | 910.48 kB | 915.50 kB | typescript.js
|
||||
10.95 MB | 3.55 MB | 3.49 MB | 909.67 kB | 915.50 kB | typescript.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue