Skip to content

Commit 23be70e

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 0c167b5 commit 23be70e

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
@@ -24326,6 +24326,20 @@ const installEvaluate = (modules) => {
2432624326
return /** @type {EXPECTED_ANY} */ (object)[key];
2432724327
}
2432824328
}
24329+
// A hole the correct phase keeps reads the prototype, which `unsafe`
24330+
// takes as holding no index.
24331+
if (Array.isArray(object) && typeof key !== "symbol") {
24332+
const index = Number(key);
24333+
if (
24334+
String(index) === String(key) &&
24335+
Number.isInteger(index) &&
24336+
index >= 0 &&
24337+
index < object.length &&
24338+
!(index in object)
24339+
) {
24340+
return undefined;
24341+
}
24342+
}
2432924343
if (
2433024344
!object ||
2433124345
object === expression ||

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6550,6 +6550,15 @@ describe("syntax-printer", () => {
65506550
});
65516551
}
65526552

6553+
it("should still read a hole of an array literal as undefined under `unsafe`", async () => {
6554+
const { minify } = await load();
6555+
const { code } = await minify(
6556+
"console.log([1, , 3][1], [, 2][\"0\"], [1, , 3][5], [1, , 3].x);",
6557+
{ compress: { unsafe: true }, mangle: false }
6558+
);
6559+
expect(code).toBe("console.log(void 0,void 0,[1,,3][5],[1,,3].x);");
6560+
});
6561+
65536562
it("should keep a string's `charAt` of a BigInt, where terser throws", async () => {
65546563
const { minify, corrections } = await load();
65556564
const input =

0 commit comments

Comments
 (0)