Skip to content

Commit 156b207

Browse files
fix(javascript): still read a hole of an evaluated array as undefined
Keeping holes for `indexOf` stopped `[1, , 3][1]` folding to `void 0` under `unsafe`, which terser's `evaluate/unsafe_array` case measures against swc. A read of a hole now evaluates to `undefined` again. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4
1 parent 360a47d commit 156b207

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23825,6 +23825,20 @@ const installEvaluate = (modules) => {
2382523825
return /** @type {EXPECTED_ANY} */ (object)[key];
2382623826
}
2382723827
}
23828+
// A hole the correct phase keeps reads the prototype, which `unsafe`
23829+
// takes as holding no index.
23830+
if (Array.isArray(object) && typeof key !== "symbol") {
23831+
const index = Number(key);
23832+
if (
23833+
String(index) === String(key) &&
23834+
Number.isInteger(index) &&
23835+
index >= 0 &&
23836+
index < object.length &&
23837+
!(index in object)
23838+
) {
23839+
return undefined;
23840+
}
23841+
}
2382823842
if (
2382923843
!object ||
2383023844
object === expression ||

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6353,6 +6353,15 @@ describe("syntax-printer", () => {
63536353
});
63546354
}
63556355

6356+
it("should still read a hole of an array literal as undefined under `unsafe`", async () => {
6357+
const { minify } = await load();
6358+
const { code } = await minify(
6359+
"console.log([1, , 3][1], [, 2][\"0\"], [1, , 3][5], [1, , 3].x);",
6360+
{ compress: { unsafe: true }, mangle: false }
6361+
);
6362+
expect(code).toBe("console.log(void 0,void 0,[1,,3][5],[1,,3].x);");
6363+
});
6364+
63566365
it("should keep a string's `charAt` of a BigInt, where terser throws", async () => {
63576366
const { minify, corrections } = await load();
63586367
const input =

0 commit comments

Comments
 (0)