refactor(source-map): share segment lower bound (#15925)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 12:20:58 +00:00
1 parent 01d98ffe14
commit fb084e21b2
1 file changed
+33 -62
+33 -62
View File
@@ -53,29 +53,27 @@ export const traceSourceMap = (
const line = matched.line - leaf.offset.line - 1;
const column = matched.column - (line === 0 ? leaf.offset.column : 0);
const row = line >= 0 ? rows[line] : undefined;
if (row !== undefined && row.length > 0) {
const start = firstSegmentIndexAt(row, column);
if (start !== -1) {
for (let index = start; index < row.length; index += 1) {
const segment = row[index]!;
if (segment[0] !== column) break;
const match = originalMatch(
segment,
map,
url,
inspected.declarations,
sourceBase,
contentFactsCache,
if (row !== undefined) {
const start = lowerBoundSegmentIndex(row, column);
for (let index = start; index < row.length; index += 1) {
const segment = row[index]!;
if (segment[0] !== column) break;
const match = originalMatch(
segment,
map,
url,
inspected.declarations,
sourceBase,
contentFactsCache,
);
bytes += Buffer.byteLength(JSON.stringify(match)) + 1;
if (bytes > limits.outputBytes)
throw sourceMapLimitFailure(
`Complete source-map point evidence exceeds output admission; no partial evidence was returned.`,
"output_bytes",
limits.outputBytes,
);
bytes += Buffer.byteLength(JSON.stringify(match)) + 1;
if (bytes > limits.outputBytes)
throw sourceMapLimitFailure(
`Complete source-map point evidence exceeds output admission; no partial evidence was returned.`,
"output_bytes",
limits.outputBytes,
);
matches.push(match);
}
matches.push(match);
}
}
}
@@ -135,11 +133,11 @@ const greatestPosition = (
// Rows from @jridgewell/trace-mapping are sorted by column; the last entry is greatest.
column = row[row.length - 1]![0] + lineOffset;
} else {
const index = greatestSegmentIndexAtOrBefore(
row,
position.column - lineOffset,
);
if (index >= 0) column = row[index]![0] + lineOffset;
const target = position.column - lineOffset;
const index = lowerBoundSegmentIndex(row, target);
const localColumn =
row[index]?.[0] === target ? target : row[index - 1]?.[0];
if (localColumn !== undefined) column = localColumn + lineOffset;
}
if (column === undefined) continue;
const absoluteLine = leaf.offset.line + line + 1;
@@ -155,48 +153,21 @@ const greatestPosition = (
return matched;
};
/** Largest index in column-sorted `row` with `segment[0] <= targetColumn`, or `-1`. */
const greatestSegmentIndexAtOrBefore = (
/** First index whose generated column is at least the target, or row length. */
const lowerBoundSegmentIndex = (
row: readonly SourceMapSegment[],
targetColumn: number,
target: number,
): number => {
let low = 0;
let high = row.length - 1;
let best = -1;
while (low <= high) {
let high = row.length;
while (low < high) {
const mid = low + ((high - low) >> 1);
if (row[mid]![0] <= targetColumn) {
best = mid;
low = mid + 1;
} else {
high = mid - 1;
}
if (row[mid]![0] < target) low = mid + 1;
else high = mid;
}
return best;
return low;
};
/** First index in column-sorted `row` with `segment[0] === column`, or `-1`. */
const firstSegmentIndexAt = (
row: readonly SourceMapSegment[],
column: number,
): number => {
let low = 0;
let high = row.length - 1;
let first = -1;
while (low <= high) {
const mid = low + ((high - low) >> 1);
const value = row[mid]![0];
if (value === column) {
first = mid;
high = mid - 1;
} else if (value < column) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return first;
};
const originalMatch = (
segment: Readonly<SourceMapSegment>,
map: TraceMap,