/
getOriginalLocation.ts
31 lines (30 loc) · 1.06 KB
/
getOriginalLocation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { DecodedSourceMapOrMissing, ExistingDecodedSourceMap } from '../rollup/types';
export function getOriginalLocation(
sourcemapChain: readonly DecodedSourceMapOrMissing[],
location: { column: number; line: number }
): { column: number; line: number } {
const filteredSourcemapChain = sourcemapChain.filter(
(sourcemap): sourcemap is ExistingDecodedSourceMap => !!sourcemap.mappings
);
traceSourcemap: while (filteredSourcemapChain.length > 0) {
const sourcemap = filteredSourcemapChain.pop()!;
const line = sourcemap.mappings[location.line - 1];
if (line) {
const filteredLine = line.filter(
(segment): segment is [number, number, number, number] => segment.length > 1
);
const lastSegment = filteredLine[filteredLine.length - 1];
for (const segment of filteredLine) {
if (segment[0] >= location.column || segment === lastSegment) {
location = {
column: segment[3],
line: segment[2] + 1
};
continue traceSourcemap;
}
}
}
throw new Error("Can't resolve original location of error.");
}
return location;
}