| 1 | import { COLUMN, SOURCES_INDEX, SOURCE_LINE, SOURCE_COLUMN } from './sourcemap-segment';
|
|---|
| 2 | import { sortComparator } from './sort';
|
|---|
| 3 |
|
|---|
| 4 | import type { ReverseSegment, SourceMapSegment } from './sourcemap-segment';
|
|---|
| 5 |
|
|---|
| 6 | export type Source = ReverseSegment[][];
|
|---|
| 7 |
|
|---|
| 8 | // Rebuilds the original source files, with mappings that are ordered by source line/column instead
|
|---|
| 9 | // of generated line/column.
|
|---|
| 10 | export default function buildBySources(
|
|---|
| 11 | decoded: readonly SourceMapSegment[][],
|
|---|
| 12 | memos: unknown[],
|
|---|
| 13 | ): Source[] {
|
|---|
| 14 | const sources: Source[] = memos.map(() => []);
|
|---|
| 15 |
|
|---|
| 16 | for (let i = 0; i < decoded.length; i++) {
|
|---|
| 17 | const line = decoded[i];
|
|---|
| 18 | for (let j = 0; j < line.length; j++) {
|
|---|
| 19 | const seg = line[j];
|
|---|
| 20 | if (seg.length === 1) continue;
|
|---|
| 21 |
|
|---|
| 22 | const sourceIndex = seg[SOURCES_INDEX];
|
|---|
| 23 | const sourceLine = seg[SOURCE_LINE];
|
|---|
| 24 | const sourceColumn = seg[SOURCE_COLUMN];
|
|---|
| 25 |
|
|---|
| 26 | const source = sources[sourceIndex];
|
|---|
| 27 | const segs = (source[sourceLine] ||= []);
|
|---|
| 28 | segs.push([sourceColumn, i, seg[COLUMN]]);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | for (let i = 0; i < sources.length; i++) {
|
|---|
| 33 | const source = sources[i];
|
|---|
| 34 | for (let j = 0; j < source.length; j++) {
|
|---|
| 35 | const line = source[j];
|
|---|
| 36 | if (line) line.sort(sortComparator);
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | return sources;
|
|---|
| 41 | }
|
|---|