Skip to content

Commit f9524b3

Browse files
fix(javascript): fix minified code that threw, read the wrong binding or no longer parsed (#22473)
* test(test262): build production unminified as well Since the minified modes replaced it, production only ran through a minimizer, so a failure of webpack's own production optimizations was listed in MINIFIED_FAILURES as the minimizer's. An unminified production mode tells the two apart. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(test262): accept a parse error only where one is reported or thrown A negative parse test passed on any thrown value, so a bundle that ran into the test's unreachable call passed too. It now needs webpack's parse error or a SyntaxError from the engine. The top-level `return` cases are built as CommonJS, so they are listed as a deliberate divergence. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep a switch whose case test reads a binding declared in its body A case test is evaluated in the switch's block, so the rewrites to `if` and to a block moved it out of the scope of a `let`, `const` or `class` its body declares: `case (f = () => x, 1): let x = 2` became `if (1 == (f = () => x, 1)) { let x = 2 }`, where `x` is no longer declared. The correction leaves such a switch as written, as oxc does. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): minify a function body that declares `arguments` with `let` The minifier declares a function's own `arguments` as a parameter, so a `let` or `const arguments` in the body read as a redeclaration and failed the build, as terser's check does. Under the parameter scopes the correct phase turns on, such a declaration shadows the function's own instead; a parameter written `arguments` beside it the parser still refuses. 12 test262 cases leave the printer's list. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep a spread, a throwing accessor and a strict empty body the minifier dropped An unused `[...it]` or a pure call's `...it` dropped to `it`, skipping the iteration a generator observes. A read of `arguments.callee`, or of a function's `caller` or `arguments`, and a write to one, dropped where strict code throws. An empty function's `"use strict"` went, so its `caller` no longer threw. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep a BigInt operation that throws the minifier dropped An unused `1n + 1`, `1n / 0n`, `2n ** -1n`, `5n >>> 1n` or `Symbol() + 0n` went, and with it the TypeError or RangeError it throws. The check reads only a BigInt literal, a wrapped one or a `Symbol()` call, so an operation on neither costs nothing more. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep a `for` head's `let` in the loop when dropping an unused name beside it Dropping `n` from `for (let i = 0, n = f(); ...)` kept `f()` as a statement, which moved `let i` before the loop: closures reading `i` shared one binding, and two such loops redeclared `i`, a SyntaxError. The unused name now stays in the head, carrying its initializer's effects, as oxc does. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep an async generator's `return undefined`, which awaits `return undefined` or `return void 0` in an async generator awaits the value, a tick a bare `return` does not take; the minifier wrote it as a bare `return`, or dropped it, changing when the generator settles. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep an unused class whose heritage is no constructor `class extends (() => {})` or `class extends 1` throws a TypeError as it is defined; the minifier dropped such a class nobody read. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep an array element's position when dropping an unused update in it `[--b, f(), g()][1]` with `b` read nowhere else lost its first element, so the index read `g()` where it read `f()`; terser's fuzzer found it. The dropped update now leaves a `0` in its place outside a sequence. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep a static member computed as `prototype` computed `static ["prototype"]() {}` throws a TypeError as the class is defined; the minifier wrote it as `static prototype() {}`, an early error that stops the whole script from parsing, and dropped an unused class declaring one, or extending a local async or generator function. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(test262): list as terser's alone the cases the printer now runs right The spreads, `arguments.callee`, function `caller`, BigInt, class heritage and static `prototype` cases, the `for` and `switch` scopes and the async generator's `return undefined` pass with webpack's printer, so each moves from the groups both minifiers fail to terser's own. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): name a catch parameter after the function around it in a switch's discriminant A switch's block scope opens before the function scopes of its discriminant, so counting the scopes opened placed a catch parameter there before its function in the mangler's walk: the parameter took the name an outer variable it reads got next, or kept the name of the `var` it redeclares while that `var`'s reads were renamed. Each catch parameter is now named where the walk meets its own scope; terser's fuzzer found it. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * docs(changeset): fold the printer fixes into one entry Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): drop an empty strict function's directive where it is called in place Nothing reads `caller` of a function called where it is written, so its lone `"use strict"` goes as terser drops it, and the empty call with it, as oxc writes. The corpora list oxc dropping a class extending a regular expression, which throws as it is defined, and no longer list terser refusing `let arguments`: the uncorrected printer refuses it too. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test: keep a spread class key out of the case terser writes alike Spreading a non-iterable throws, which the printer now keeps. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * fix(javascript): keep the conversions, reads and writes oxc keeps that terser assumes away As oxc reads them, an operation converting a side that may be an object or a symbol (`o + 1`, `x - 1`, `-x`, `${o}`), an object spreading what may hold getters, a write to a key that converts or to a primitive's property, a `let` read before its declaration, a getter on an object handed to `Object.defineProperty`, a write to a `const` or a class expression's own name, `this` before `super()`, a class extending itself, a name read in a `with`, and a block's async or generator function read outside it all run or throw. Each is kept where terser dropped it or moved it. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(test262): list as terser's alone the conversions, spreads and TDZ reads the printer now keeps Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): reduce the conversions and spreads kept to what oxc keeps Where oxc proves a conversion or a spread runs no code, drop it: a literal converting with the builtins, a `Math` or `Number` constant, a function's own `arguments` spread, a spread string, function or setter. Where it may run code, keep only that part: a template's or concatenation's conversions (`a${b}c` as `${b}`, `"a" + b` as `"" + b`) and an object's spreads. Also keep a concatenation's conversion where only its truth is read, and merge an async generator's returns wherever that adds or drops no awaited value. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): read a literal as gaining a getter only where it escapes itself terser counts a property read off a literal and stored as the literal escaping, so `var push = array.push` kept every unused `array.slice`. Track the literal's own escape apart. List what swc still writes shorter by dropping conversions oxc keeps, and what oxc drops in a `with` body. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep a class whose static key may name `prototype`, and a block's async function in its block A static member whose computed key a variable may hold as `"prototype"` throws as the class is defined, so the class is kept unless the key is known to be something else. Folding a `switch` no longer writes a block's async or generator function as a `var`, which only a plain function is. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(minify): list the classes oxc drops though a static key may name `prototype` Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * refactor(javascript): read a static key of no known value in `definesStaticPrototype` The check `main` added for a static `prototype` key now also takes a computed key of no known value as one, unless a number or a well-known symbol, so the class heritage check no longer repeats it. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test: list as terser's alone the test262 cases webpack's printer now passes Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * fix(javascript): keep a read of a lexical binding that may run before its declaration `{ x; let x; }`, a `typeof x` before `let x`, a closure read in a `for-of` head of the loop's own binding, and a closure called before its `let` all throw a ReferenceError, which dropping the unused read lost. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * feat(javascript): add `pure_conversions` and keep private names under `keep_fnames` `compress.pure_conversions`, on by default as terser assumes, can be turned off to keep a comparison or a computed key converting what may be an object or a symbol, whose `valueOf`, `toString` or throw then still runs. `keep_fnames: true` now also keeps private names, which a private method's `name` reads. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * feat(javascript): add `pure_heritage`, and keep the name a literal gives a class `compress.pure_heritage`, on by default as terser assumes, can be turned off to keep a class nobody reads where it extends what may be no constructor. Under `keep_classnames` or `keep_fnames`, `{ default: class {} }.default`, which names the class `default`, is no longer flattened into the bare class. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(test262): run the printer with its assumptions off, and keep names in both minified modes test262 reads functions' and classes' `name`, converts objects whose conversions throw, and extends what is no constructor, all on purpose. Both minified modes now keep names, and the printer runs with `pure_conversions` and `pure_heritage` off, so 194 of its 195 listed failures pass and leave the list; terser's name cases leave it too. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * docs(changeset): name the options the printer fixes add Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): keep `pure_conversions` and `pure_heritage` on under `defaults: false` Both name assumptions terser makes whatever its other options are, not passes to turn off with the rest. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(test262): share the options terser supports between both minified modes Both modes keep names and turn `pure_getters` off, each commented with what test262 does on purpose; the printer adds only its own switches. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(test262): drop the 30 cases terser passes with `pure_getters` off Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * fix(javascript): never quote a kept private name terser prints a private member's name as a property's, quoting one it does not take for an identifier, so a kept non-ASCII name printed as `#"ࢶ"`, a SyntaxError. The correct phase prints it as a name, as `this.#x` already is. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b * test(javascript): retire an swc size entry the minifier no longer needs Dropping an empty constructor and the `new` arguments nothing reads now writes `fixture/issues/11684/class-decl` as small as swc does. Claude-Session: https://claude.ai/code/session_019cRZoHW2M4sCbrLFgrAc6b
1 parent 86d31e0 commit f9524b3

10 files changed

Lines changed: 2514 additions & 712 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Fix minified code that threw, and add `pure_conversions` and `pure_heritage`.

‎TESTING_DOCS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Directories come first, in alphabetical order, then the individual files worth t
4040
#### `test262-cases/`
4141

4242
- **Purpose**: ECMAScript test262 conformance test cases.
43-
- **Usage**: Git submodule — initialize with `git submodule update --init test/external/test262-cases`. Test runners: `test/specCases/test262.spectest.js`, which builds and runs each test in development and in production once per JavaScript minimizer — terser, and webpack's printer, as `experiments.futureDefaults` switches it — skipping the `fn-name` cases a renamed binding changes; what each minimizer still fails is listed by reason in its `MINIFIED_FAILURES` table, and a listed case that passes fails the suite until removed. And `test/specCases/minify-corpora.spectest.js`, which minifies each test with webpack's printer and with terser.
43+
- **Usage**: Git submodule — initialize with `git submodule update --init test/external/test262-cases`. Test runners: `test/specCases/test262.spectest.js`, which builds and runs each test in development, in production unminified (a failure there is webpack's own, not a minimizer's), and in production once per JavaScript minimizer: terser, and webpack's printer as `experiments.futureDefaults` switches it. The minified modes skip the `fn-name` cases a renamed binding changes and keep function and class names and turn `pure_getters` off, as test262 reads names and properties that throw; the printer also runs with `pure_conversions` and `pure_heritage` off, as test262 converts and extends values that run code or throw on purpose. What each minimizer still fails is listed by reason in its `MINIFIED_FAILURES` table, and a listed case that passes fails the suite until removed. And `test/specCases/minify-corpora.spectest.js`, which minifies each test with webpack's printer and with terser.
4444

4545
#### `html5lib-tests/`
4646

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,8 @@ class CompressorVariable extends MinifierVariable {
16241624
this.direct_access = false;
16251625
/** @type {number} how many properties are read off it before its value escapes, 0 where it does not */
16261626
this.escaped = 0;
1627+
/** @type {boolean} whether its value itself escapes, not only a property read off it */
1628+
this.leaked = false;
16271629
/** @type {number} how many of its reads are its own function's calls of itself */
16281630
this.recursive_refs = 0;
16291631
/** @type {boolean | string | undefined} whether it is read once, "m" where that read may change it */
@@ -1674,7 +1676,7 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
16741676
* @property {boolean=} ie8 whether catch parameters belong to their function
16751677
* @property {boolean=} safari10 whether a loop's scope reads its parent's names, and catch parameters belong to their function
16761678
* @property {EXPECTED_ANY=} keep_fnames which function names survive, collecting the declarators naming one
1677-
* @property {boolean=} parameterScopes whether a name read in a parameter list or a catch parameter skips what the body declares, as the spec scopes it
1679+
* @property {boolean=} parameterScopes whether names are scoped as the spec scopes them where terser does not: a parameter list's or a catch parameter's reads skip what the body declares, and a block's async or generator function stays in the block
16781680
*/
16791681

16801682
/**
@@ -1684,8 +1686,7 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
16841686
* @property {Map<string, MinifierVariable>} globals the names nothing declares, in the order first read
16851687
* @property {Set<MinifierScope>} blockDefunScopes the function scopes holding a function declared in a block
16861688
* @property {PrintIdentifier[] | null} functionDeclarators under `keep_fnames`, the names declarators give anonymous functions, in the order the mangler asks `keep_fnames` of them
1687-
* @property {PrintIdentifier[]} catchParameters unless `ie8` or `safari10` move them, the catch parameters, in the order the mangler walks them
1688-
* @property {number[]} catchParameterScopeCounts how many scopes the walk had opened at each catch parameter
1689+
* @property {PrintIdentifier[]} catchParameters unless `ie8` or `safari10` move them, the catch parameters, each named where the mangler's walk meets its scope
16891690
*/
16901691

16911692
/**
@@ -1805,8 +1806,6 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
18051806
/** @type {MinifierScope} */
18061807
this.toplevel = new MinifierScope("global", program, null, true);
18071808
this.scope = this.toplevel;
1808-
/** @type {number} how many scopes are open or closed */
1809-
this.scopeCount = 1;
18101809
/** @type {MinifierLabel[]} */
18111810
this.labels = [];
18121811
/** @type {Map<string, MinifierVariable>} */
@@ -1829,11 +1828,9 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
18291828
this.catchNames = [];
18301829
/** @type {MinifierScope[]} */
18311830
this.catchScopes = [];
1832-
// Otherwise the catch parameters, which the mangler names in walk order too.
1831+
// Otherwise the catch parameters, which the mangler names with their scope.
18331832
/** @type {PrintIdentifier[]} */
18341833
this.catchParameters = [];
1835-
/** @type {number[]} */
1836-
this.catchParameterScopeCounts = [];
18371834
/** @type {PrintNode[]} the nodes terser's walk has pushed, the node visited last */
18381835
this.stack = [];
18391836
/** @type {boolean} */
@@ -1859,7 +1856,6 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
18591856
/** @type {MinifierScope} */ (this.scope),
18601857
isVarScope
18611858
);
1862-
this.scopeCount++;
18631859
this.scope = scope;
18641860
return scope;
18651861
}
@@ -2085,10 +2081,17 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
20852081
name,
20862082
kind
20872083
);
2084+
// A body's lexical `arguments` shadows the function's own, which is declared
2085+
// as a parameter here; a parameter so named beside it the parser refuses.
2086+
const shadowsOwnArguments =
2087+
this.parameterScopes &&
2088+
name === "arguments" &&
2089+
earlier === 1 << DECLARES_FUNARG;
20882090
if (
2089-
blockDeclaration
2091+
!shadowsOwnArguments &&
2092+
(blockDeclaration
20902093
? (earlier & ~(1 << DECLARES_LAMBDA)) !== 0
2091-
: (earlier & LEXICAL_DECLARATIONS) !== 0
2094+
: (earlier & LEXICAL_DECLARATIONS) !== 0)
20922095
) {
20932096
this.raise(identifier, `"${name}" is redeclared`);
20942097
}
@@ -2110,7 +2113,6 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
21102113
this.catchScopes.push(scope);
21112114
} else {
21122115
this.catchParameters.push(identifier);
2113-
this.catchParameterScopeCounts.push(this.scopeCount);
21142116
}
21152117
}
21162118
}
@@ -2152,7 +2154,9 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
21522154
} else if (id.role === "defun") {
21532155
this._markExport(
21542156
this._defineVariable(
2155-
this.strict
2157+
// Annex B hoists a sloppy block's plain function only.
2158+
this.strict ||
2159+
(this.parameterScopes && (node.async || node.generator))
21562160
? outer
21572161
: /** @type {MinifierScope} */ (outer.variableScope),
21582162
id,
@@ -2754,8 +2758,7 @@ const analyzeMinifierScope = (program, options, raise, release) => {
27542758
globals: analyzer.globals,
27552759
blockDefunScopes: analyzer.blockDefunScopes,
27562760
functionDeclarators: analyzer.functionDeclarators,
2757-
catchParameters: analyzer.catchParameters,
2758-
catchParameterScopeCounts: analyzer.catchParameterScopeCounts
2761+
catchParameters: analyzer.catchParameters
27592762
};
27602763
};
27612764

@@ -2849,7 +2852,6 @@ class CompressorScopeAnalyzer extends MinifierScopeAnalyzer {
28492852
);
28502853
// eslint-disable-next-line camelcase -- terser's name, which the compressor reads
28512854
opener.block_scope = scope;
2852-
this.scopeCount++;
28532855
this.scope = /** @type {MinifierScope} */ (/** @type {unknown} */ (scope));
28542856
return this.scope;
28552857
}

0 commit comments

Comments
 (0)