Source Map Consumer
Source Map Consumer
import type {
IndexSourceMap,
IndexSourceMapSection,
BasicSourceMap,
MixedSourceMap,
} from './SourceMapTypes';
type SearchPosition = {
columnNumber: number,
lineNumber: number,
};
type ResultPosition = {
column: number,
line: number,
sourceContent: string,
sourceURL: string,
};
function originalPositionFor({
columnNumber,
lineNumber,
}: SearchPosition): ResultPosition {
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-
based.
const targetColumnNumber = columnNumber - 1;
let startIndex = 0;
let stopIndex = lineMappings.length - 1;
let index = -1;
while (startIndex <= stopIndex) {
index = Math.floor((stopIndex + startIndex) / 2);
nearestEntry = lineMappings[index];
if (nearestEntry == null) {
// TODO maybe fall back to the runtime source instead of throwing?
throw Error(
`Could not find runtime location for line:${lineNumber} and column:$
{columnNumber}`,
);
}
return {
column,
line,
sourceContent: ((sourceContent: any): string),
sourceURL: ((sourceURL: any): string),
};
}
return (({
originalPositionFor,
}: any): SourceMapConsumerType);
}
type Section = {
+generatedColumn: number,
+generatedLine: number,
+map: MixedSourceMap,
if (
offsetLine < lastOffset.line ||
(offsetLine === lastOffset.line && offsetColumn < lastOffset.column)
) {
throw new Error('Section offsets must be ordered and non-overlapping.');
}
lastOffset = offset;
return {
// The offset fields are 0-based, but we use 1-based indices when
encoding/decoding from VLQ.
generatedLine: offsetLine + 1,
generatedColumn: offsetColumn + 1,
map: section.map,
sourceMapConsumer: null,
};
});
function originalPositionFor({
columnNumber,
lineNumber,
}: SearchPosition): ResultPosition {
// Error.prototype.stack columns are 1-based (like most IDEs) but ASTs are 0-
based.
const targetColumnNumber = columnNumber - 1;
let startIndex = 0;
let stopIndex = sections.length - 1;
let index = -1;
while (startIndex <= stopIndex) {
index = Math.floor((stopIndex + startIndex) / 2);
section = sections[index];
if (section == null) {
// TODO maybe fall back to the runtime source instead of throwing?
throw Error(
`Could not find matching section for line:${lineNumber} and column:$
{columnNumber}`,
);
}
return section.sourceMapConsumer.originalPositionFor({
columnNumber,
lineNumber,
});
}
return (({
originalPositionFor,
}: any): SourceMapConsumerType);
}