Skip to content

Commit 6b2daa8

Browse files
committed
fix(minifier): don't inline single use variable in conditional logical expressions (#14185)
https://github.com/oxc-project/oxc/blob/358f2fccd3183b7987332cb321b946fe31a85691/crates/oxc_minifier/src/peephole/minimize_statements.rs#L1427-L1429 This should be handled in logical expressions as well, but was not handled.
1 parent 6123684 commit 6b2daa8

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

crates/oxc_minifier/src/peephole/minimize_statements.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,13 +1405,17 @@ impl<'a> PeepholeOptimizations {
14051405
) {
14061406
return Some(changed);
14071407
}
1408-
if let Some(changed) = Self::substitute_single_use_symbol_in_expression(
1409-
&mut logical_expr.right,
1410-
search_for,
1411-
replacement,
1412-
replacement_has_side_effect,
1413-
ctx,
1414-
) {
1408+
// Do not substitute our unconditionally-executed value into a branch
1409+
// unless the value itself has no side effects
1410+
if !replacement_has_side_effect
1411+
&& let Some(changed) = Self::substitute_single_use_symbol_in_expression(
1412+
&mut logical_expr.right,
1413+
search_for,
1414+
replacement,
1415+
replacement_has_side_effect,
1416+
ctx,
1417+
)
1418+
{
14151419
return Some(changed);
14161420
}
14171421
}

crates/oxc_minifier/tests/peephole/esbuild.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,10 @@ fn js_parser_test() {
678678
);
679679
test("if (a) if (b) if (c) d", "a && b && c && d;");
680680
test("if (!a) if (!b) if (!c) d", "a || b || c || d;");
681-
// FIXME
682-
// test(
683-
// "function _() { let a = foo(), b = bar(), c = baz(); return a != null ? a : b != null ? b : c }",
684-
// "function _() { let a = foo(), b = bar(), c = baz(); return a ?? b ?? c; }",
685-
// );
681+
test(
682+
"function _() { let a = foo(), b = bar(), c = baz(); return a != null ? a : b != null ? b : c }",
683+
"function _() { let a = foo(), b = bar(), c = baz(); return a ?? b ?? c; }",
684+
);
686685
test(
687686
"function _() { if (a) return c; if (b) return d; }",
688687
"function _() { if (a) return c;if (b) return d; }",
@@ -1934,19 +1933,18 @@ fn test_inline_single_use_variable() {
19341933
"function wrapper(arg0, arg1) { let x = fn(); return x ?? arg0;}",
19351934
"function wrapper(arg0, arg1) { return fn() ?? arg0;}",
19361935
);
1937-
// FIXME
1938-
// test(
1939-
// "function wrapper(arg0, arg1) { let x = fn(); return arg0 || x;}",
1940-
// "function wrapper(arg0, arg1) { let x = fn(); return arg0 || x;}",
1941-
// );
1942-
// test(
1943-
// "function wrapper(arg0, arg1) { let x = fn(); return arg0 && x;}",
1944-
// "function wrapper(arg0, arg1) { let x = fn(); return arg0 && x;}",
1945-
// );
1946-
// test(
1947-
// "function wrapper(arg0, arg1) { let x = fn(); return arg0 ?? x;}",
1948-
// "function wrapper(arg0, arg1) { let x = fn(); return arg0 ?? x;}",
1949-
// );
1936+
test(
1937+
"function wrapper(arg0, arg1) { let x = fn(); return arg0 || x;}",
1938+
"function wrapper(arg0, arg1) { let x = fn(); return arg0 || x;}",
1939+
);
1940+
test(
1941+
"function wrapper(arg0, arg1) { let x = fn(); return arg0 && x;}",
1942+
"function wrapper(arg0, arg1) { let x = fn(); return arg0 && x;}",
1943+
);
1944+
test(
1945+
"function wrapper(arg0, arg1) { let x = fn(); return arg0 ?? x;}",
1946+
"function wrapper(arg0, arg1) { let x = fn(); return arg0 ?? x;}",
1947+
);
19501948
test(
19511949
"function wrapper(arg0, arg1) { let x = fn(); let y = x[prop]; let z = y.val; throw z}",
19521950
"function wrapper(arg0, arg1) { throw fn()[prop].val;}",

tasks/minsize/minsize.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ Original | minified | minified | gzip | gzip | Iterations | Fi
77

88
287.63 kB | 89.26 kB | 90.07 kB | 30.91 kB | 31.95 kB | 2 | jquery.js
99

10-
342.15 kB | 116.97 kB | 118.14 kB | 43.15 kB | 44.37 kB | 2 | vue.js
10+
342.15 kB | 116.98 kB | 118.14 kB | 43.17 kB | 44.37 kB | 2 | vue.js
1111

12-
544.10 kB | 71.03 kB | 72.48 kB | 25.76 kB | 26.20 kB | 2 | lodash.js
12+
544.10 kB | 71.04 kB | 72.48 kB | 25.78 kB | 26.20 kB | 2 | lodash.js
1313

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

1616
1.01 MB | 439.40 kB | 458.89 kB | 122.06 kB | 126.71 kB | 2 | bundle.min.js
1717

18-
1.25 MB | 642.65 kB | 646.76 kB | 159.38 kB | 163.73 kB | 2 | three.js
18+
1.25 MB | 642.65 kB | 646.76 kB | 159.39 kB | 163.73 kB | 2 | three.js
1919

20-
2.14 MB | 711.11 kB | 724.14 kB | 160.41 kB | 181.07 kB | 2 | victory.js
20+
2.14 MB | 711.15 kB | 724.14 kB | 160.43 kB | 181.07 kB | 2 | victory.js
2121

22-
3.20 MB | 1.00 MB | 1.01 MB | 322.51 kB | 331.56 kB | 3 | echarts.js
22+
3.20 MB | 1.00 MB | 1.01 MB | 322.53 kB | 331.56 kB | 3 | echarts.js
2323

24-
6.69 MB | 2.22 MB | 2.31 MB | 458.38 kB | 488.28 kB | 4 | antd.js
24+
6.69 MB | 2.22 MB | 2.31 MB | 458.41 kB | 488.28 kB | 4 | antd.js
2525

26-
10.95 MB | 3.33 MB | 3.49 MB | 853.27 kB | 915.50 kB | 4 | typescript.js
26+
10.95 MB | 3.33 MB | 3.49 MB | 853.38 kB | 915.50 kB | 4 | typescript.js
2727

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
File | File size || Sys allocs | Sys reallocs || Arena allocs | Arena reallocs | Arena bytes
22
-------------------------------------------------------------------------------------------------------------------------------------------
3-
checker.ts | 2.92 MB || 83481 | 14179 || 152610 | 28185
3+
checker.ts | 2.92 MB || 83500 | 14179 || 152592 | 28237
44

5-
cal.com.tsx | 1.06 MB || 40446 | 3033 || 37151 | 4578
5+
cal.com.tsx | 1.06 MB || 40449 | 3033 || 37146 | 4583
66

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

99
pdf.mjs | 567.30 kB || 19808 | 2899 || 47442 | 7725
1010

11-
antd.js | 6.69 MB || 99498 | 13523 || 331565 | 69251
11+
antd.js | 6.69 MB || 99524 | 13523 || 331573 | 69338
1212

1313
binder.ts | 193.08 kB || 4760 | 974 || 7075 | 824
1414

0 commit comments

Comments
 (0)