Skip to content

Commit afa595c

Browse files
committed
revert: handling of function expression
1 parent f9a5937 commit afa595c

File tree

6 files changed

+54
-93
lines changed

6 files changed

+54
-93
lines changed

crates/oxc_minifier/src/peephole/fold_constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ mod test {
13801380
fn test_fold_logical_op2() {
13811381
fold("x = function(){} && x", "x=x");
13821382
fold("x = true && function(){}", "x=function(){}");
1383-
fold("x = [(function(){alert(x)})()] && x", "x=(alert(x), x)");
1383+
fold("x = [(function(){alert(x)})()] && x", "x = ((function() { alert(x); })(), x)");
13841384
}
13851385

13861386
#[test]

crates/oxc_minifier/src/peephole/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ impl<'a> Traverse<'a, MinifierState<'a>> for DeadCodeElimination {
520520
Expression::ChainExpression(_) => PeepholeOptimizations::fold_chain_expr(e, ctx),
521521
Expression::CallExpression(_) => {
522522
PeepholeOptimizations::fold_call_expression(e, ctx);
523+
PeepholeOptimizations::fold_iife_expression(e, ctx);
523524
PeepholeOptimizations::remove_dead_code_call_expression(e, ctx);
524525
}
525526
Expression::ConditionalExpression(_) => {

crates/oxc_minifier/src/peephole/remove_unused_expression.rs

Lines changed: 40 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -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

crates/oxc_minifier/tests/peephole/esbuild.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,21 +1283,22 @@ fn test_flatten_values() {
12831283
test("var a = () => {return}", "var a = () => {};");
12841284
test("var a = () => {return 123}", "var a = () => 123;");
12851285
test("var a = () => {throw 123}", "var a = () => { throw 123;};");
1286-
// test("var a = (() => {})()", "var a = /* @__PURE__ */ (() => {})();");
1286+
test("var a = (() => {})()", "var a = void 0;");
12871287
test("(() => {})()", "");
12881288
test("(() => a())()", "a();");
12891289
test("(() => { a() })()", "a();");
12901290
test("(() => { return a() })()", "a();");
1291-
// test("(() => { let b = a; b() })()", "a();");
1292-
// test("(() => { let b = a; return b() })()", "a();");
1293-
// test("(async () => {})()", "");
1294-
// test("(async () => { a() })()", "(async () => a())();");
1295-
// test("(async () => { let b = a; b() })()", "(async () => a())();");
1296-
// test("var a = (function() {})()", "var a = /* @__PURE__ */ function() {}();");
1291+
test("(() => { let b = a; b() })()", "a();");
1292+
test("(() => { let b = a; return b() })()", "a();");
1293+
test("(async () => {})()", "");
1294+
test("(async () => { a() })()", "(async () => { a() })();");
1295+
test("(async () => { let b = a; b() })()", "(async () => { a() })();");
1296+
test("(async () => { let b = a; return b() })()", "(async () => a())();");
1297+
test("var a = (function() {})()", "var a = void 0;");
12971298
test("(function() {})()", "");
12981299
test("(function*() {})()", "");
12991300
test("(async function() {})()", "");
1300-
test("(function() { a() })()", "a();");
1301+
test("(function() { a() })()", "(function() { a();})();");
13011302
test("(function*() { a() })()", "(function* () { a();})();");
13021303
test("(async function() { a() })()", "(async function() { a();})();");
13031304
test("(() => x)()", "x;");

tasks/minsize/minsize.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Original | minified | minified | gzip | gzip | Iterations | Fi
1313

1414
555.77 kB | 267.39 kB | 270.13 kB | 88.00 kB | 90.80 kB | 2 | d3.js
1515

16-
1.01 MB | 439.40 kB | 458.89 kB | 122.06 kB | 126.71 kB | 2 | bundle.min.js
16+
1.01 MB | 439.38 kB | 458.89 kB | 122.06 kB | 126.71 kB | 2 | bundle.min.js
1717

1818
1.25 MB | 642.64 kB | 646.76 kB | 159.40 kB | 163.73 kB | 2 | three.js
1919

tasks/track_memory_allocations/allocs_minifier.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ File | File size || Sys allocs | Sys reallocs |
22
-------------------------------------------------------------------------------------------------------------------------------------------
33
checker.ts | 2.92 MB || 83503 | 14179 || 152592 | 28237
44

5-
cal.com.tsx | 1.06 MB || 40453 | 3033 || 37145 | 4586
5+
cal.com.tsx | 1.06 MB || 40448 | 3032 || 37138 | 4586
66

77
RadixUIAdoptionSection.jsx | 2.52 kB || 85 | 9 || 30 | 6
88

9-
pdf.mjs | 567.30 kB || 20495 | 2899 || 47447 | 7728
9+
pdf.mjs | 567.30 kB || 20495 | 2899 || 47457 | 7730
1010

1111
antd.js | 6.69 MB || 99534 | 13524 || 331657 | 69358
1212

0 commit comments

Comments
 (0)