Skip to content

Commit aa41f7f

Browse files
fix(javascript): keep what fuzzing modules, classes and bundles found the parser and minifier changed (#22611)
* fix(javascript): read a regular expression after `yield` in a generator method The tokenizer took a generator from the `function` keyword's context, which a method's `*` never pushes, so `*m() { yield /a/ }` read `/` as division. A parse now reads the enclosing scope instead. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * fix(javascript): keep what fuzzing modules, classes and bundles found the minifier changed - Parenthesize a named function or class expression after `export default`, and one a member, call or tag after it starts. - Keep a sloppy function out of a class or other strict code where it reads `this`, `arguments` or `with`, deletes, or writes a property or an undeclared name. - Keep an object `hoist_props` would split whole where it is read bare. - Read `NaN` and `Infinity` in a catch clause whose parameter is a pattern, where the minifier threw. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * fix(javascript): read `yield` after a nested function body in the enclosing generator The token after a function body's `}` is read before its scope exits, so `function* g() { function f() {} yield /a/g; }` read `/` as division and failed to parse. The `yield` there is now classified again once the scope exits. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * fix(javascript): read a function's `"use strict"` directive when keeping sloppy code out of a class The check read `value`, which a directive has no field for, so a function already strict by a directive was kept out of a class too. It reads `directive` now, which leaves emotion, syncfusion, 2257 and 7568 as small as before and 8173 back among the cases swc minifies smaller. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * fix(javascript): classify `yield` after a function body without base parser code on the lazy path The lazy path runs no base prototype method, so it checks the token after a body itself instead of calling `rereadYieldContext`. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * fix(javascript): keep a sloppy function writing a `for-in` or `for-of` target out of a class A loop head writes its target without an assignment, so a function that creates a global through `for (x of ...)` was still moved into a class, where the write throws. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * test(javascript): drop the `vercel/006` corpus entry the rebase onto main made stale With the minifier fixes from main, its own options no longer print it bigger than swc, so the entry lists a difference that is gone. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4
1 parent 2289141 commit aa41f7f

7 files changed

Lines changed: 358 additions & 12 deletions

File tree

‎.changeset/099-import-then-assert-call.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Parse an `assert(...)` call on the line after an import with no semicolon.
5+
Parse an `assert(...)` call after an import with no semicolon, and a regular expression after `yield` in a generator method.

‎lib/javascript/syntax-parser.js‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,8 +3550,13 @@ class SyntaxParser {
35503550
return !this.exprAllowed;
35513551
}
35523552

3553-
/** @returns {boolean} whether the enclosing function is a generator */
3553+
/**
3554+
* Whether the enclosing function is a generator. A parse reads its scope: a
3555+
* method's `*` pushes no `function` context, which only the tokenizer reads.
3556+
* @returns {boolean} true where `yield` is an operator
3557+
*/
35543558
inGeneratorContext() {
3559+
if (this.scopeStack.length > 0) return this.inGenerator;
35553560
for (let i = this.context.length - 1; i >= 1; i--) {
35563561
const context = this.context[i];
35573562
if (context.token === "function") return Boolean(context.generator);
@@ -5263,6 +5268,18 @@ class SyntaxParser {
52635268
this.labels = oldLabels;
52645269
}
52655270
this.exitScope();
5271+
this.rereadYieldContext();
5272+
}
5273+
5274+
/**
5275+
* The token after a function body is read in the body's scope, so a `yield`
5276+
* there is classified again in the enclosing one.
5277+
* @returns {void}
5278+
*/
5279+
rereadYieldContext() {
5280+
if (this.type === tokTypes.name && this.value === "yield") {
5281+
this.exprAllowed = this.inGeneratorContext();
5282+
}
52665283
}
52675284

52685285
/**
@@ -9885,8 +9902,13 @@ class WebpackParser extends BaseParser {
98859902
}
98869903
}
98879904

9888-
/** @returns {boolean} whether the enclosing function is a generator */
9905+
/**
9906+
* Whether the enclosing function is a generator. A parse reads its scope: a
9907+
* method's `*` pushes no `function` context, which only the tokenizer reads.
9908+
* @returns {boolean} true where `yield` is an operator
9909+
*/
98899910
inGeneratorContext() {
9911+
if (this.scopeStack.length > 0) return this.inGenerator;
98909912
for (let i = this.context.length - 1; i >= 1; i--) {
98919913
const context = this.context[i];
98929914
if (context.token === "function") {
@@ -13881,6 +13903,10 @@ class WebpackParser extends BaseParser {
1388113903
this._exitedHoisted = undefined;
1388213904
}
1388313905
this.exitScope();
13906+
// the lazy path runs no base prototype code: `rereadYieldContext` inlined
13907+
if (this.type === tokTypes.name && this.value === "yield") {
13908+
this.exprAllowed = this.inGeneratorContext();
13909+
}
1388413910
}
1388513911

1388613912
/**

‎lib/javascript/syntax-printer.js‎

Lines changed: 172 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,7 +3940,8 @@ const installEquivalent = ({ ast }) => {
39403940
* @param {MinifierModules} modules terser's modules
39413941
* @returns {void}
39423942
*/
3943-
const installHoist = ({ ast, common, utils }) => {
3943+
const installHoist = (modules) => {
3944+
const { ast, common, utils } = modules;
39443945
const { transformNode } = ast;
39453946
const {
39463947
isExpansionNode,
@@ -4102,9 +4103,46 @@ const installHoist = ({ ast, common, utils }) => {
41024103
node.object.definition.id;
41034104
}
41044105
});
4105-
return splits === undefined ? self : hoist(self, splits);
4106+
if (
4107+
splits !== undefined &&
4108+
modules.corrections !== undefined &&
4109+
modules.corrections.enabled
4110+
) {
4111+
// terser splits an object read bare where the read stays, as `o;` or
4112+
// `typeof o`, which the split leaves reading a name nothing declares.
4113+
const bare = definitionsReadBare(scope);
4114+
for (const node of splits) {
4115+
if (bare.has(node.id.definition)) splits.delete(node);
4116+
}
4117+
}
4118+
return splits === undefined || splits.size === 0
4119+
? self
4120+
: hoist(self, splits);
41064121
}
41074122

4123+
/**
4124+
* @param {Node} scope the node of the scope
4125+
* @returns {Set<SymbolDefinition>} the definitions a reference in it reads other
4126+
* than as the object of a property access
4127+
*/
4128+
const definitionsReadBare = (scope) => {
4129+
/** @type {Set<Node>} */
4130+
const objects = new Set();
4131+
/** @type {Set<SymbolDefinition>} */
4132+
const bare = new Set();
4133+
walk(scope, (/** @type {Node} */ node) => {
4134+
if (
4135+
node[hoistKindOf] & HOIST_PROP_ACCESS &&
4136+
isSymbolRefNode(node.object)
4137+
) {
4138+
objects.add(node.object);
4139+
} else if (isSymbolRefNode(node) && !objects.has(node)) {
4140+
bare.add(node.definition);
4141+
}
4142+
});
4143+
return bare;
4144+
};
4145+
41084146
ast.hoistProperties = hoistProperties;
41094147
};
41104148

@@ -17257,13 +17295,92 @@ const installCorrect = (modules) => {
1725717295
return undefined;
1725817296
};
1725917297

17298+
/**
17299+
* Whether a function is sloppy code: no `"use strict"` in it or around it,
17300+
* no class around it, and no module.
17301+
* @param {Node} lambda the function
17302+
* @param {CompressorShape} compressor the compressor
17303+
* @returns {boolean} true where its code would read otherwise as strict
17304+
*/
17305+
const isSloppy = (lambda, compressor) => {
17306+
if (compressor.option("module")) return false;
17307+
for (
17308+
let scope = /** @type {Scope | null} */ (lambda.block_scope);
17309+
scope;
17310+
scope = scope.upper
17311+
) {
17312+
const { node } = scope;
17313+
if (A.isClassNode(node)) return false;
17314+
if (A.isLambdaNode(node) || A.isToplevelNode(node)) {
17315+
for (const statement of A.bodyOf(node)) {
17316+
if (!A.isDirectiveNode(statement)) break;
17317+
if (statement.directive === "use strict") return false;
17318+
}
17319+
}
17320+
}
17321+
return true;
17322+
};
17323+
/**
17324+
* Whether code would run otherwise as strict: it reads `this`, `arguments`
17325+
* or `with`, deletes, or writes a property or an undeclared name.
17326+
* @param {Node} lambda the function
17327+
* @returns {boolean} true where one of them is in it
17328+
*/
17329+
const readsStrictness = (lambda) =>
17330+
Boolean(
17331+
A.walk(lambda, (/** @type {Node} */ node) => {
17332+
if (
17333+
A.isThisNode(node) ||
17334+
A.isWithNode(node) ||
17335+
(A.isSymbolRefNode(node) && node.name === "arguments")
17336+
) {
17337+
return A.walk_abort;
17338+
}
17339+
if (A.isUnaryNode(node) && node.operator === "delete") {
17340+
return A.walk_abort;
17341+
}
17342+
const target =
17343+
A.isAssignNode(node) || A.isForInNode(node) || A.isForOfNode(node)
17344+
? node.left
17345+
: A.isUnaryNode(node) &&
17346+
(node.operator === "++" || node.operator === "--")
17347+
? node.argument
17348+
: undefined;
17349+
return target !== undefined &&
17350+
(A.isPropAccessNode(target) ||
17351+
A.isDestructuringNode(target) ||
17352+
(A.isSymbolRefNode(target) && target.definition.undeclared))
17353+
? A.walk_abort
17354+
: undefined;
17355+
})
17356+
);
17357+
/**
17358+
* @param {Node} lambda a function
17359+
* @param {CompressorShape} compressor the compressor at strict code or not
17360+
* @returns {boolean} whether moving it there would run sloppy code as strict
17361+
*/
17362+
const runsSloppyAsStrict = (lambda, compressor) =>
17363+
corrections.enabled &&
17364+
compressor.has_directive("use strict") &&
17365+
isSloppy(lambda, compressor) &&
17366+
readsStrictness(lambda);
17367+
// terser moves a sloppy function into strict code, a class above all, where
17368+
// writing a primitive's property or an undeclared name throws, and `this`
17369+
// is no longer the global object.
17370+
modules.webpackInline.keepsSloppyLambda = runsSloppyAsStrict;
17371+
1726017372
// terser inlines a function naming `await` or `yield` into an async function
1726117373
// or a generator, where the name is a keyword, so its output does not parse.
1726217374
// Only the inline is held back, so the call still optimizes otherwise.
1726317375
wrapOptimize("Call", (original) => (self, compressor) => {
17376+
const lambda = inlinedLambda(self);
17377+
if (lambda !== undefined && runsSloppyAsStrict(lambda, compressor)) {
17378+
return withOptionsOff(compressor, ["inline"], () =>
17379+
original(self, compressor)
17380+
);
17381+
}
1726417382
const reserved = reservedAround(compressor);
1726517383
if (reserved === undefined) return original(self, compressor);
17266-
const lambda = inlinedLambda(self);
1726717384
if (lambda === undefined || !namesInOwnContext(lambda, reserved)) {
1726817385
return original(self, compressor);
1726917386
}
@@ -30115,6 +30232,7 @@ const createTightenBody = (modules) => {
3011530232
* @property {((call: Node, reference: Node, value: Node) => boolean) | null} inlinesIntoPureArgument whether a value read once may replace its read in a call terser holds pure, which it otherwise keeps out
3011630233
* @property {((reference: Node, definition: SymbolDefinition, compressor: CompressorShape) => boolean) | null} inlinesIntoCallee whether a function read once, in another scope, is inlined where its value is only called
3011730234
* @property {((call: Node, lambda: Node, index: number, compressor: CompressorShape) => boolean) | null} aliasesParameter whether a parameter of a function flattened into its call, its name taken around the call, reads the argument's variable instead
30235+
* @property {((lambda: Node, compressor: CompressorShape) => boolean) | null} keepsSloppyLambda whether a sloppy function read once stays where strict code would run it
3011830236
*/
3011930237

3012030238
/**
@@ -30197,7 +30315,8 @@ const createInline = (modules) => {
3019730315
const inlineHooks = {
3019830316
inlinesIntoPureArgument: null,
3019930317
inlinesIntoCallee: null,
30200-
aliasesParameter: null
30318+
aliasesParameter: null,
30319+
keepsSloppyLambda: null
3020130320
};
3020230321
modules.webpackInline = inlineHooks;
3020330322

@@ -30352,6 +30471,15 @@ const createInline = (modules) => {
3035230471
}
3035330472
}
3035430473

30474+
if (
30475+
singleUse &&
30476+
isLambdaNode(fixed) &&
30477+
inlineHooks.keepsSloppyLambda !== null &&
30478+
inlineHooks.keepsSloppyLambda(fixed, compressor)
30479+
) {
30480+
singleUse = false;
30481+
}
30482+
3035530483
if (singleUse && (isLambdaNode(fixed) || isClassNode(fixed))) {
3035630484
nearestScope = /** @type {Scope} */ (compressor.find_scope());
3035730485
singleUse =
@@ -36816,7 +36944,11 @@ const installOptimize = (modules) => {
3681636944
break;
3681736945
}
3681836946
if (A.isCatchNode(node) && node.param) {
36819-
scope = node.param.definition.scope;
36947+
// A pattern declares in the clause's own scope; terser read its
36948+
// `definition`, which only a lone name has.
36949+
scope = node.param.definition
36950+
? node.param.definition.scope
36951+
: node.block_scope;
3682036952
break;
3682136953
}
3682236954
}
@@ -37597,16 +37729,25 @@ const installEstreePrint = (modules) => {
3759737729
/**
3759837730
* terser's `first_in_statement`, over the stream's parent stack.
3759937731
* @param {OutputStream} output the stream printed into
37732+
* @param {boolean=} exportDefault whether an `export default` value it starts, but is not, counts
3760037733
* @returns {boolean | undefined} whether the node printed starts its statement
3760137734
*/
37602-
const firstInStatement = (output) => {
37735+
const firstInStatement = (output, exportDefault) => {
3760337736
let node = /** @type {PrintParent} */ (output.parent(-1));
3760437737
/** @type {PrintParent | undefined} */
3760537738
let parent;
3760637739
for (let i = 0; (parent = output.parent(i)); i++) {
3760737740
if (parent.type === "ExpressionStatement" && parent.expression === node) {
3760837741
return true;
3760937742
}
37743+
if (
37744+
exportDefault &&
37745+
i > 0 &&
37746+
parent.type === "ExportDefaultDeclaration" &&
37747+
/** @type {PrintExport} */ (parent).exported_value === node
37748+
) {
37749+
return true;
37750+
}
3761037751
if (
3761137752
(parent.type === "SequenceExpression" &&
3761237753
parent.expressions[0] === node) ||
@@ -38746,6 +38887,21 @@ const installEstreePrint = (modules) => {
3874638887
output.space();
3874738888
output.print("default");
3874838889
output.space();
38890+
const { corrections } = modules;
38891+
// terser writes a named function or class expression bare, which reads
38892+
// back as a declaration binding its name in the module.
38893+
if (
38894+
node.exported_value &&
38895+
(declaration.type === "FunctionExpression" ||
38896+
declaration.type === "ClassExpression") &&
38897+
declaration.id &&
38898+
corrections !== undefined &&
38899+
corrections.enabled
38900+
) {
38901+
output.with_parens(() => printEstree(declaration, output));
38902+
output.semicolon();
38903+
return;
38904+
}
3874938905
printEstree(declaration, output);
3875038906
if (
3875138907
!kindOf.Class(declaration) &&
@@ -39229,10 +39385,17 @@ const installEstreePrint = (modules) => {
3922939385
);
3923039386
};
3923139387

39388+
// terser prints `export default (class {}).name` bare, which does not parse:
39389+
// `export default` reads a leading `class` or `function` as a declaration.
39390+
const correctsExport = () =>
39391+
modules.corrections !== undefined && modules.corrections.enabled;
39392+
3923239393
/** @type {EstreeParensTests} */
3923339394
const needsParens = {
3923439395
FunctionExpression: (node, output) => {
39235-
if (!output.has_parens() && firstInStatement(output)) return true;
39396+
if (!output.has_parens() && firstInStatement(output, correctsExport())) {
39397+
return true;
39398+
}
3923639399
const parent = parentOf(output, 0);
3923739400
if (
3923839401
output.option("webkit") &&
@@ -39271,7 +39434,8 @@ const installEstreePrint = (modules) => {
3927139434
// Otherwise `{...}` would read as a block.
3927239435
ObjectExpression: (node, output) =>
3927339436
!output.has_parens() && firstInStatement(output),
39274-
ClassExpression: (node, output) => firstInStatement(output),
39437+
ClassExpression: (node, output) =>
39438+
firstInStatement(output, correctsExport()),
3927539439
UnaryExpression: unaryNeedsParens,
3927639440
UpdateExpression: unaryNeedsParens,
3927739441
AwaitExpression: (node, output) => {

‎test/helpers/swcSmaller.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,6 @@ const SWC_SMALLER_BY_REASON = [
543543
"swc minifier: fixture/projects/wmr/archive-1/chunks/index.5a544c41/input.js",
544544
"swc minifier: fixture/pure-annotations-not-member-pattern/input.js",
545545
"swc minifier: full/helpers/1/input.js",
546-
"terser compress: classes/class_duplication",
547546
"terser compress: export/issue_2126",
548547
"terser compress: export/redirection"
549548
]

0 commit comments

Comments
 (0)