Skip to content

Commit 55626e5

Browse files
perf(javascript): print a simple function's call as its value in the one pass
terser's `inline` of simple functions: a function written in place, or moved to its one read, whose body only returns an expression and which is passed nothing, or for each parameter a name never written, prints as that expression, each parameter read as the name passed for it. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 7e07690 commit 55626e5

3 files changed

Lines changed: 281 additions & 4 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+
Print a function called once with no or unchanged names passed as its returned value in the one-pass JavaScript minifier.

‎lib/javascript/syntax-printer.js‎

Lines changed: 259 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60599,6 +60599,10 @@ const createNodePrinter = (modules) => {
6059960599
const nameOf = (node) => {
6060060600
if (tree.type(node) === NodeType.Literal) return "Infinity";
6060160601
const definition = mangledNames === null ? null : mangledNames[node];
60602+
if (substituted.size !== 0 && definition) {
60603+
const passed = substituted.get(definition);
60604+
if (passed !== undefined) return nameOf(passed);
60605+
}
6060260606
return definition
6060360607
? definition.mangled_name || definition.name
6060460608
: tree.name(node);
@@ -60898,7 +60902,8 @@ const createNodePrinter = (modules) => {
6089860902
}
6089960903
case NodeType.ReturnStatement:
6090060904
return (
60901-
tree.type(grandparent) === NodeType.ArrowFunctionExpression &&
60905+
(tree.type(grandparent) === NodeType.ArrowFunctionExpression ||
60906+
(reducing.size !== 0 && reducing.has(grandparent))) &&
6090260907
tree.fieldCount(Field.body, parent) === 1
6090360908
);
6090460909
case NodeType.FunctionExpression: {
@@ -62682,6 +62687,20 @@ const createNodePrinter = (modules) => {
6268262687
// By assignment, 1 where nothing reads the name it sets: its value prints.
6268362688
/** @type {Uint8Array} */
6268462689
let dropsWrite = new Uint8Array(0);
62690+
// The functions called once with nothing passed that print as the value
62691+
// they return, and those calls, terser's `inline` of simple functions.
62692+
/** @type {Map<number, number>} */
62693+
const reducing = new Map();
62694+
/** @type {Map<number, number>} */
62695+
const reducedCalls = new Map();
62696+
// By parameter of such a function, the name passed for it, read instead.
62697+
/** @type {Map<TreeDefinition, number>} */
62698+
const substituted = new Map();
62699+
// By read of a function moved there, the call with nothing passed it is.
62700+
/** @type {Map<number, number>} */
62701+
const callReads = new Map();
62702+
/** @type {Set<number>} */
62703+
const moveReads = new Set();
6268562704
// By declarator, 1 for an alias whose reads read the name it is set to,
6268662705
// 2 for a function its one read prints.
6268762706
/** @type {Uint8Array} */
@@ -63222,6 +63241,138 @@ const createNodePrinter = (modules) => {
6322263241
);
6322363242
};
6322463243

63244+
/**
63245+
* @param {number} fn a function
63246+
* @param {number} call a call of it
63247+
* @returns {number} the one expression it returns, which the call may print as, 0 where none: it waits, yields, holds comments, reads its own `this`, `arguments` or name, or is passed other than a name never written for each parameter, which a function or class it holds could not read
63248+
*/
63249+
const reducedValue = (fn, call) => {
63250+
const count = tree.fieldCount(Field.params, fn);
63251+
if (
63252+
count !== tree.fieldCount(Field.arguments, call) ||
63253+
tree.flag(Flag.async, fn) ||
63254+
tree.flag(Flag.generator, fn) ||
63255+
holdsComments(fn)
63256+
) {
63257+
return 0;
63258+
}
63259+
for (let i = 0; i < count; i++) {
63260+
const param = tree.field(i, Field.params, fn);
63261+
const argument = itemOf(i, Field.arguments, call);
63262+
if (
63263+
typeOf(param) !== NodeType.Identifier ||
63264+
typeOf(argument) !== NodeType.Identifier ||
63265+
!isFixedName(param) ||
63266+
!isFixedName(argument)
63267+
) {
63268+
return 0;
63269+
}
63270+
}
63271+
let value = childOf(Field.body, fn);
63272+
if (typeOf(value) === NodeType.BlockStatement) {
63273+
if (tree.fieldCount(Field.body, value) !== 1) return 0;
63274+
const statement = tree.field(0, Field.body, value);
63275+
if (typeOf(statement) !== NodeType.ReturnStatement) return 0;
63276+
value = childOf(Field.argument, statement);
63277+
if (value === 0) return 0;
63278+
}
63279+
const id = tree.field(0, Field.id, fn);
63280+
const name = id === 0 ? "" : tree.name(id);
63281+
return findNode(value, (node) => {
63282+
switch (tree.type(node)) {
63283+
case NodeType.ThisExpression:
63284+
case NodeType.Super:
63285+
case NodeType.MetaProperty:
63286+
return true;
63287+
case NodeType.Identifier:
63288+
return tree.name(node) === "arguments" || tree.name(node) === name;
63289+
case NodeType.FunctionExpression:
63290+
case NodeType.FunctionDeclaration:
63291+
case NodeType.ArrowFunctionExpression:
63292+
case NodeType.ClassExpression:
63293+
case NodeType.ClassDeclaration:
63294+
// What it reads there is its own, but for a name.
63295+
return Boolean(
63296+
count !== 0 ||
63297+
(name !== "" &&
63298+
findNode(node, (inner) =>
63299+
tree.type(inner) === NodeType.Identifier &&
63300+
tree.name(inner) === name
63301+
? true
63302+
: undefined
63303+
))
63304+
);
63305+
default:
63306+
return undefined;
63307+
}
63308+
})
63309+
? 0
63310+
: value;
63311+
};
63312+
63313+
/**
63314+
* @param {number} name a name
63315+
* @returns {boolean} whether it is a variable of this source nothing ever writes but its declaration
63316+
*/
63317+
const isFixedName = (name) => {
63318+
const definition = /** @type {TreeDefinition[]} */ (mangledNames)[name];
63319+
return (
63320+
isMinifierVariable(definition) &&
63321+
!definition.undeclared &&
63322+
!definition.written
63323+
);
63324+
};
63325+
63326+
/**
63327+
* Notes a function a call prints as the value of, each parameter read
63328+
* printing as the name passed for it.
63329+
* @param {number} fn the function
63330+
* @param {number} call the call
63331+
* @param {number} value what it returns
63332+
* @returns {void}
63333+
*/
63334+
const noteReduced = (fn, call, value) => {
63335+
reducing.set(fn, value);
63336+
reducedCalls.set(call, fn);
63337+
const count = tree.fieldCount(Field.params, fn);
63338+
for (let i = 0; i < count; i++) {
63339+
substituted.set(
63340+
/** @type {TreeDefinition[]} */ (mangledNames)[
63341+
tree.field(i, Field.params, fn)
63342+
],
63343+
itemOf(i, Field.arguments, call)
63344+
);
63345+
}
63346+
};
63347+
63348+
/**
63349+
* @param {number} call a node
63350+
* @returns {boolean} whether it is a call that may print as what it calls returns: no `?.`, no comment on it
63351+
*/
63352+
const passesNothing = (call) =>
63353+
typeOf(call) === NodeType.CallExpression &&
63354+
!tree.flag(Flag.optional, call) &&
63355+
!hasEdgeComments(call);
63356+
63357+
/**
63358+
* Notes a call of a function written in place that prints as its value.
63359+
* @param {number} call a call
63360+
* @returns {void}
63361+
*/
63362+
const noteReducedCall = (call) => {
63363+
const callee = childOf(Field.callee, call);
63364+
const type = typeOf(callee);
63365+
if (
63366+
(type !== NodeType.FunctionExpression &&
63367+
type !== NodeType.ArrowFunctionExpression) ||
63368+
!passesNothing(call)
63369+
) {
63370+
return;
63371+
}
63372+
const value = reducedValue(callee, call);
63373+
if (value !== 0) noteReduced(callee, call, value);
63374+
};
63375+
6322563376
/**
6322663377
* Notes a statement only setting a name with `=`, terser's `unused` of an
6322763378
* assignment: the name it sets counts as unread there, so a name only set
@@ -63321,6 +63472,15 @@ const createNodePrinter = (modules) => {
6332163472
} else if (type === NodeType.Identifier) {
6332263473
noteRead(node, parent);
6332363474
if (probingTry !== 0) probedReads.add(node);
63475+
if (
63476+
moveReads.has(node) &&
63477+
passesNothing(parent) &&
63478+
childOf(Field.callee, parent) === node
63479+
) {
63480+
callReads.set(node, parent);
63481+
}
63482+
} else if (type === NodeType.CallExpression) {
63483+
noteReducedCall(node);
6332463484
} else if (type === NodeType.WithStatement) {
6332563485
answersWith = true;
6332663486
}
@@ -63993,6 +64153,14 @@ const createNodePrinter = (modules) => {
6399364153
readingFunction = program;
6399464154
functionStack.length = 0;
6399564155
functionStack.push(program);
64156+
reducing.clear();
64157+
reducedCalls.clear();
64158+
substituted.clear();
64159+
callReads.clear();
64160+
moveReads.clear();
64161+
if (treeMoves !== null) {
64162+
for (let i = 1; i < treeMoves.length; i += 2) moveReads.add(treeMoves[i]);
64163+
}
6399664164
/** @type {number[]} */
6399764165
const candidates = [];
6399864166
collectDroppable(program, 0, candidates);
@@ -64054,12 +64222,19 @@ const createNodePrinter = (modules) => {
6405464222
}
6405564223
for (let i = 0; i < moves.length; i += 2) {
6405664224
if (dropped[moves[i]] !== 1) continue;
64057-
if (typeOf(moves[i]) === NodeType.FunctionDeclaration) {
64225+
const fn =
64226+
typeOf(moves[i]) === NodeType.FunctionDeclaration
64227+
? moves[i]
64228+
: childOf(Field.init, moves[i]);
64229+
if (fn === moves[i]) {
6405864230
defunReads.set(moves[i + 1], moves[i]);
6405964231
movedDefuns.add(moves[i]);
6406064232
} else {
64061-
movedReads.set(childOf(Field.init, moves[i]), moves[i + 1]);
64233+
movedReads.set(fn, moves[i + 1]);
6406264234
}
64235+
const call = callReads.get(moves[i + 1]);
64236+
const value = call === undefined ? 0 : reducedValue(fn, call);
64237+
if (value !== 0) noteReduced(fn, /** @type {number} */ (call), value);
6406364238
}
6406464239
};
6406564240

@@ -65018,12 +65193,85 @@ const createNodePrinter = (modules) => {
6501865193
printNode(childOf(Field.right, node));
6501965194
};
6502065195

65196+
/**
65197+
* Prints a function called with nothing passed as the value it returns.
65198+
* @param {number} node the function
65199+
* @returns {void}
65200+
*/
65201+
const printReduced = (node) => {
65202+
const id = tree.field(0, Field.id, node);
65203+
if (id !== 0) skipNode(id);
65204+
const count = tree.fieldCount(Field.params, node);
65205+
for (let i = 0; i < count; i++) {
65206+
skipNode(tree.field(i, Field.params, node));
65207+
}
65208+
printNode(/** @type {number} */ (reducing.get(node)));
65209+
};
65210+
65211+
/**
65212+
* @param {number} call a call printing as the value its function returns
65213+
* @param {number} value that value
65214+
* @returns {boolean} whether the value needs parentheses where the call is
65215+
*/
65216+
const reducedParens = (call, value) => {
65217+
const type = typeOf(value);
65218+
if (
65219+
type === NodeType.Identifier ||
65220+
type === NodeType.ThisExpression ||
65221+
type === NodeType.MemberExpression ||
65222+
type === NodeType.CallExpression ||
65223+
type === NodeType.ArrayExpression ||
65224+
type === NodeType.TemplateLiteral ||
65225+
(type === NodeType.Literal && typeof tree.value(value) === "string")
65226+
) {
65227+
return false;
65228+
}
65229+
if (type === NodeType.SequenceExpression) return true;
65230+
const parent = parentAt(0);
65231+
switch (typeOf(parent)) {
65232+
case NodeType.VariableDeclarator:
65233+
case NodeType.ReturnStatement:
65234+
case NodeType.ThrowStatement:
65235+
case NodeType.ArrayExpression:
65236+
case NodeType.SpreadElement:
65237+
case NodeType.TemplateLiteral:
65238+
return false;
65239+
case NodeType.ExpressionStatement:
65240+
return opensAmbiguously(value);
65241+
case NodeType.CallExpression:
65242+
case NodeType.NewExpression:
65243+
return childOf(Field.callee, parent) === call;
65244+
case NodeType.AssignmentExpression:
65245+
return childOf(Field.right, parent) !== call;
65246+
case NodeType.Property:
65247+
return childOf(Field.value, parent) !== call;
65248+
default:
65249+
return true;
65250+
}
65251+
};
65252+
6502165253
/**
6502265254
* terser's `Call` printer.
6502365255
* @param {number} node a call or `new`
6502465256
* @returns {void}
6502565257
*/
6502665258
const printCall = (node) => {
65259+
const fn = reducedCalls.size === 0 ? undefined : reducedCalls.get(node);
65260+
if (fn !== undefined) {
65261+
// The function called prints as the value it returns.
65262+
const parens = reducedParens(
65263+
node,
65264+
/** @type {number} */ (reducing.get(fn))
65265+
);
65266+
if (parens) output.print("(");
65267+
printNode(childOf(Field.callee, node));
65268+
if (parens) output.print(")");
65269+
const count = tree.fieldCount(Field.arguments, node);
65270+
for (let i = 0; i < count; i++) {
65271+
skipNode(itemOf(i, Field.arguments, node));
65272+
}
65273+
return;
65274+
}
6502765275
printNode(childOf(Field.callee, node));
6502865276
if (
6502965277
typeOf(node) === NodeType.NewExpression &&
@@ -66436,9 +66684,17 @@ const createNodePrinter = (modules) => {
6643666684
generators[NodeType.FunctionExpression] = generators[
6643766685
NodeType.FunctionDeclaration
6643866686
] = (node) => {
66687+
if (reducing.size !== 0 && reducing.has(node)) {
66688+
printReduced(node);
66689+
return;
66690+
}
6643966691
printLambda(node, false);
6644066692
};
6644166693
generators[NodeType.ArrowFunctionExpression] = (node) => {
66694+
if (reducing.size !== 0 && reducing.has(node)) {
66695+
printReduced(node);
66696+
return;
66697+
}
6644266698
if (!isPrivateInRight(node, 0)) {
6644366699
printArrow(node);
6644466700
return;

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14757,7 +14757,7 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1475714757
[
1475814758
'function u(){ g(); var z = "z1", w = "w2", v = "v3"; function k(){ return w } h(function(){ return z }, function(){ function inner(){ return v } return inner }); return k } function f(o){ function a(e){ return e + o } const b = function(){ return this }, c = () => this; return [function(x){ return a(x) }, function(){ return b() }, function(){ return c() }] }',
1475914759
{},
14760-
'function u(){g();h(function(){return"z1"},function(){return function(){return"v3"}});return function(){return"w2"}}function f(n){let r=()=>this;return[function(r){return function(r){return r+n}(r)},function(){return function(){return this}()},function(){return r()}]}'
14760+
'function u(){g();h(function(){return"z1"},function(){return function(){return"v3"}});return function(){return"w2"}}function f(n){let t=()=>this;return[function(t){return t+n},function(){return function(){return this}()},function(){return t()}]}'
1476114761
],
1476214762
// What a statement sets folds into the next where that reads it first,
1476314763
// but not past a compound assignment's read nor a name it may write.
@@ -15230,6 +15230,22 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1523015230
{},
1523115231
"function f(c){switch(c){case-1:c=1;break;case 0:c=2}switch(c){case 1:x()}switch(c){default:}switch(c){case 1:y()}c:switch(c){case 1:z();break c}switch(c){case 1:y();/*! c */break}return import(r(c))}"
1523215232
],
15233+
// A function called with nothing but names never written passed prints as what it returns.
15234+
[
15235+
"function f(g) { var h = function () { return g.a }; function k() { return g.b } return [h(), k(), (() => ({ a: 1 }))(), (function () { return g.c + 1 })() * 2] }",
15236+
{},
15237+
"function f(n){return[n.a,n.b,{a:1},(n.c+1)*2]}"
15238+
],
15239+
[
15240+
"function f(s, e) { (() => ({ a: 1 }))(); return [((x, y) => x + y)(s, e), ((x) => (x, 1))(s)] }",
15241+
{},
15242+
"function f(e,n){return[e+n,(e,1)]}"
15243+
],
15244+
[
15245+
"function f(a, b) { a = 2; return [((x) => x + 1)(a), ((x) => () => x)(b), (function () { return this })(), (function n() { return n })(), (async () => 1)(), ((x) => x)(b, b), (() => 1)?.()] }",
15246+
{},
15247+
"function f(n,t){n=2;return[(n=>n+1)(n),(n=>()=>n)(t),function(){return this}(),function n(){return n}(),(async()=>1)(),(n=>n)(t,t),(()=>1)?.()]}"
15248+
],
1523315249
[
1523415250
"function f(a){ if (0) x(); Math.PI }",
1523515251
{ compress: {toplevel: false,builtins_ecma: 2020},format: {comments: false} },

0 commit comments

Comments
 (0)