refactor(domain): move the validated source map leaf decoder into src/domain (#11498)

This commit is contained in:
morluto authored and GitHub committed 2026-11-15 16:10:11 -05:00
1 parent ea668fc953
commit 733b361073
4 files changed
+4 -5

No files matched your search

@@ -1,63 +0,0 @@
import { malformedInput } from "../../domain/parseFailure.js";
import { TraceMap, decodedMappings } from "@jridgewell/trace-mapping";
import {
SOURCE_MAP_FORMAT,
sourceMapSectionOverlap,
type SourceMapLeaf,
} from "../../domain/sourceMapFormat.js";
/** Determine whether a leaf-local mapping falls before its enclosing boundary. */
const isBeforeSourceMapLeafStop = (
leaf: SourceMapLeaf,
line: number,
column: number,
): boolean => {
if (leaf.stop === null) return true;
const generatedLine = leaf.offset.line + line;
const generatedColumn = column + (line === 0 ? leaf.offset.column : 0);
return (
generatedLine < leaf.stop.line ||
(generatedLine === leaf.stop.line && generatedColumn < leaf.stop.column)
);
};
/** Decode leaf mappings and validate producer indices before any consumer publishes them. */
export const decodeValidatedSourceMapLeaves = (
leaves: readonly SourceMapLeaf[],
mapUrl: string,
check?: () => void,
): readonly {
readonly leaf: SourceMapLeaf;
readonly map: TraceMap;
readonly rows: ReturnType<typeof decodedMappings>;
}[] =>
leaves.map((leaf) => {
check?.();
const map = new TraceMap(leaf.codecMap, mapUrl);
const rows = decodedMappings(map);
for (const [line, row] of rows.entries()) {
if ((line & 255) === 0) check?.();
for (const segment of row) {
const generatedLine = leaf.offset.line + line;
const generatedColumn =
segment[0] + (line === 0 ? leaf.offset.column : 0);
if (!isBeforeSourceMapLeafStop(leaf, line, segment[0]))
throw sourceMapSectionOverlap(
"Embedded source-map mappings overlap the following section boundary.",
);
if (
!Number.isSafeInteger(generatedLine) ||
!Number.isSafeInteger(generatedColumn) ||
![1, 4, 5].includes(segment.length) ||
segment.some((value) => !Number.isSafeInteger(value) || value < 0) ||
(segment.length !== 1 && segment[1] >= leaf.map.sources.length) ||
(segment.length === 5 && segment[4] >= leaf.map.names.length)
)
throw malformedInput(
SOURCE_MAP_FORMAT,
"Decoded local or absolute position, source index or name index is invalid.",
);
}
}
return { leaf, map, rows };
});
+1 -1
View File
@@ -29,7 +29,7 @@ import {
sourceMapLimitFailure,
type SourceMapSourceDeclaration,
} from "../../domain/sourceMapFormat.js";
import { decodeValidatedSourceMapLeaves } from "./DecodedSourceMap.js";
import { decodeValidatedSourceMapLeaves } from "../../domain/decodedSourceMap.js";
/** Decode with the pinned upstream codec; preserve every equal-position candidate. */
export const traceSourceMap = (