@@ -17,7 +17,7 @@ use super::Pure;
1717use crate :: {
1818 compress:: {
1919 pure:: { strings:: convert_str_value_to_tpl_raw, Ctx } ,
20- util:: { eval_to_undefined , is_pure_undefined} ,
20+ util:: is_pure_undefined,
2121 } ,
2222 usage_analyzer:: util:: is_global_var_with_pure_property_access,
2323} ;
@@ -61,45 +61,6 @@ fn can_compress_new_regexp(args: Option<&[ExprOrSpread]>) -> bool {
6161 }
6262}
6363
64- fn may_evaluate_to_nullish ( expr_ctx : ExprCtx , expr : & Expr ) -> bool {
65- if is_pure_undefined ( expr_ctx, expr) || matches ! ( expr, Expr :: Lit ( Lit :: Null ( ..) ) ) {
66- return true ;
67- }
68-
69- match expr {
70- Expr :: Paren ( ParenExpr { expr, .. } ) => may_evaluate_to_nullish ( expr_ctx, expr) ,
71- Expr :: Seq ( SeqExpr { exprs, .. } ) => match exprs. last ( ) {
72- Some ( last) => may_evaluate_to_nullish ( expr_ctx, last) ,
73- None => false ,
74- } ,
75- Expr :: Cond ( CondExpr { cons, alt, .. } ) => {
76- may_evaluate_to_nullish ( expr_ctx, cons) || may_evaluate_to_nullish ( expr_ctx, alt)
77- }
78- _ => matches ! (
79- expr. get_type( expr_ctx) ,
80- Value :: Known ( Type :: Undefined | Type :: Null ) | Value :: Unknown
81- ) ,
82- }
83- }
84-
85- /// Returns true if evaluating `expr` always produces a nullish value.
86- ///
87- /// Unlike [`is_pure_undefined`], this accepts expressions with effects because
88- /// callers can preserve those effects separately.
89- fn eval_to_nullish ( expr_ctx : ExprCtx , expr : & Expr ) -> bool {
90- match expr {
91- Expr :: Paren ( ParenExpr { expr, .. } ) => eval_to_nullish ( expr_ctx, expr) ,
92- Expr :: Seq ( SeqExpr { exprs, .. } ) => exprs
93- . last ( )
94- . is_some_and ( |last| eval_to_nullish ( expr_ctx, last) ) ,
95- Expr :: Cond ( CondExpr { cons, alt, .. } ) => {
96- eval_to_nullish ( expr_ctx, cons) && eval_to_nullish ( expr_ctx, alt)
97- }
98- Expr :: Lit ( Lit :: Null ( ..) ) => true ,
99- _ => eval_to_undefined ( expr_ctx, expr) ,
100- }
101- }
102-
10364fn collect_exprs_from_object ( obj : & mut ObjectLit ) -> Vec < Box < Expr > > {
10465 let mut exprs = Vec :: new ( ) ;
10566
@@ -885,9 +846,10 @@ impl Pure<'_> {
885846 if !self . options . unsafe_passes
886847 && groups. iter ( ) . any ( |group| match group {
887848 GroupType :: Literals ( _) => false ,
888- GroupType :: Expression ( expr) => {
889- may_evaluate_to_nullish ( self . expr_ctx , & expr. expr )
890- }
849+ GroupType :: Expression ( expr) => matches ! (
850+ & expr. expr. get_type( self . expr_ctx, ) ,
851+ Value :: Known ( Type :: Null | Type :: Undefined ) | Value :: Unknown
852+ ) ,
891853 } )
892854 {
893855 return None ;
@@ -2339,8 +2301,10 @@ impl Pure<'_> {
23392301 . and_then ( |arg| arg. first ( ) )
23402302 . map ( |arg| {
23412303 arg. spread . is_none ( )
2342- && ( eval_to_nullish ( self . expr_ctx , & arg. expr )
2343- || is_valid_map_set_init ( & arg. expr , self . expr_ctx , callee) )
2304+ && ( matches ! (
2305+ & arg. expr. get_type( self . expr_ctx, ) ,
2306+ Value :: Known ( Type :: Null | Type :: Undefined )
2307+ ) || is_valid_map_set_init ( & arg. expr , self . expr_ctx , callee) )
23442308 } )
23452309 . unwrap_or ( true ) =>
23462310 {
0 commit comments