Skip to content

Commit 122e921

Browse files
fix(minifier): keep a dead-zone read among extra constructor arguments
An extra argument is dropped only where `dropSideEffectFree` leaves nothing, so `new F(1, x)` before `let x` still throws. A `let` read as nullish once its initializer is dropped must also be one nothing writes. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent eaf22c6 commit 122e921

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24904,10 +24904,12 @@ const installImprove = (modules) => {
2490424904
args.length > read &&
2490524905
!args.some((/** @type {Node} */ arg) => A.isExpansionNode(arg))
2490624906
) {
24907-
// An argument past them only runs, so one without effects goes.
24907+
// An argument past them only runs, so one without effects goes; a
24908+
// read that may throw, as in a dead zone, stays.
2490824909
let kept = read;
2490924910
for (let i = read; i < args.length; i++) {
24910-
if (A.hasSideEffects(args[i], compressor)) args[kept++] = args[i];
24911+
const effects = A.dropSideEffectFree(args[i], compressor);
24912+
if (effects) args[kept++] = effects;
2491124913
}
2491224914
args.length = kept;
2491324915
}
@@ -43535,7 +43537,9 @@ const createCompressHelpers = (modules) => {
4353543537
// A `let` whose `void 0` was dropped still reads as `undefined`.
4353643538
if (
4353743539
fixed === null &&
43538-
ast.isSymbolLetNode(node.definition.identifiers[0])
43540+
ast.isSymbolLetNode(node.definition.identifiers[0]) &&
43541+
node.definition.assignments === 0 &&
43542+
node.definition.patternWrites === 0
4353943543
) {
4354043544
return true;
4354143545
}

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8776,6 +8776,17 @@ describe("syntax-printer", () => {
87768776
});
87778777
}
87788778

8779+
it("should keep an extra constructor argument reading a `let` in its dead zone, and an optional call of a `let` written again", async () => {
8780+
const { minify } = await load();
8781+
for (const input of [
8782+
"function g() { try { return new function (v) { this.v = v; }(1, x).v; } catch (e) { return e.name; } let x; } console.log(g());",
8783+
"function f() { let d = void 0; d ||= function () { console.log(\"called\"); }; d?.(console.log(\"effect\")); return 1; } console.log(f());"
8784+
]) {
8785+
const { code } = await minify(input, { compress: {}, mangle: false });
8786+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));
8787+
}
8788+
});
8789+
87798790
it("should keep two calls apart where a getter may write the parameter the test reads through `arguments`", async () => {
87808791
const { minify } = await load();
87818792
const input =

0 commit comments

Comments
 (0)