Skip to content

Commit 0bd70ef

Browse files
fix(minifier): mangle a for loop the compressor left without a head
Also list the corpus sources printing differently only by what the improve phase assumes, as swc and oxc do. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 975970a commit 0bd70ef

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,12 +1812,13 @@ const bindsName = (node, name) => {
18121812
};
18131813

18141814
/**
1815-
* @param {PrintNode | null} node a statement, or a loop's head
1815+
* @param {PrintNode | null | undefined} node a statement, or a loop's head, which a compressed `for` may lack
18161816
* @param {string} name a name
18171817
* @returns {boolean} whether it declares the name with `let`, `const`, `using` or a class
18181818
*/
18191819
const declaresLexically = (node, name) =>
18201820
node !== null &&
1821+
node !== undefined &&
18211822
(node.type === "VariableDeclaration"
18221823
? node.kind !== "var" &&
18231824
node.declarations.some((declarator) =>

‎test/specCases/minify-corpora.spectest.js‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,29 @@ const INHERITED = {
952952
"`pure_getters` says property reads have no effects, so dropping the getter is what it asks"
953953
};
954954

955+
// The `improve` phase takes a read of a global to have no effect and never
956+
// throw, as swc and oxc do, so a source probing a missing one prints less.
957+
const GLOBAL_READ = "a read of a global is taken never to throw, as swc and oxc take it";
958+
959+
/**
960+
* Sources whose output prints differently only by what the `improve` phase
961+
* assumes, as swc and oxc do, each with the assumption; an entry that starts
962+
* printing what the test says fails until retired.
963+
* @type {Record<string, string>}
964+
*/
965+
const ASSUMED = {
966+
"terser compress: arrow/call_args_drop_param (its own options)": GLOBAL_READ,
967+
"terser compress: arrow/call_args_drop_param (the default minimizer's options)":
968+
GLOBAL_READ,
969+
"terser compress: arrow/call_args_drop_param (reminify 3 {})": GLOBAL_READ,
970+
"terser compress: evaluate/call_args_drop_param (its own options)": GLOBAL_READ,
971+
"terser compress: evaluate/call_args_drop_param (the default minimizer's options)":
972+
GLOBAL_READ,
973+
"terser compress: evaluate/call_args_drop_param (reminify 3 {})": GLOBAL_READ,
974+
"terser compress: switch/issue_1705_3 (the default minimizer's options)":
975+
GLOBAL_READ
976+
};
977+
955978
/**
956979
* Sources terser misprints under a set and webpack's `correct` phase prints
957980
* right, each with the defect: without the phase each must misprint as terser's.
@@ -2478,12 +2501,18 @@ describe("JavaScript minifier", () => {
24782501
const corrected = Boolean(
24792502
printer.corrections && printer.corrections.enabled
24802503
);
2504+
const improved = Boolean(
2505+
printer.improvements && printer.improvements.enabled
2506+
);
24812507
const table = Object.prototype.hasOwnProperty.call(
24822508
INHERITED,
24832509
key
24842510
)
24852511
? "INHERITED"
2486-
: !corrected &&
2512+
: improved &&
2513+
Object.prototype.hasOwnProperty.call(ASSUMED, key)
2514+
? "ASSUMED"
2515+
: !corrected &&
24872516
Object.prototype.hasOwnProperty.call(CORRECTED, key)
24882517
? "CORRECTED"
24892518
: undefined;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9376,6 +9376,14 @@ describe("syntax-printer", () => {
93769376
}
93779377
});
93789378

9379+
it("should mangle a loop without a head declaring a sloppy block's function", async () => {
9380+
const { minify } = await load();
9381+
const input =
9382+
'while (!((foo && bar) || x + "0")) { console.log("unreachable"); var foo; function bar() {} } for (var x = 10, y; x && (y || x) && !typeof x; ++x) { var moo; } console.log(typeof bar);';
9383+
const { code } = await minify(input, { compress: {} });
9384+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));
9385+
});
9386+
93799387
it("should keep a call after the read of a global it writes where no `reduce_vars` counts the writes", async () => {
93809388
const { minify } = await load();
93819389
const input =

0 commit comments

Comments
 (0)