fix(sourcemaps)!: preserve nullable source declarations

Accept absent map-level sourcesContent and preserve null source-name slots alongside known originals and aligned content. Represent unnamed local declarations explicitly, retain leaf-local and flattened indices, and preserve declared indexed-section order across consumers.

Account for added indices before browser output allocation. Accept legacy retained rows without indices. Consolidate public CLI/MCP source-map fixtures and update the stale named-source-only assertion.

BREAKING CHANGE: Browser source-map original and mapping rows expose null for unavailable source names instead of inventing an unknown-source string. New index fields are optional when reading older retained evidence.

Refs #2023, #2024
This commit is contained in:
morluto
2026-10-12 11:10:49 +08:00
parent c0904b4c1a
commit ee50ed121e
13 changed files with 361 additions and 70 deletions
@@ -1,6 +1,7 @@
import { Readable } from "node:stream";
import { createHash } from "node:crypto";
import { symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Readable } from "node:stream";
import { expect, it } from "vitest";
@@ -135,6 +136,7 @@ it.each([
["a non-array sourcesContent value", { sourcesContent: "source" }],
["a non-string, non-null source entry", { sourcesContent: [42] }],
["a source-content array with the wrong length", { sourcesContent: [] }],
["a non-string source name", { sources: [42] }],
])(
"reports %s as an invalid local source map",
async (_description, content) => {
@@ -187,7 +189,7 @@ it("accepts omitted and explicitly null source-map source contents", async () =>
JSON.stringify({
version: 3,
sources: ["null.ts"],
sourcesContent: [null],
sourcesContent: null,
names: [],
mappings: "",
}),
@@ -209,6 +211,77 @@ it("accepts omitted and explicitly null source-map source contents", async () =>
expect(result.graph.coverage.status).toBe("complete");
});
it("retains unnamed source declarations without shifting later source evidence", async () => {
const root = await createTestTempDirectory("rea-javascript-unnamed-source-");
const text = JSON.stringify({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: {
version: 3,
sources: ["prior.ts"],
sourcesContent: ["export const prior = 1;"],
names: [],
mappings: "",
},
},
{
offset: { line: 1, column: 0 },
map: {
version: 3,
sources: [null, "original.ts"],
sourcesContent: [null, "export const answer: number = 42;"],
names: [],
mappings: "ACAA",
},
},
],
});
await writeFile(join(root, "bundle.js.map"), text);
const result = await reconstructJavaScriptArtifact({ input_path: root });
const nodes = result.graph.nodes;
const original = nodes.find(
({ kind, identity }) =>
kind === "source-module" &&
identity.strategy === "source-map-original" &&
identity.original_source === "original.ts",
);
const unnamed = nodes.find(
({ kind, observations }) =>
kind === "unknown" &&
observations.some(
({ properties }) => properties.source_name_available === false,
),
);
expect(original?.identity).toMatchObject({
strategy: "source-map-original",
source_sha256: createHash("sha256")
.update("export const answer: number = 42;")
.digest("hex"),
});
expect(original?.observations[0]?.properties).toMatchObject({
source_index: 1,
declaration_index: 2,
content_available: true,
});
expect(unnamed?.observations[0]?.properties).toMatchObject({
source_index: 0,
declaration_index: 1,
source_name_available: false,
content_available: false,
content_sha256: null,
});
expect(unnamed?.identity).toMatchObject({
strategy: "artifact-local-key",
namespace: "source-map-unnamed-declaration",
key: "1",
});
expect(result.graph.coverage.status).toBe("complete");
});
it("retains every repeated content observation and containment path", async () => {
const root = await createTestTempDirectory("rea-javascript-observations-");
await Promise.all(