You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 593815b
Browse filesBrowse the repository at this point in the historyBrowse files
feat(minify): drop writes to a let nothing reads from closures created after it (#22960)
* feat(minify): drop writes to a `let` nothing reads from closures created after it
* fix(minify): keep a write to a `for…of` or `for…in` head from its right side
* docs(changeset): shorten the closure let-writes changeset to one line
* test(minify): assert the `switch` case's own write stays
* fix(minify): count a `for…of` head's `let` as set once its right side has run
* test(minify): snapshot the printed loop-head cases
"oxc's output is wrong: it drops a write in a `for…of` or `for…in` right side to the loop's own `let`, which throws a ReferenceError there, as its own test keeps it",
"oxc drops an unread deferred or source-phase import, assuming its load cannot fail, where webpack keeps it as oxc's `invalid_import_side_effects` does, since it still fetches and links its module",
Copy file name to clipboardExpand all lines: test/unitCases/javascript/__snapshots__/printer.unittest.js.snap
+8Lines changed: 8 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2847,6 +2847,12 @@ Array [
2847
2847
]
2848
2848
`;
2849
2849
2850
+
exports[`syntax-printer improve phase should drop a write to a \`for…of\` head's \`let\` from its body, not its right side 1`] = `"export function f(a){let p;for(let x of a)p=()=>{};return p}"`;
2851
+
2852
+
exports[`syntax-printer improve phase should drop a write to a \`for…of\` head's \`let\` from its body, not its right side 2`] = `"export function f(a){for(let x of a)h(1)}"`;
2853
+
2854
+
exports[`syntax-printer improve phase should drop a write to a \`for…of\` head's \`let\` from its body, not its right side 3`] = `"export function f(a){let p;for(let x of a)p=()=>x;return p}"`;
2855
+
2850
2856
exports[`syntax-printer improve phase should keep a function called in place whose \`var\`, its name taken, a class in a block there also names 1`] = `"function f(x){var a=g();!function(){var x=typeof a;{class x{}console.log(x.name)}console.log(x)}(),console.log(x,a)}function g(){return 1}f(\\"o\\");"`;
2851
2857
2852
2858
exports[`syntax-printer improve phase should keep a function called in place whose \`var\`s, more than one, take names already taken 1`] = `"function f(x){var a=g(),b=g();console.log(x),function(){if(x){var a=console.log.name,b=typeof x;console.log(a,a,b,b)}}(),console.log(a,b)}function g(){return\\"o\\"}f(1),f(0);"`;
@@ -2989,6 +2995,8 @@ exports[`syntax-printer improve phase should write less, printing the same: a \`
2989
2995
2990
2996
exports[`syntax-printer improve phase should write less, printing the same: a \`let\` before a loop assigning it first, given that value where it stands, but where the value reads it 1`] = `"function f(n){let i=0,j;for(;i<n;i++);for(j=()=>j;i<3;i++);return[i,typeof j]}console.log(f(2));"`;
2991
2997
2998
+
exports[`syntax-printer improve phase should write less, printing the same: a \`let\` closures created after its declaration write and nothing reads 1`] = `"function f(a){return[1,2].forEach(v=>{}),a+1}console.log(f(1));[3].forEach(function(v){});"`;
2999
+
2992
3000
exports[`syntax-printer improve phase should write less, printing the same: a \`let\` declaration beside one written from a \`const\`, joined, its names mangled 1`] = `"function f(o){let n=o.x,t=o.y;t++;for(let o of[n,t]){let c=o+t,f=[c,c];f.push(n),console.log(f)}}f({x:1,y:2});"`;
2993
3001
2994
3002
exports[`syntax-printer improve phase should write less, printing the same: a \`let\` of a \`switch\` case a later case reads, kept 1`] = `"function f(x){switch(x){case 1:let a=[x];case 2:return a}}console.log(f(1));"`;
Copy file name to clipboardExpand all lines: test/unitCases/javascript/printer.unittest.js
+39-1Lines changed: 39 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -495,6 +495,11 @@ const IMPROVED_CASES = [
495
495
"function m() { var t = typeof f; function f() { return x; } const x = 2; let r = []; try { r.push(z); } catch (e) { r.push(e.name); } let z = x + 1; r.push(t, f(), x * 3, z * z); return r; } console.log(m());",
496
496
{ compress: {}, mangle: false }
497
497
],
498
+
[
499
+
"a `let` closures created after its declaration write and nothing reads",
500
+
"function f(a) { let x, k = function (v) { x = v; return v + 1; }; [1, 2].forEach((v) => { x = v; }); return k(a); } console.log(f(1)); { let y; [3].forEach(function (v) { y = v; }); }",
501
+
{ compress: {}, mangle: false }
502
+
],
498
503
[
499
504
"a `let` whose every read folds into its value, left with its write",
500
505
"(function () { let x; x = 2; console.log(x); globalThis.g = function () { return x; }; })(); console.log(g());",
@@ -11421,6 +11426,22 @@ x = a ** b ** c; x = (a ** b) ** c; x = (-a) ** b; x = a < b < c; x = a < (b < c
11421
11426
}
11422
11427
});
11423
11428
11429
+
it("should drop a write to a `for…of` head's `let` from its body, not its right side", async () => {
11430
+
const { minify } = await load();
11431
+
for (const input of [
11432
+
"export function f(a){let p;for(let x of a)p=()=>{x=3};return p}",
11433
+
"export function f(a){for(let x of a)x=1,h(x)}",
11434
+
"export function f(a){let p;for(const x of a)p=()=>x;return p}"
11435
+
]) {
11436
+
const { code } = await minify(input, {
11437
+
module: true,
11438
+
compress: {},
11439
+
mangle: false
11440
+
});
11441
+
expect(code).toMatchSnapshot();
11442
+
}
11443
+
});
11444
+
11424
11445
it("should keep a write to a `let` that may run before its declaration", async () => {
11425
11446
const { minify } = await load();
11426
11447
for (const input of [
@@ -11429,7 +11450,14 @@ x = a ** b ** c; x = (a ** b) ** c; x = (-a) ** b; x = a < b < c; x = a < (b < c
11429
11450
"export function f(){for(;;){x=1;let x}}",
11430
11451
"export function f(){let x;window.h=()=>x;x=2}",
11431
11452
"export function f(){g();let x;function g(){x=1}}",
11432
-
"export function f(){class C{static{x=1}}let x}"
11453
+
"export function f(){class C{static{x=1}}let x}",
11454
+
"export function f(){h(()=>{x=1});let x}",
11455
+
"export function f(){let x=h(()=>{x=1})}",
11456
+
"export function f(){let x;h(()=>{x=1});h(()=>x)}",
11457
+
"export function f(){for(let x of(g(()=>{x=1}),[]));}",
11458
+
"export function f(){for(let x in(x=1,{}));}",
11459
+
"export function f(){const w=()=>{x=1};h(w),h(w);let x}",
11460
+
"export function f(){let y;const w=()=>{x=1};h(w),h(w);let x}"
11433
11461
]) {
11434
11462
const { code } = await minify(input, {
11435
11463
module: true,
@@ -11438,6 +11466,16 @@ x = a ** b ** c; x = (a ** b) ** c; x = (-a) ** b; x = a < b < c; x = a < (b < c
11438
11466
});
11439
11467
expect(code).toMatch(/x=[12]/);
11440
11468
}
11469
+
const { code: inSwitch } = await minify(
11470
+
"export function f(a){switch(a){case 0:let x;h(()=>{x=1});break;case 1:h(()=>{x=2})}}",
0 commit comments