Skip to content

Commit eaf22c6

Browse files
alexander-akaitclaude
authored andcommitted
feat(minifier): drop catch-shadowed functions, pure extra constructor arguments and nullish let calls
- A catch parameter's read keeps the binding of its name around the clause only where the clause declares that name again; terser always keeps it. - `typeof f` compared against a string reads `"function"` where `f` is a function its scope declares and nothing writes. - `let d = void 0` reads as nullish once its initializer is dropped, so `d?.(…)` folds as for `let d;`. - Every argument without effects past a constructor's parameters goes, not only the trailing ones. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 1fe55b4 commit eaf22c6

3 files changed

Lines changed: 120 additions & 20 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Drop more unused functions, arguments and optional calls from minified JavaScript.

‎lib/javascript/syntax-printer.js‎

Lines changed: 91 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5516,6 +5516,35 @@ const installUnused = (modules) => {
55165516
definition.kind === DECLARES_CATCH ||
55175517
(definition.kind === 0 && isSymbolCatchNode(definition.identifiers[0]));
55185518

5519+
/**
5520+
* The binding around a catch clause that a read of its parameter keeps, as
5521+
* terser keeps it; improved, only where the clause declares that name again.
5522+
* @param {Definition} definition the catch parameter
5523+
* @returns {Definition | false | undefined} the binding kept, if any
5524+
*/
5525+
const redefinedByCatch = (definition) => {
5526+
const redefined =
5527+
definition.scope.isBlockScope() &&
5528+
definition.scope.getDefunScope().getBinding(definition.name);
5529+
if (
5530+
!redefined ||
5531+
modules.improvements === undefined ||
5532+
!modules.improvements.enabled
5533+
) {
5534+
return redefined;
5535+
}
5536+
const { identifiers } = definition;
5537+
return walk(definition.scope.node, (/** @type {Node} */ node) =>
5538+
isSymbolDeclarationNode(node) &&
5539+
node.name === definition.name &&
5540+
!identifiers.includes(node)
5541+
? ast.walk_abort
5542+
: undefined
5543+
)
5544+
? redefined
5545+
: undefined;
5546+
};
5547+
55195548
// A recording: one walk of a function nested in the scope being dropped
55205549
// from, writing its scan as events into these scratch arrays, copied to
55215550
// their size at the end for the logs of the functions walked to share.
@@ -5912,9 +5941,7 @@ const installUnused = (modules) => {
59125941
if (!isInUse(definition)) {
59135942
markInUse(definition);
59145943
if (isCatchDefinition(definition)) {
5915-
const redefined =
5916-
definition.scope.isBlockScope() &&
5917-
definition.scope.getDefunScope().getBinding(definition.name);
5944+
const redefined = redefinedByCatch(definition);
59185945
if (redefined) markInUse(redefined);
59195946
}
59205947
}
@@ -6638,11 +6665,7 @@ const installUnused = (modules) => {
66386665
if (!isInUse(nodeDefinition)) {
66396666
markInUse(nodeDefinition);
66406667
if (isCatchDefinition(nodeDefinition)) {
6641-
const redefined =
6642-
nodeDefinition.scope.isBlockScope() &&
6643-
nodeDefinition.scope
6644-
.getDefunScope()
6645-
.getBinding(nodeDefinition.name);
6668+
const redefined = redefinedByCatch(nodeDefinition);
66466669
if (redefined) markInUse(redefined);
66476670
}
66486671
}
@@ -22523,6 +22546,43 @@ const installImprove = (modules) => {
2252322546
return undeletable ? makeNode(A.FalseNode, optimized) : optimized;
2252422547
});
2252522548

22549+
/**
22550+
* Whether a node reads a function declared in its scope's own body, which
22551+
* nothing writes: hoisted, it holds the function wherever it is read.
22552+
* @param {Node} node a node
22553+
* @param {Compressor} compressor the compressor
22554+
* @returns {boolean} true when it does
22555+
*/
22556+
const readsDeclaredFunction = (node, compressor) => {
22557+
const definition = A.isSymbolRefNode(node) ? node.definition : undefined;
22558+
if (
22559+
!definition ||
22560+
definition.undeclared ||
22561+
definition.identifiers.length !== 1 ||
22562+
!A.isSymbolDefunNode(definition.identifiers[0]) ||
22563+
definition.assignments !== 0 ||
22564+
definition.patternWrites !== 0 ||
22565+
definition.scope.isPinned() ||
22566+
compressor.exposed(definition)
22567+
) {
22568+
return false;
22569+
}
22570+
// One in a block is the sloppy-mode `var` only once the block runs.
22571+
const scope = definition.scope.node;
22572+
const body = A.bodyOf(scope);
22573+
return (
22574+
(A.isLambdaNode(scope) || A.isToplevelNode(scope)) &&
22575+
Array.isArray(body) &&
22576+
body.some(
22577+
(/** @type {Node} */ statement) =>
22578+
A.isDefunNode(statement) &&
22579+
statement.id &&
22580+
statement.id.definition === definition
22581+
) &&
22582+
!writesVariable(definition.scope.node, definition)
22583+
);
22584+
};
22585+
2252622586
// `typeof (a === b)` as `"boolean"`: the type of what nothing else observes.
2252722587
A.wrapOptimizer("UnaryPrefix", (optimizeUnary) => (self, compressor) => {
2252822588
const optimized = optimizeUnary(self, compressor);
@@ -22536,15 +22596,16 @@ const installImprove = (modules) => {
2253622596
return optimized;
2253722597
}
2253822598
const { argument } = optimized;
22539-
const type = A.isClassNode(argument)
22540-
? "function"
22541-
: A.isBoolean(argument)
22542-
? "boolean"
22543-
: A.isNumber(argument, compressor)
22544-
? "number"
22545-
: A.isString(argument, compressor)
22546-
? "string"
22547-
: undefined;
22599+
const type =
22600+
A.isClassNode(argument) || readsDeclaredFunction(argument, compressor)
22601+
? "function"
22602+
: A.isBoolean(argument)
22603+
? "boolean"
22604+
: A.isNumber(argument, compressor)
22605+
? "number"
22606+
: A.isString(argument, compressor)
22607+
? "string"
22608+
: undefined;
2254822609
return type === undefined
2254922610
? optimized
2255022611
: makeNode(A.StringNode, optimized, { value: type });
@@ -24843,9 +24904,12 @@ const installImprove = (modules) => {
2484324904
args.length > read &&
2484424905
!args.some((/** @type {Node} */ arg) => A.isExpansionNode(arg))
2484524906
) {
24846-
let end = args.length;
24847-
while (end > read && !A.hasSideEffects(args[end - 1], compressor)) end--;
24848-
args.length = end;
24907+
// An argument past them only runs, so one without effects goes.
24908+
let kept = read;
24909+
for (let i = read; i < args.length; i++) {
24910+
if (A.hasSideEffects(args[i], compressor)) args[kept++] = args[i];
24911+
}
24912+
args.length = kept;
2484924913
}
2485024914
if (!constructsWhenCalled(optimized, compressor)) return optimized;
2485124915
return A.optimize(
@@ -43468,6 +43532,13 @@ const createCompressHelpers = (modules) => {
4346843532
!compressor.exposed(node.definition)
4346943533
) {
4347043534
fixed = ast.fixedValue(node);
43535+
// A `let` whose `void 0` was dropped still reads as `undefined`.
43536+
if (
43537+
fixed === null &&
43538+
ast.isSymbolLetNode(node.definition.identifiers[0])
43539+
) {
43540+
return true;
43541+
}
4347143542
}
4347243543
return (
4347343544
isSyntaxNode(fixed) && isNullish(/** @type {Node} */ (fixed), compressor)

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,30 @@ const IMPROVED_CASES = [
281281
{ compress: {}, mangle: false },
282282
"function f(fn,o){return delete(o={...o,b:2}).a,fn(o)}console.log(f(JSON.stringify,{a:1}));"
283283
],
284+
[
285+
"arguments without effects among others past a constructor's parameters",
286+
"function e(v) { console.log(v); return v; } console.log(new function (v) { this.v = v; }(e(1), 1, e(2), 2).v);",
287+
{ compress: {}, mangle: false },
288+
"function e(v){return console.log(v),v}console.log(new function(v){this.v=v}(e(1),e(2)).v);"
289+
],
290+
[
291+
"an optional call of a `let` set to `void 0`, read again after",
292+
"function f(g) { let d = void 0; d?.(g()); return [d, 1]; } console.log(f(function () { throw 0; }));",
293+
{ compress: {}, mangle: false },
294+
"function f(g){let d;return[d,1]}console.log(f(function(){throw 0}));"
295+
],
296+
[
297+
"the type of a function its scope declares and nothing writes",
298+
'function g() { function x() {} return [typeof x === "function", "function" != typeof x, typeof x]; } console.log(g());',
299+
{ compress: {}, mangle: false },
300+
'function g(){return[!0,!1,"function"]}console.log(g());'
301+
],
302+
[
303+
"an unused function a catch clause's parameter shadows",
304+
"function f() { function a() {} try { throw 42; } catch (a) { return a; } } console.log(f());",
305+
{ compress: {}, mangle: false },
306+
"function f(){try{throw 42}catch(a){return a}}console.log(f());"
307+
],
284308
[
285309
"a value calling a method on a name, moved past a bare read of the name",
286310
"function c(ctl, e) { var p = ctl.t(e); return u(ctl, p); } function u(a, b) { return [typeof a, b]; } console.log(c({ t: function (x) { return x + 1; } }, 1));",

0 commit comments

Comments
 (0)