@@ -7,7 +7,6 @@ use swc_ecma_utils::{ExprCtx, ExprExt, Type, Value};
77use super :: Pure ;
88use crate :: {
99 compress:: util:: { can_absorb_negate, is_eq, is_pure_undefined, negate, negate_cost} ,
10- option:: CompressOptions ,
1110 util:: make_bool,
1211} ;
1312
@@ -243,36 +242,32 @@ impl Pure<'_> {
243242 }
244243
245244 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) {
245+ if !matches ! ( e. op, op!( "===" ) | op!( "!==" ) ) {
246+ return ;
247+ }
248+
249+ let is_typeof_unaray = |l : & Expr , r : & Expr | {
250+ matches ! (
251+ ( l, r) ,
248252 (
249253 Expr :: Unary ( UnaryExpr {
250- op : op ! ( "typeof" ) , ..
254+ op: op!( "typeof" ) ,
255+ ..
251256 } ) ,
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- }
257+ Expr :: Lit ( ..)
258+ )
259+ )
260+ } ;
267261
268- match e. op {
269- op ! ( "===" ) | op ! ( "!==" ) => { }
270- _ => return ,
271- }
262+ let should_optimize = is_typeof_unaray ( & e. left , & e. right )
263+ || is_typeof_unaray ( & e. right , & e. left )
264+ || ( self . options . comparisons
265+ && matches ! (
266+ ( e. left. get_type( self . expr_ctx) , e. right. get_type( self . expr_ctx) ) ,
267+ ( Value :: Known ( l) , Value :: Known ( r) ) if l == r
268+ ) ) ;
272269
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- {
270+ if should_optimize {
276271 report_change ! ( "bools: Compressing comparison of `typeof` with literal" ) ;
277272 self . changed = true ;
278273 e. op = match e. op {
0 commit comments