by-source.ts 1.2 KB

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