| import { COLUMN, SOURCES_INDEX, SOURCE_LINE, SOURCE_COLUMN } from './sourcemap-segment'; |
| import { memoizedBinarySearch, upperBound } from './binary-search'; |
|
|
| import type { ReverseSegment, SourceMapSegment } from './sourcemap-segment'; |
| import type { MemoState } from './binary-search'; |
|
|
| export type Source = { |
| __proto__: null; |
| [line: number]: Exclude<ReverseSegment, [number]>[]; |
| }; |
|
|
| |
| |
| export default function buildBySources( |
| decoded: readonly SourceMapSegment[][], |
| memos: MemoState[], |
| ): Source[] { |
| const sources: Source[] = memos.map(buildNullArray); |
|
|
| for (let i = 0; i < decoded.length; i++) { |
| const line = decoded[i]; |
| for (let j = 0; j < line.length; j++) { |
| const seg = line[j]; |
| if (seg.length === 1) continue; |
|
|
| const sourceIndex = seg[SOURCES_INDEX]; |
| const sourceLine = seg[SOURCE_LINE]; |
| const sourceColumn = seg[SOURCE_COLUMN]; |
| const originalSource = sources[sourceIndex]; |
| const originalLine = (originalSource[sourceLine] ||= []); |
| const memo = memos[sourceIndex]; |
|
|
| |
| |
| |
| |
| let index = upperBound( |
| originalLine, |
| sourceColumn, |
| memoizedBinarySearch(originalLine, sourceColumn, memo, sourceLine), |
| ); |
|
|
| memo.lastIndex = ++index; |
| insert(originalLine, index, [sourceColumn, i, seg[COLUMN]]); |
| } |
| } |
|
|
| return sources; |
| } |
|
|
| function insert<T>(array: T[], index: number, value: T) { |
| for (let i = array.length; i > index; i--) { |
| array[i] = array[i - 1]; |
| } |
| array[index] = value; |
| } |
|
|
| |
| |
| |
| |
| |
| function buildNullArray<T extends { __proto__: null }>(): T { |
| return { __proto__: null } as T; |
| } |
|
|