Skip to content

Commit 483d73b

Browse files
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
1 parent d4ff324 commit 483d73b

5 files changed

Lines changed: 272 additions & 10 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 171 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

@@ -17245,13 +17283,91 @@ const installCorrect = (modules) => {
1724517283
return undefined;
1724617284
};
1724717285

17286+
/**
17287+
* Whether a function is sloppy code: no `"use strict"` in it or around it,
17288+
* no class around it, and no module.
17289+
* @param {Node} lambda the function
17290+
* @param {CompressorShape} compressor the compressor
17291+
* @returns {boolean} true where its code would read otherwise as strict
17292+
*/
17293+
const isSloppy = (lambda, compressor) => {
17294+
if (compressor.option("module")) return false;
17295+
for (
17296+
let scope = /** @type {Scope | null} */ (lambda.block_scope);
17297+
scope;
17298+
scope = scope.upper
17299+
) {
17300+
const { node } = scope;
17301+
if (A.isClassNode(node)) return false;
17302+
if (A.isLambdaNode(node) || A.isToplevelNode(node)) {
17303+
for (const statement of A.bodyOf(node)) {
17304+
if (!A.isDirectiveNode(statement)) break;
17305+
if (statement.value === "use strict") return false;
17306+
}
17307+
}
17308+
}
17309+
return true;
17310+
};
17311+
/**
17312+
* Whether code would run otherwise as strict: it reads `this`, `arguments`
17313+
* or `with`, deletes, or writes a property or an undeclared name.
17314+
* @param {Node} lambda the function
17315+
* @returns {boolean} true where one of them is in it
17316+
*/
17317+
const readsStrictness = (lambda) =>
17318+
Boolean(
17319+
A.walk(lambda, (/** @type {Node} */ node) => {
17320+
if (
17321+
A.isThisNode(node) ||
17322+
A.isWithNode(node) ||
17323+
(A.isSymbolRefNode(node) && node.name === "arguments")
17324+
) {
17325+
return A.walk_abort;
17326+
}
17327+
if (A.isUnaryNode(node) && node.operator === "delete") {
17328+
return A.walk_abort;
17329+
}
17330+
const target = A.isAssignNode(node)
17331+
? node.left
17332+
: A.isUnaryNode(node) &&
17333+
(node.operator === "++" || node.operator === "--")
17334+
? node.argument
17335+
: undefined;
17336+
return target !== undefined &&
17337+
(A.isPropAccessNode(target) ||
17338+
A.isDestructuringNode(target) ||
17339+
(A.isSymbolRefNode(target) && target.definition.undeclared))
17340+
? A.walk_abort
17341+
: undefined;
17342+
})
17343+
);
17344+
/**
17345+
* @param {Node} lambda a function
17346+
* @param {CompressorShape} compressor the compressor at strict code or not
17347+
* @returns {boolean} whether moving it there would run sloppy code as strict
17348+
*/
17349+
const runsSloppyAsStrict = (lambda, compressor) =>
17350+
corrections.enabled &&
17351+
compressor.has_directive("use strict") &&
17352+
isSloppy(lambda, compressor) &&
17353+
readsStrictness(lambda);
17354+
// terser moves a sloppy function into strict code, a class above all, where
17355+
// writing a primitive's property or an undeclared name throws, and `this`
17356+
// is no longer the global object.
17357+
modules.webpackInline.keepsSloppyLambda = runsSloppyAsStrict;
17358+
1724817359
// terser inlines a function naming `await` or `yield` into an async function
1724917360
// or a generator, where the name is a keyword, so its output does not parse.
1725017361
// Only the inline is held back, so the call still optimizes otherwise.
1725117362
wrapOptimize("Call", (original) => (self, compressor) => {
17363+
const lambda = inlinedLambda(self);
17364+
if (lambda !== undefined && runsSloppyAsStrict(lambda, compressor)) {
17365+
return withOptionsOff(compressor, ["inline"], () =>
17366+
original(self, compressor)
17367+
);
17368+
}
1725217369
const reserved = reservedAround(compressor);
1725317370
if (reserved === undefined) return original(self, compressor);
17254-
const lambda = inlinedLambda(self);
1725517371
if (lambda === undefined || !namesInOwnContext(lambda, reserved)) {
1725617372
return original(self, compressor);
1725717373
}
@@ -29313,6 +29429,7 @@ const createTightenBody = (modules) => {
2931329429
* @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
2931429430
* @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
2931529431
* @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
29432+
* @property {((lambda: Node, compressor: CompressorShape) => boolean) | null} keepsSloppyLambda whether a sloppy function read once stays where strict code would run it
2931629433
*/
2931729434

2931829435
/**
@@ -29395,7 +29512,8 @@ const createInline = (modules) => {
2939529512
const inlineHooks = {
2939629513
inlinesIntoPureArgument: null,
2939729514
inlinesIntoCallee: null,
29398-
aliasesParameter: null
29515+
aliasesParameter: null,
29516+
keepsSloppyLambda: null
2939929517
};
2940029518
modules.webpackInline = inlineHooks;
2940129519

@@ -29550,6 +29668,15 @@ const createInline = (modules) => {
2955029668
}
2955129669
}
2955229670

29671+
if (
29672+
singleUse &&
29673+
isLambdaNode(fixed) &&
29674+
inlineHooks.keepsSloppyLambda !== null &&
29675+
inlineHooks.keepsSloppyLambda(fixed, compressor)
29676+
) {
29677+
singleUse = false;
29678+
}
29679+
2955329680
if (singleUse && (isLambdaNode(fixed) || isClassNode(fixed))) {
2955429681
nearestScope = /** @type {Scope} */ (compressor.find_scope());
2955529682
singleUse =
@@ -35999,7 +36126,11 @@ const installOptimize = (modules) => {
3599936126
break;
3600036127
}
3600136128
if (A.isCatchNode(node) && node.param) {
36002-
scope = node.param.definition.scope;
36129+
// A pattern declares in the clause's own scope; terser read its
36130+
// `definition`, which only a lone name has.
36131+
scope = node.param.definition
36132+
? node.param.definition.scope
36133+
: node.block_scope;
3600336134
break;
3600436135
}
3600536136
}
@@ -36780,16 +36911,25 @@ const installEstreePrint = (modules) => {
3678036911
/**
3678136912
* terser's `first_in_statement`, over the stream's parent stack.
3678236913
* @param {OutputStream} output the stream printed into
36914+
* @param {boolean=} exportDefault whether an `export default` value it starts, but is not, counts
3678336915
* @returns {boolean | undefined} whether the node printed starts its statement
3678436916
*/
36785-
const firstInStatement = (output) => {
36917+
const firstInStatement = (output, exportDefault) => {
3678636918
let node = /** @type {PrintParent} */ (output.parent(-1));
3678736919
/** @type {PrintParent | undefined} */
3678836920
let parent;
3678936921
for (let i = 0; (parent = output.parent(i)); i++) {
3679036922
if (parent.type === "ExpressionStatement" && parent.expression === node) {
3679136923
return true;
3679236924
}
36925+
if (
36926+
exportDefault &&
36927+
i > 0 &&
36928+
parent.type === "ExportDefaultDeclaration" &&
36929+
/** @type {PrintExport} */ (parent).exported_value === node
36930+
) {
36931+
return true;
36932+
}
3679336933
if (
3679436934
(parent.type === "SequenceExpression" &&
3679536935
parent.expressions[0] === node) ||
@@ -37918,6 +38058,21 @@ const installEstreePrint = (modules) => {
3791838058
output.space();
3791938059
output.print("default");
3792038060
output.space();
38061+
const { corrections } = modules;
38062+
// terser writes a named function or class expression bare, which reads
38063+
// back as a declaration binding its name in the module.
38064+
if (
38065+
node.exported_value &&
38066+
(declaration.type === "FunctionExpression" ||
38067+
declaration.type === "ClassExpression") &&
38068+
declaration.id &&
38069+
corrections !== undefined &&
38070+
corrections.enabled
38071+
) {
38072+
output.with_parens(() => printEstree(declaration, output));
38073+
output.semicolon();
38074+
return;
38075+
}
3792138076
printEstree(declaration, output);
3792238077
if (
3792338078
!kindOf.Class(declaration) &&
@@ -38393,10 +38548,17 @@ const installEstreePrint = (modules) => {
3839338548
);
3839438549
};
3839538550

38551+
// terser prints `export default (class {}).name` bare, which does not parse:
38552+
// `export default` reads a leading `class` or `function` as a declaration.
38553+
const correctsExport = () =>
38554+
modules.corrections !== undefined && modules.corrections.enabled;
38555+
3839638556
/** @type {EstreeParensTests} */
3839738557
const needsParens = {
3839838558
FunctionExpression: (node, output) => {
38399-
if (!output.has_parens() && firstInStatement(output)) return true;
38559+
if (!output.has_parens() && firstInStatement(output, correctsExport())) {
38560+
return true;
38561+
}
3840038562
const parent = parentOf(output, 0);
3840138563
if (
3840238564
output.option("webkit") &&
@@ -38435,7 +38597,8 @@ const installEstreePrint = (modules) => {
3843538597
// Otherwise `{...}` would read as a block.
3843638598
ObjectExpression: (node, output) =>
3843738599
!output.has_parens() && firstInStatement(output),
38438-
ClassExpression: (node, output) => firstInStatement(output),
38600+
ClassExpression: (node, output) =>
38601+
firstInStatement(output, correctsExport()),
3843938602
UnaryExpression: unaryNeedsParens,
3844038603
UpdateExpression: unaryNeedsParens,
3844138604
AwaitExpression: (node, output) => {

‎test/helpers/oxcSmaller.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
/** @type {[string, string[]][]} */
1010
const OXC_SMALLER_BY_REASON = [
11+
[
12+
"webpack keeps 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, which would run otherwise as strict",
13+
[
14+
"swc minifier: fixture/issues/2257/full/input.js",
15+
"swc minifier: fixture/issues/7568/1/input.js"
16+
]
17+
],
1118
[
1219
"oxc applies esbuild's shortenings, as oxc's `peephole/esbuild.rs` tests show",
1320
[

‎test/helpers/swcSmaller.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
/** @type {[string, string[]][]} */
1010
const SWC_SMALLER_BY_REASON = [
11+
[
12+
"webpack keeps 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, which would run otherwise as strict",
13+
[
14+
"swc minifier: fixture/issues/emotion/react/1/input.js",
15+
"swc minifier: fixture/next/syncfusion/933-e9f9a6bf671b96fc/input.js"
16+
]
17+
],
1118
[
1219
"swc drops what nothing reads or what has no effect: unused variables and destructuring, side-effect-free `new Map()` and pure calls, an empty constructor",
1320
[
@@ -211,7 +218,6 @@ const SWC_SMALLER_BY_REASON = [
211218
"swc minifier: fixture/issues/7568/1/input.js",
212219
"swc minifier: fixture/issues/7739/1/input.js",
213220
"swc minifier: fixture/issues/7847/input.js",
214-
"swc minifier: fixture/issues/8173/input.js",
215221
"swc minifier: fixture/issues/9610-keep-fargs/input.js",
216222
"swc minifier: fixture/issues/react-countup/2/input.js",
217223
"swc minifier: fixture/issues/react/hooks/1/input.js",
@@ -545,7 +551,6 @@ const SWC_SMALLER_BY_REASON = [
545551
"swc minifier: fixture/projects/wmr/archive-1/chunks/index.5a544c41/input.js",
546552
"swc minifier: fixture/pure-annotations-not-member-pattern/input.js",
547553
"swc minifier: full/helpers/1/input.js",
548-
"terser compress: classes/class_duplication",
549554
"terser compress: export/issue_2126",
550555
"terser compress: export/redirection"
551556
]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,8 @@ const IMPROVED_YET_BIGGER = {
13021302
"5 bytes fewer, 1 more gzipped: `{x();return}` breaks the `return x()` run the function before it repeats",
13031303
"fixture/issues/8173/input.js (the default minimizer's options)":
13041304
"7 bytes fewer, 2 more gzipped: `{x(n);return}` breaks the `return void` runs of the helpers around it",
1305+
"fixture/issues/8173/input.js (its own options)":
1306+
"7 bytes fewer, 2 more gzipped: `{x(n);return}` breaks the `return void` runs of the helpers around it",
13051307
"fixture/issues/vercel/006/input.js (its own options)":
13061308
"12 bytes fewer, 1 more gzipped: `{x();return}` breaks the `return void` runs of the branches around it",
13071309
"fixture/issues/d3-color/1/input.js (the default minimizer's options)":

0 commit comments

Comments
 (0)