Skip to content

Commit 573ed5e

Browse files
fix(javascript): keep !!!x and writes arguments reads in the one pass (#22958)
In the one pass, `!!!x` read for its truth printed nothing: the outer `!!` fold dropped the `!` its inner fold had already dropped. A write to a parameter of a function reading its own `arguments` went as unread while `arguments` reads it in sloppy code. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent c494697 commit 573ed5e

3 files changed

Lines changed: 62 additions & 1 deletion

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+
Fix `!!!x` and writes `arguments` reads in the one-pass JavaScript minifier.

‎lib/javascript/syntax-printer.js‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63219,6 +63219,33 @@ const createNodePrinter = (modules) => {
6321963219
);
6322063220
};
6322163221

63222+
// By function, whether it reads its own `arguments`, found once.
63223+
/** @type {Map<number, boolean>} */
63224+
const argumentsReaders = new Map();
63225+
63226+
/**
63227+
* @param {number} fn a function
63228+
* @returns {boolean} whether it reads its own `arguments`, as an arrow inside it would
63229+
*/
63230+
const readsArguments = (fn) => {
63231+
let found = argumentsReaders.get(fn);
63232+
if (found === undefined) {
63233+
found = findNode(tree.field(0, Field.body, fn), (node) => {
63234+
switch (tree.type(node)) {
63235+
case NodeType.Identifier:
63236+
return tree.name(node) === "arguments" ? true : undefined;
63237+
case NodeType.FunctionExpression:
63238+
case NodeType.FunctionDeclaration:
63239+
return false;
63240+
default:
63241+
return undefined;
63242+
}
63243+
});
63244+
argumentsReaders.set(fn, found);
63245+
}
63246+
return found;
63247+
};
63248+
6322263249
/**
6322363250
* Notes a statement only setting a name with `=`, terser's `unused` of an
6322463251
* assignment: the name it sets counts as unread there, so a name only set
@@ -63238,7 +63265,14 @@ const createNodePrinter = (modules) => {
6323863265
const name = childOf(Field.left, assignment);
6323963266
if (typeOf(name) !== NodeType.Identifier || hasEdgeComments(name)) return;
6324063267
const definition = /** @type {TreeDefinition[]} */ (mangledNames)[name];
63241-
if (!isMinifierVariable(definition)) return;
63268+
if (
63269+
!isMinifierVariable(definition) ||
63270+
// `arguments` reads what a parameter is set to, in sloppy code.
63271+
(definition.kind === DECLARES_FUNARG &&
63272+
readsArguments(functionOf(definition)))
63273+
) {
63274+
return;
63275+
}
6324263276
droppedReads.set(definition, (droppedReads.get(definition) || 0) + 1);
6324363277
writeStatements.push(statement, assignment);
6324463278
};
@@ -63973,6 +64007,7 @@ const createNodePrinter = (modules) => {
6397364007
for (const declarator of treeAliases) aliasesRead[declarator] = 1;
6397464008
}
6397564009
droppedReads.clear();
64010+
argumentsReaders.clear();
6397664011
inlined.clear();
6397764012
constantValues.clear();
6397864013
constantDeclarators.clear();
@@ -65032,6 +65067,14 @@ const createNodePrinter = (modules) => {
6503265067
printArguments(node, Field.arguments);
6503365068
};
6503465069

65070+
/**
65071+
* @param {number} node a node
65072+
* @returns {boolean} whether it is a `!`, whose own `!!` fold leaves it no `!` to drop
65073+
*/
65074+
const isNegation = (node) =>
65075+
typeOf(node) === NodeType.UnaryExpression &&
65076+
tree.textOf(Part.operator, node) === "!";
65077+
6503565078
/**
6503665079
* @param {number} node the node being printed
6503765080
* @returns {boolean} whether its parent reads only its truth: as a test, or what `!` negates
@@ -65171,6 +65214,7 @@ const createNodePrinter = (modules) => {
6517165214
operator === "!" &&
6517265215
typeOf(argument) === NodeType.UnaryExpression &&
6517365216
tree.textOf(Part.operator, argument) === "!" &&
65217+
!isNegation(childOf(Field.argument, argument)) &&
6517465218
!holdsComments(argument) &&
6517565219
readsTruth(node)
6517665220
) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15228,6 +15228,18 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1522815228
{},
1522915229
"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))}"
1523015230
],
15231+
// `!!!x` read for its truth folds its innermost `!!` alone.
15232+
[
15233+
"function f(x) { if (!!!x) g(); if (!!!!x) h(); return [!!!x ? 1 : 2, !!!x] }",
15234+
{},
15235+
"function f(x){if(!x)g();if(x)h();return[!x?1:2,!x]}"
15236+
],
15237+
// A parameter `arguments` reads keeps what sets it.
15238+
[
15239+
"function f(a) { a = 1; return arguments[0] } function k(b) { b = 1; return () => arguments } function m(c) { c = 1; return function () { return arguments } }",
15240+
{},
15241+
"function f(n){n=1;return arguments[0]}function k(n){n=1;return()=>arguments}function m(n){return function(){return arguments}}"
15242+
],
1523115243
[
1523215244
"function f(a){ if (0) x(); Math.PI }",
1523315245
{ compress: {toplevel: false,builtins_ecma: 2020},format: {comments: false} },

0 commit comments

Comments
 (0)