fix(replay): preserve sparse array positions in result projection (#522)

Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
Rudy Mizrahi Celekli
2026-10-05 14:50:05 +08:00
committed by GitHub
parent 405732a7f5
commit 9d1c3631b8
2 changed files with 56 additions and 0 deletions
+11
View File
@@ -434,6 +434,17 @@ const projectComplexValue = (
);
}
}
if (Array.isArray(candidate) && Array.isArray(output)) {
for (let index = 0; index < candidate.length; index += 1) {
if (Object.hasOwn(descriptors, String(index))) continue;
output[index] = projectValueRecursive(
null,
depth + 1,
ancestors,
context,
);
}
}
return output;
};
@@ -225,3 +225,48 @@ describe("disposable JavaScript replay worker", () => {
});
});
});
describe("replay array projection", () => {
it.each([
["Array(3)", [null, null, null]],
[
"(() => { const value = Array(3); value[1] = 42; return value; })()",
[null, 42, null],
],
["[1, null, 3]", [1, null, 3]],
["[]", []],
])("preserves JSON array positions for %s", async (expression, expected) => {
const workerRequest = await request("parser.mjs", "esm", "default", []);
workerRequest.left.modules[0] = {
alias: "entry",
format: "esm",
dependencies: {},
source: `export default function () { return ${expression}; }`,
};
const result = await runWorker(workerRequest);
expect(result.code).toBe(0);
expect(JSON.parse(result.stdout)).toMatchObject({
left: [{ outcome: "return", value: expected }],
});
});
it("accounts for holes in the existing result node budget", async () => {
const workerRequest = await request("parser.mjs", "esm", "default", []);
workerRequest.left.modules[0] = {
alias: "entry",
format: "esm",
dependencies: {},
source: "export default function () { return Array(4); }",
};
workerRequest.limits.resultNodes = 3;
const result = await runWorker(workerRequest);
expect(JSON.parse(result.stdout)).toMatchObject({
left: [
{
outcome: "serialization_error",
exception: { message: "Replay result projection limit exceeded" },
},
],
});
});
});