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
+11
View File
@@ -0,0 +1,11 @@
import { expect, it } from "vitest";
import { hasValidSourceMapContents } from "./sourceMapContents.js";
it("accepts absent map-level source contents and aligned nullable slots", () => {
expect(hasValidSourceMapContents(2, undefined)).toBe(true);
expect(hasValidSourceMapContents(2, null)).toBe(true);
expect(hasValidSourceMapContents(2, [null, "retained source"])).toBe(true);
expect(hasValidSourceMapContents(2, ["retained source"])).toBe(false);
expect(hasValidSourceMapContents(2, [null, 42])).toBe(false);
});
+1
View File
@@ -4,6 +4,7 @@ export const hasValidSourceMapContents = (
contents: unknown,
): boolean =>
contents === undefined ||
contents === null ||
(Array.isArray(contents) &&
contents.length === sourceCount &&
contents.every(
+23 -3
View File
@@ -14,11 +14,31 @@ describe("source-map envelope walk", () => {
const root = {
version: 3,
sections: [
{ offset: { line: 0, column: 0 }, map: leaf },
{ offset: { line: 10, column: 0 }, map: { ...leaf, sources: [] } },
{
offset: { line: 0, column: 0 },
map: {
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: { ...leaf, sources: ["b.ts"] },
},
{
offset: { line: 1, column: 0 },
map: { ...leaf, sources: ["c.ts"] },
},
],
},
},
{
offset: { line: 10, column: 0 },
map: { ...leaf, sources: ["d.ts"] },
},
],
};
expect(flattenSourceMapLeaves(root)).toHaveLength(2);
expect(flattenSourceMapLeaves(root)?.map(({ sources }) => sources)).toEqual(
[["b.ts"], ["c.ts"], ["d.ts"]],
);
});
it("rejects sections without embedded maps", () => {
+2 -1
View File
@@ -37,7 +37,8 @@ export const flattenSourceMapLeaves = (
const map = pending.pop();
if (!isVersion3Map(map)) return undefined;
if (Array.isArray(map.sections)) {
for (const section of map.sections) {
for (let index = map.sections.length - 1; index >= 0; index -= 1) {
const section = map.sections[index];
if (!isRecord(section) || !isRecord(section.map)) return undefined;
pending.push(section.map);
}
+6 -2
View File
@@ -32,7 +32,9 @@ const findingSchema = z.object({
});
const originalSourceSchema = z.object({
source: z.string(),
source_index: z.number().int().min(0).optional(),
declaration_index: z.number().int().min(0).optional(),
source: z.string().nullable(),
artifact: webTextArtifactSchema.nullable(),
});
const originalModuleEdgeSchema = z.object({
@@ -42,9 +44,11 @@ const originalModuleEdgeSchema = z.object({
resolved_source: z.string().nullable(),
});
const sourceMapMappingSchema = z.object({
source_index: z.number().int().min(0).optional(),
declaration_index: z.number().int().min(0).optional(),
generated_line: z.number().int().min(1),
generated_column: z.number().int().min(0),
source: z.string(),
source: z.string().nullable(),
original_line: z.number().int().min(1),
original_column: z.number().int().min(0),
name: z.string().nullable(),