Files
reverse-engineering-anything/src/domain/decodedSourceMap.ts
T
ShuaixinHuangandShuaixinHuang 919531c7c1 fix(source-maps): retain all candidates at shared section boundaries (#12026)
* fix(source-maps): retain mappings at shared section boundaries

* docs(source-maps): align boundary and decoder guidance

* test(source-maps): type named browser source-map leaves

---------

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
2026-11-18 09:42:28 -05:00

64 lines
2.2 KiB
TypeScript

import { malformedInput } from "./parseFailure.js";
import { TraceMap, decodedMappings } from "@jridgewell/trace-mapping";
import {
SOURCE_MAP_FORMAT,
sourceMapSectionOverlap,
type SourceMapLeaf,
} from "./sourceMapFormat.js";
/** ECMA-426 permits equal-position mappings at a following section's start. */
const isWithinSourceMapLeafStop = (
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 (!isWithinSourceMapLeafStop(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 };
});