Skip to content

Commit 5f55eb1

Browse files
fix(minify): keep a concat, Set or Map of an array literal with a hole
A hole reads its index through `Array.prototype`. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent b54dfa0 commit 5f55eb1

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21627,11 +21627,13 @@ const installImprove = (modules) => {
2162721627
args = [...inner.arguments, ...args];
2162821628
}
2162921629
const receiver = callee.object;
21630-
if (!A.isArrayNode(receiver)) return;
21630+
// A hole reads an index through `Array.prototype`, which `concat` copies.
21631+
if (!A.isArrayNode(receiver) || receiver.elements.includes(null)) return;
2163121632
const elements = [...receiver.elements];
2163221633
let taken = 0;
2163321634
for (const argument of args) {
2163421635
if (A.isArrayNode(argument)) {
21636+
if (argument.elements.includes(null)) break;
2163521637
elements.push(...argument.elements);
2163621638
} else {
2163721639
const value = constantOf(argument);
@@ -22586,14 +22588,15 @@ const installImprove = (modules) => {
2258622588
/** @type {Node[]} */
2258722589
const values = [];
2258822590
for (const element of /** @type {(Node | null)[]} */ (list.elements)) {
22591+
// Iterating a hole reads its index through `Array.prototype`.
22592+
if (!element || A.isExpansionNode(element)) return;
2258922593
if (self.callee.name === "Map") {
22590-
if (!element || !A.isArrayNode(element)) return;
22594+
if (!A.isArrayNode(element)) return;
2259122595
for (const value of /** @type {(Node | null)[]} */ (element.elements)) {
22592-
if (value && A.isExpansionNode(value)) return;
22593-
if (value) values.push(value);
22596+
if (!value || A.isExpansionNode(value)) return;
22597+
values.push(value);
2259422598
}
22595-
} else if (element) {
22596-
if (A.isExpansionNode(element)) return;
22599+
} else {
2259722600
values.push(element);
2259822601
}
2259922602
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,12 +2036,6 @@ const IMPROVED_CASES = [
20362036
{ compress: {}, mangle: false },
20372037
"function f(n,x,y){return[[n,2,3,4],[1].concat(x,y)]}console.log(f(1,[5],6));"
20382038
],
2039-
[
2040-
"a `concat` keeping a hole",
2041-
"console.log([, 1].concat(2).length, 0 in [, 1].concat(2));",
2042-
{ compress: {}, mangle: false },
2043-
"console.log(3,0 in[,1,2]);"
2044-
],
20452039
[
20462040
"an element of an array literal read by its index",
20472041
"function f(o) { return [[o][0].p, [1, o][1].p]; } console.log(f({ p: 2 }));",
@@ -2079,6 +2073,7 @@ const IMPROVED_CASES = [
20792073
/** @type {[string, string][]} */
20802074
const KEPT_CASES = [
20812075
["a computed key of a loop head's pattern, which the body writes", 'function f(o) { let k = 1; for ({ [k]: x } of o) { console.log(x); k = 2; } } var x; f([{ 1: "a", 2: "b" }, { 1: "c", 2: "d" }]);'],
2076+
["a `concat` of an array literal with a hole, which reads its index through `Array.prototype`", "console.log([, 1].concat(2).length, 0 in [, 1].concat(2), [1].concat([, 2]).length);"],
20822077
["a `Set` built from an array literal where a direct `eval` may rebind `Set`", 'function f() { eval("var Set = function () { console.log(1); }"); new Set([1]); } f();'],
20832078
["a `Set` built from an array literal where a `with` object may rebind `Set`", "with ({ Set: function () { console.log(1); } }) { new Set([1]); }"],
20842079
["an element of an array literal written through as a property of a destructuring assignment", "var a = 1, value = { x: 5 }; ({ x: [a][0] } = value); console.log(a);"],

0 commit comments

Comments
 (0)