Skip to content

Commit 6403cfe

Browse files
committed
pref(es/minifier): less get_type
1 parent 4eab8e8 commit 6403cfe

File tree

1 file changed

+21
-25
lines changed
  • crates/swc_ecma_minifier/src/compress/pure

1 file changed

+21
-25
lines changed

crates/swc_ecma_minifier/src/compress/pure/bools.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -243,36 +243,32 @@ impl Pure<'_> {
243243
}
244244

245245
pub(super) fn compress_cmp_with_long_op(&mut self, e: &mut BinExpr) {
246-
fn should_optimize(l: &Expr, r: &Expr, ctx: ExprCtx, opts: &CompressOptions) -> bool {
247-
match (l, r) {
246+
if !matches!(e.op, op!("===") | op!("!==")) {
247+
return;
248+
}
249+
250+
let is_typeof_unaray = |l: &Expr, r: &Expr| {
251+
matches!(
252+
(l, r),
248253
(
249254
Expr::Unary(UnaryExpr {
250-
op: op!("typeof"), ..
255+
op: op!("typeof"),
256+
..
251257
}),
252-
Expr::Lit(..),
253-
) => true,
254-
_ => {
255-
if opts.comparisons {
256-
match (l.get_type(ctx), r.get_type(ctx)) {
257-
(Value::Known(lt), Value::Known(rt)) => lt == rt,
258-
259-
_ => false,
260-
}
261-
} else {
262-
false
263-
}
264-
}
265-
}
266-
}
258+
Expr::Lit(..)
259+
)
260+
)
261+
};
267262

268-
match e.op {
269-
op!("===") | op!("!==") => {}
270-
_ => return,
271-
}
263+
let should_optimize = is_typeof_unaray(&e.left, &e.right)
264+
|| is_typeof_unaray(&e.right, &e.left)
265+
|| (self.options.comparisons
266+
&& matches!(
267+
(e.left.get_type(self.expr_ctx), e.right.get_type(self.expr_ctx)),
268+
(Value::Known(l), Value::Known(r)) if l == r
269+
));
272270

273-
if should_optimize(&e.left, &e.right, self.expr_ctx, self.options)
274-
|| should_optimize(&e.right, &e.left, self.expr_ctx, self.options)
275-
{
271+
if should_optimize {
276272
report_change!("bools: Compressing comparison of `typeof` with literal");
277273
self.changed = true;
278274
e.op = match e.op {

0 commit comments

Comments
 (0)