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:
@@ -3,12 +3,10 @@ import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
|
||||
import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js";
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"recovers retained prefixed source-map originals and byte identity through %s",
|
||||
@@ -29,27 +27,9 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
);
|
||||
await writeFile(mapPath, mapText);
|
||||
|
||||
let document: unknown;
|
||||
if (surface === "CLI") {
|
||||
const response = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--format", "json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(response.exitCode, response.stderr).toBe(0);
|
||||
document = response.json;
|
||||
} else {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const response = await call("analyze_javascript_application", {
|
||||
input_path: root,
|
||||
format: "directory",
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
document = response.structuredContent;
|
||||
}
|
||||
const { graph } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(document).normalized_result;
|
||||
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
const mapDigest = createHash("sha256").update(mapText).digest("hex");
|
||||
const originalDigest = createHash("sha256").update(original).digest("hex");
|
||||
expect(graph.nodes).toContainEqual(
|
||||
expect.objectContaining({
|
||||
kind: "source-module",
|
||||
@@ -57,7 +37,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
strategy: "source-map-original",
|
||||
source_map_sha256: mapDigest,
|
||||
original_source: "original.ts",
|
||||
source_sha256: createHash("sha256").update(original).digest("hex"),
|
||||
source_sha256: originalDigest,
|
||||
}),
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
@@ -65,7 +45,13 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
source_name: "original.ts",
|
||||
map_path: "bundle.js.map",
|
||||
}),
|
||||
properties: { content_available: true },
|
||||
properties: {
|
||||
source_index: 0,
|
||||
declaration_index: 0,
|
||||
source_name_available: true,
|
||||
content_available: true,
|
||||
content_sha256: originalDigest,
|
||||
},
|
||||
evidence: expect.objectContaining({
|
||||
artifact: expect.objectContaining({ sha256: mapDigest }),
|
||||
}),
|
||||
@@ -77,3 +63,91 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
expect(await readFile(mapPath, "utf8")).toBe(mapText);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"retains nullable source-map declarations and aligned content through %s",
|
||||
async (surface, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-nullable-map-");
|
||||
const original = "export const answer: number = 42;";
|
||||
const unnamed = "retained source text without a declared name";
|
||||
const maps = {
|
||||
"absent.js.map": JSON.stringify({
|
||||
version: 3,
|
||||
sources: ["unembedded.ts"],
|
||||
sourcesContent: null,
|
||||
names: [],
|
||||
mappings: "AAAA",
|
||||
}),
|
||||
"mixed.js.map": JSON.stringify({
|
||||
version: 3,
|
||||
sources: [null, "original.ts"],
|
||||
sourcesContent: [unnamed, original],
|
||||
names: [],
|
||||
mappings: "ACAA",
|
||||
}),
|
||||
};
|
||||
await writeFixtureFiles(root, {
|
||||
...maps,
|
||||
"absent.js":
|
||||
"export const absent = 1;\n//# sourceMappingURL=absent.js.map\n",
|
||||
"mixed.js":
|
||||
"export const answer = 42;\n//# sourceMappingURL=mixed.js.map\n",
|
||||
});
|
||||
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
const digest = (text: string) =>
|
||||
createHash("sha256").update(text).digest("hex");
|
||||
for (const [name, mapPath, content] of [
|
||||
["unembedded.ts", "absent.js.map", null],
|
||||
["original.ts", "mixed.js.map", original],
|
||||
] as const) {
|
||||
const mapDigest = digest(maps[mapPath]);
|
||||
expect(graph.nodes).toContainEqual(
|
||||
expect.objectContaining({
|
||||
kind: "source-module",
|
||||
identity: expect.objectContaining({
|
||||
strategy: "source-map-original",
|
||||
source_map_sha256: mapDigest,
|
||||
original_source: name,
|
||||
source_sha256: content === null ? null : digest(content),
|
||||
}),
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
source_map_reference: expect.objectContaining({
|
||||
source_name: name,
|
||||
map_path: mapPath,
|
||||
}),
|
||||
properties: expect.objectContaining({
|
||||
content_available: content !== null,
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
artifact: expect.objectContaining({ sha256: mapDigest }),
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
}
|
||||
expect(graph.nodes).toContainEqual(
|
||||
expect.objectContaining({
|
||||
kind: "unknown",
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
source_name_available: false,
|
||||
content_available: true,
|
||||
content_sha256: digest(unnamed),
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
artifact: expect.objectContaining({
|
||||
sha256: digest(maps["mixed.js.map"]),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
expect(graph.coverage.status).toBe("complete");
|
||||
for (const [path, text] of Object.entries(maps))
|
||||
expect(await readFile(join(root, path), "utf8")).toBe(text);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -751,7 +751,7 @@ describe("web source-map sourcesContent validation", () => {
|
||||
JSON.stringify({
|
||||
version: 3,
|
||||
sources: ["source.ts"],
|
||||
sourcesContent: [null],
|
||||
sourcesContent: null,
|
||||
names: [],
|
||||
mappings: "",
|
||||
}),
|
||||
@@ -763,10 +763,61 @@ describe("web source-map sourcesContent validation", () => {
|
||||
expect(result.items[0]).toMatchObject({
|
||||
status: "included",
|
||||
original_sources: [
|
||||
{ source: `${origin}/assets/source.ts`, artifact: null },
|
||||
{
|
||||
source_index: 0,
|
||||
declaration_index: 0,
|
||||
source: `${origin}/assets/source.ts`,
|
||||
artifact: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps unnamed source slots and mapping indices", async () => {
|
||||
const result = await fetchWebSourceMaps([request], input(), undefined, {
|
||||
fetch: () =>
|
||||
Promise.resolve(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
version: 3,
|
||||
sources: [null, "source.ts"],
|
||||
sourcesContent: ["unknown source text", "named source text"],
|
||||
names: [],
|
||||
mappings: "ACAA",
|
||||
}),
|
||||
{ status: 200 },
|
||||
),
|
||||
),
|
||||
});
|
||||
|
||||
expect(result.items[0]).toMatchObject({
|
||||
status: "partial",
|
||||
original_sources: [
|
||||
{
|
||||
source_index: 0,
|
||||
declaration_index: 0,
|
||||
source: null,
|
||||
artifact: { text: "unknown source text" },
|
||||
},
|
||||
{
|
||||
source_index: 1,
|
||||
declaration_index: 1,
|
||||
source: `${origin}/assets/source.ts`,
|
||||
artifact: { text: "named source text" },
|
||||
},
|
||||
],
|
||||
mappings: [
|
||||
{
|
||||
source_index: 1,
|
||||
declaration_index: 1,
|
||||
source: `${origin}/assets/source.ts`,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.items[0]?.limitation).toContain(
|
||||
"unnamed source at declaration 0",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("source-map original dependency syntax", () => {
|
||||
|
||||
@@ -345,7 +345,7 @@ it.each(cases)(
|
||||
item.mappings.map((entry) => [
|
||||
entry.generated_line,
|
||||
entry.generated_column,
|
||||
entry.source.replace(origin + "/", ""),
|
||||
entry.source?.replace(origin + "/", "") ?? null,
|
||||
]),
|
||||
).toEqual(expected);
|
||||
expect(item.artifact.sha256).toBe(
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user