Skip to content

Commit 5ba5dba

Browse files
fix(javascript): keep an arguments read before a write to its parameter in the minifier (#22599)
* fix(javascript): keep an `arguments` read before a write to its parameter in the minifier In a sloppy function with simple parameters `arguments[i]` aliases the parameter, but collapse_vars moved `t = arguments[0]` past `x = {}`, so the read saw the new value (terser#768, terser#1165). Claude-Session: https://claude.ai/code/session_01UgQAw35dRJd9GKMF9p5CcQ * test(javascript): cover more `arguments` aliasing in the minifier Several parameters, a second parameter, a computed index and a read in an arrow, which terser misprints; and reads it already writes right, which the correct phase leaves as they are. Claude-Session: https://claude.ai/code/session_01UgQAw35dRJd9GKMF9p5CcQ
1 parent 37538ee commit 5ba5dba

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17073,6 +17073,20 @@ const installCorrect = (modules) => {
1707317073
: undefined
1707417074
)
1707517075
);
17076+
// A sloppy function's simple parameters are its `arguments` object's
17077+
// indices, so a value reading that object reads them too.
17078+
/** @type {CollapseHooks} */
17079+
(modules.webpackCollapse).argumentsAliases = (node, compressor) => {
17080+
if (
17081+
!corrections.enabled ||
17082+
!A.isArgumentsObject(node) ||
17083+
compressor.has_directive("use strict")
17084+
) {
17085+
return [];
17086+
}
17087+
const { params } = node.definition.scope.node;
17088+
return params.every(A.isSymbolFunargNode) ? params : [];
17089+
};
1707617090
// A sloppy `arguments[i]` follows its parameter only where an argument was
1707717091
// passed, so it is the parameter only while nothing reassigns that.
1707817092
wrapOptimize("Sub", (original) => (self, compressor) => {
@@ -26391,6 +26405,7 @@ const installDropSideEffectFree = ({
2639126405
* @property {((node: Node, candidate: Node, compressor: CompressorShape) => boolean) | null} readsSettled whether a name read keeps its value however the code a candidate runs before it moves past
2639226406
* @property {(() => boolean) | null} foldsUnobserved whether a value only the reads it folds into can see may move past any global read
2639326407
* @property {((target: Node, defunScope: Scope, compressor: CompressorShape) => boolean) | null} assignsLet whether an assignment to a `let` may be folded into the first read of it
26408+
* @property {((node: Node, compressor: CompressorShape) => Node[]) | null} argumentsAliases the parameters a read of `arguments` also reads, which a value holding it may not move past a write of
2639426409
* @property {boolean} finalPass whether the pass running is the last one asked for
2639526410
*/
2639626411

@@ -26410,6 +26425,7 @@ const createTightenBody = (modules) => {
2641026425
storesWithoutReading: null,
2641126426
readsSettled: null,
2641226427
assignsLet: null,
26428+
argumentsAliases: null,
2641326429
foldsUnobserved: null,
2641426430
finalPass: true
2641526431
};
@@ -27999,6 +28015,19 @@ const createTightenBody = (modules) => {
2799928015
)
2800028016
});
2800128017
}
28018+
if (collapseHooks.argumentsAliases !== null) {
28019+
for (const parameter of collapseHooks.argumentsAliases(
28020+
symbol,
28021+
state.compressor
28022+
)) {
28023+
if (!values.has(parameter.name)) {
28024+
values.set(parameter.name, {
28025+
definition: parameter.definition,
28026+
modified: false
28027+
});
28028+
}
28029+
}
28030+
}
2800228031
}
2800328032
};
2800428033

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,20 @@ const KEPT_CASES = [
12541254
["a function passed a global of its parameter's name", "var name = \"g\"; !function () { function show(props, name) { props.seen = name; } function create(props, flag) { flag && show(props, name); return props; } console.log(create({}, 1).seen, create({}, 0).seen); }();"]
12551255
];
12561256

1257+
// An `arguments` read terser already writes right: no parameter it aliases is
1258+
// written past it, or the parameters alias nothing. The correct phase keeps it.
1259+
/** @type {[string, string][]} */
1260+
const ARGUMENTS_KEPT_CASES = [
1261+
["a parameter incremented after `arguments[0]` is read", "function f(x) { var t = arguments[0]; x++; return [t, x]; } console.log(f(1));"],
1262+
["a parameter added to after `arguments[0]` is read", "function f(x) { var t = arguments[0]; x += 10; return t + ':' + x; } console.log(f(1));"],
1263+
["a parameter destructured into after `arguments[0]` is read", "function f(x) { var t = arguments[0]; [x] = [9]; return t + ':' + x; } console.log(f(1));"],
1264+
["a strict function", "function f(x) { 'use strict'; var t = arguments[0]; x = {}; return t; } console.log(f(1));"],
1265+
["a parameter with a default", "function f(x = 0) { var t = arguments[0]; x = {}; return t; } console.log(f(1));"],
1266+
["a destructured parameter", "function f({ a }, x) { var t = arguments[1]; x = {}; return t; } console.log(f({}, 1));"],
1267+
["a class method", "class C { m(x) { var t = arguments[0]; x = {}; return t; } } console.log(new C().m(1));"],
1268+
["no parameter written", "function f(x) { var t = arguments[0]; var y = {}; return t + typeof y; } console.log(f(1));"]
1269+
];
1270+
12571271
// Each prints one thing and terser's output another, under the options named.
12581272
/** @type {[string, string, import("terser").MinifyOptions][]} */
12591273
const CORRECTED_CASES = [
@@ -1357,6 +1371,31 @@ const CORRECTED_CASES = [
13571371
"function m(v) { v = 2; return arguments[0]; } console.log(m());",
13581372
{ compress: { arguments: true }, mangle: false }
13591373
],
1374+
[
1375+
"`arguments[0]` read before its parameter is reassigned, moved past that",
1376+
"function test(x, y) { var temp; if (typeof x === 'string') { temp = arguments[0]; x = {}; x[temp] = y; return x; } } console.log(JSON.stringify(test('a', 'b')));",
1377+
{ compress: {}, mangle: false }
1378+
],
1379+
[
1380+
"`arguments` read for several parameters before one is reassigned",
1381+
"function set(o, e) { var l, s; if (typeof o === 'string') { l = arguments[0]; s = arguments[1]; o = {}; o[l] = s; } console.log(l, s, JSON.stringify(o)); } set('foo', 'val');",
1382+
{ compress: {}, mangle: false }
1383+
],
1384+
[
1385+
"`arguments[1]` read before the second parameter is reassigned",
1386+
"function f(a, b) { var t = arguments[1]; b = 0; return t; } console.log(f(1, 2));",
1387+
{ compress: {}, mangle: false }
1388+
],
1389+
[
1390+
"`arguments[i]` read before a parameter is reassigned",
1391+
"function f(x, i) { var t = arguments[i]; x = 5; return t; } console.log(f(1, 0));",
1392+
{ compress: {}, mangle: false }
1393+
],
1394+
[
1395+
"`arguments[0]` read in an arrow before the parameter is reassigned",
1396+
"function f(x) { var g = () => { var t = arguments[0]; x = 2; return t; }; return g(); } console.log(f(1));",
1397+
{ compress: {}, mangle: false }
1398+
],
13601399
[
13611400
"an empty arrow whose default has an effect",
13621401
"var r = []; ((v = r.push(1)) => {})(); console.log(r.length);",
@@ -6262,6 +6301,20 @@ describe("syntax-printer", () => {
62626301
});
62636302
}
62646303

6304+
for (const [name, input] of ARGUMENTS_KEPT_CASES) {
6305+
it(`should leave an \`arguments\` read where terser does: ${name}`, async () => {
6306+
const { minify } = await load();
6307+
const options = { compress: {}, mangle: false };
6308+
const { code } = await minify(input, options);
6309+
expect(code).toBe(
6310+
(await uncorrected(() => minify(input, options))).code
6311+
);
6312+
expect(runProgram(/** @type {string} */ (code))).toBe(
6313+
runProgram(input)
6314+
);
6315+
});
6316+
}
6317+
62656318
it("should parenthesize a destructuring assignment V8 refuses bare after another argument", async () => {
62666319
const { minify, corrections } = await load();
62676320
const input =

0 commit comments

Comments
 (0)