@@ -562,7 +562,7 @@ impl<'a> PeepholeOptimizations {
562562 return false ;
563563 }
564564
565- !call_expr . may_have_side_effects ( ctx)
565+ !e . may_have_side_effects ( ctx)
566566 }
567567
568568 pub fn fold_iife_expression ( e : & mut Expression < ' a > , ctx : & mut Ctx < ' a , ' _ > ) -> bool {
@@ -573,86 +573,56 @@ impl<'a> PeepholeOptimizations {
573573 Expression :: FunctionExpression ( f) => {
574574 f. params . is_empty ( ) && f. body . as_ref ( ) . is_some_and ( |body| body. is_empty ( ) )
575575 }
576+
576577 Expression :: ArrowFunctionExpression ( f) => f. params . is_empty ( ) && f. body . is_empty ( ) ,
577578 _ => false ,
578579 } ;
580+
579581 if is_empty_iife {
580582 * e = ctx. value_to_expr ( call_expr. span , ConstantValue :: Undefined ) ;
583+ ctx. state . changed = true ;
581584 // Replace "(() => {})()" with "undefined"
582585 // Replace "(function () => { return })()" with "undefined"
583586 return true ;
584587 }
585588
586- match & mut call_expr. callee {
587- Expression :: FunctionExpression ( f) => {
588- if !f. r#async && !f. generator && !f. params . has_parameter ( ) {
589- let statements = & mut f. body . as_mut ( ) . unwrap ( ) . statements ;
590- if statements. len ( ) == 1 {
591- match & mut statements[ 0 ] {
592- Statement :: ExpressionStatement ( expr_stmt) => {
593- // Replace "(function () { foo() })" with "(foo(), undefined)"
594- * e = ctx. ast . expression_sequence ( expr_stmt. span , {
595- let mut sequence = ctx. ast . vec ( ) ;
596- sequence. push ( expr_stmt. expression . take_in ( ctx. ast ) ) ;
597- sequence. push ( ctx. value_to_expr (
598- call_expr. span ,
599- ConstantValue :: Undefined ,
600- ) ) ;
601- sequence
602- } ) ;
603- ctx. state . changed = true ;
604- return true ;
605- }
606- Statement :: ReturnStatement ( ret_stmt) => {
607- if let Some ( argument) = & mut ret_stmt. argument {
608- // Replace "(function () { return foo() })" with "foo()"
609- * e = argument. take_in ( ctx. ast ) ;
610- return true ;
611- }
612- return true ;
613- }
614- _ => { }
615- }
616- }
589+ if let Expression :: ArrowFunctionExpression ( f) = & mut call_expr. callee
590+ && !f. r#async
591+ && !f. params . has_parameter ( )
592+ && f. body . statements . len ( ) == 1
593+ {
594+ if f. expression {
595+ // Replace "(() => foo())()" with "foo()"
596+ let expr = f. get_expression_mut ( ) . unwrap ( ) ;
597+ * e = expr. take_in ( ctx. ast ) ;
598+ ctx. state . changed = true ;
599+ return true ;
600+ }
601+ match & mut f. body . statements [ 0 ] {
602+ Statement :: ExpressionStatement ( expr_stmt) => {
603+ // Replace "(() => { foo() })" with "(foo(), undefined)"
604+ * e = ctx. ast . expression_sequence ( expr_stmt. span , {
605+ let mut sequence = ctx. ast . vec ( ) ;
606+ sequence. push ( expr_stmt. expression . take_in ( ctx. ast ) ) ;
607+ sequence
608+ . push ( ctx. value_to_expr ( call_expr. span , ConstantValue :: Undefined ) ) ;
609+ sequence
610+ } ) ;
611+ ctx. state . changed = true ;
617612 return true ;
618613 }
619- }
620- Expression :: ArrowFunctionExpression ( f) => {
621- if !f. r#async && !f. params . has_parameter ( ) && f. body . statements . len ( ) == 1 {
622- if f. expression {
623- // Replace "(() => foo())()" with "foo()"
624- let expr = f. get_expression_mut ( ) . unwrap ( ) ;
625- * e = expr. take_in ( ctx. ast ) ;
614+ Statement :: ReturnStatement ( ret_stmt) => {
615+ if let Some ( argument) = & mut ret_stmt. argument {
616+ // Replace "(() => { return foo() })" with "foo()"
617+ * e = argument. take_in ( ctx. ast ) ;
618+ ctx. state . changed = true ;
626619 return true ;
627620 }
628- match & mut f. body . statements [ 0 ] {
629- Statement :: ExpressionStatement ( expr_stmt) => {
630- // Replace "(() => { foo() })" with "(foo(), undefined)"
631- * e = ctx. ast . expression_sequence ( expr_stmt. span , {
632- let mut sequence = ctx. ast . vec ( ) ;
633- sequence. push ( expr_stmt. expression . take_in ( ctx. ast ) ) ;
634- sequence. push (
635- ctx. value_to_expr ( call_expr. span , ConstantValue :: Undefined ) ,
636- ) ;
637- sequence
638- } ) ;
639- ctx. state . changed = true ;
640- return true ;
641- }
642- Statement :: ReturnStatement ( ret_stmt) => {
643- if let Some ( argument) = & mut ret_stmt. argument {
644- // Replace "(() => { return foo() })" with "foo()"
645- * e = argument. take_in ( ctx. ast ) ;
646- return true ;
647- }
648- return true ;
649- }
650- _ => { }
651- }
652621 return true ;
653622 }
623+ _ => { }
654624 }
655- _ => return false ,
625+ return true ;
656626 }
657627 }
658628
@@ -1050,11 +1020,6 @@ mod test {
10501020 fn test_fold_iife ( ) {
10511021 test_same ( "var k = () => {}" ) ;
10521022 test_same ( "var k = function () {}" ) ;
1053- test ( "var a = (() => {})()" , "var a = void 0;" ) ;
1054- test ( "var a = (() => b())();" , "var a = b()" ) ;
1055- test ( "var a = (() => true)()" , "var a = !0" ) ;
1056- test ( "var a = (() => { return true })()" , "var a = !0" ) ;
1057- test ( "var a = (() => { b() })()" , "var a = (b(), void 0)" ) ;
10581023 test ( "(() => {})()" , "" ) ;
10591024 test ( "(() => a())()" , "a();" ) ;
10601025 test ( "(() => { a() })()" , "a();" ) ;
@@ -1064,37 +1029,31 @@ mod test {
10641029 test_same ( "(a => { a() })()" ) ;
10651030 test ( "((...a) => {})()" , "" ) ;
10661031 test_same ( "((...a) => { a() })()" ) ;
1067- test ( "(() => { let b = a; b() })()" , "a();" ) ;
1068- test ( "(() => { let b = a; return b() })()" , "a();" ) ;
10691032 test ( "(async () => {})()" , "" ) ;
10701033 test_same ( "(async () => { a() })()" ) ;
1071- test ( "(async () => { let b = a; b() })()" , "(async () => { a() })();" ) ;
1072- test ( "(async () => { let b = a; return b() })()" , "(async () => a())();" ) ;
10731034 test ( "a((() => b())());" , "a(b())" ) ;
10741035 test ( "a((() => true)());" , "a(!0)" ) ;
10751036 test ( "a((() => { return true })());" , "a(!0)" ) ;
10761037
1077- test ( "var a = (function() {})()" , "var a = void 0;" ) ;
1078- test ( "var a = (function () { b() })()" , "var a = (b(), void 0)" ) ;
1079- test ( "var a = (function () { return b() })()" , "var a = b()" ) ;
1080- test ( "var a = (function () { return true })()" , "var a = !0" ) ;
1081- test ( "a((function () { return true })());" , "a(!0)" ) ;
1038+ test_same ( "var a = (function () { b() })()" ) ;
1039+ test_same ( "var a = (function () { return b() })()" ) ;
1040+ test_same ( "var a = (function () { return !0 })()" ) ;
1041+ test_same ( "a((function () { return !0 })());" ) ;
10821042 test ( "(function() {})()" , "" ) ;
10831043 test ( "(function*() {})()" , "" ) ;
10841044 test ( "(async function() {})()" , "" ) ;
1085- test ( "(function() { a() })()" , "a ()") ;
1045+ test_same ( "(function() { a() })()" ) ;
10861046 test_same ( "(function*() { a() })()" ) ;
10871047 test_same ( "(async function() { a() })()" ) ;
10881048
10891049 test ( "(() => x)()" , "x;" ) ;
10901050 test ( "(() => { return x })()" , "x;" ) ;
1091- test ( "(function () { return x })()" , "x; ") ;
1051+ test_same ( "(function () { return x })()" ) ;
10921052
10931053 test_same ( "var a = /* @__PURE__ */ (() => x)(y, z)" ) ;
10941054 test ( "(/* @__PURE__ */ (() => !0)() ? () => x() : () => {})();" , "x();" ) ;
10951055 test ( "/* @__PURE__ */ (() => x)()" , "x;" ) ;
10961056 test ( "/* @__PURE__ */ (() => { return x })()" , "x;" ) ;
1097- test ( "/* @__PURE__ */ (function () { return x })()" , "x;" ) ;
10981057 test ( "/* @__PURE__ */ (() => x)(y, z)" , "y, z;" ) ;
10991058 }
11001059
0 commit comments