/
any-map.ts
128 lines (108 loc) · 4.35 KB
/
any-map.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { TraceMap, presortedDecodedMap, decodedMappings } from './trace-mapping';
import {
COLUMN,
SOURCES_INDEX,
SOURCE_LINE,
SOURCE_COLUMN,
NAMES_INDEX,
} from './sourcemap-segment';
import type { Section, DecodedSourceMap, SectionedSourceMapInput } from './types';
import type { SourceMapSegment } from './sourcemap-segment';
type AnyMap = {
new (map: SectionedSourceMapInput, mapUrl?: string | null): TraceMap;
(map: SectionedSourceMapInput, mapUrl?: string | null): TraceMap;
};
export const AnyMap: AnyMap = function (map, mapUrl) {
const parsed =
typeof map === 'string' ? (JSON.parse(map) as Exclude<SectionedSourceMapInput, string>) : map;
if (!('sections' in parsed)) return new TraceMap(parsed, mapUrl);
const mappings: SourceMapSegment[][] = [];
const sources: string[] = [];
const sourcesContent: (string | null)[] = [];
const names: string[] = [];
const { sections } = parsed;
let i = 0;
for (; i < sections.length - 1; i++) {
const no = sections[i + 1].offset;
addSection(sections[i], mapUrl, mappings, sources, sourcesContent, names, no.line, no.column);
}
if (sections.length > 0) {
addSection(sections[i], mapUrl, mappings, sources, sourcesContent, names, Infinity, Infinity);
}
const joined: DecodedSourceMap = {
version: 3,
file: parsed.file,
names,
sources,
sourcesContent,
mappings,
};
return presortedDecodedMap(joined);
} as AnyMap;
function addSection(
section: Section,
mapUrl: string | null | undefined,
mappings: SourceMapSegment[][],
sources: string[],
sourcesContent: (string | null)[],
names: string[],
stopLine: number,
stopColumn: number,
) {
const map = AnyMap(section.map, mapUrl);
const { line: lineOffset, column: columnOffset } = section.offset;
const sourcesOffset = sources.length;
const namesOffset = names.length;
const decoded = decodedMappings(map);
const { resolvedSources } = map;
append(sources, resolvedSources);
append(sourcesContent, map.sourcesContent || fillSourcesContent(resolvedSources.length));
append(names, map.names);
// If this section jumps forwards several lines, we need to add lines to the output mappings catch up.
for (let i = mappings.length; i <= lineOffset; i++) mappings.push([]);
// We can only add so many lines before we step into the range that the next section's map
// controls. When we get to the last line, then we'll start checking the segments to see if
// they've crossed into the column range.
const stopI = stopLine - lineOffset;
const len = Math.min(decoded.length, stopI + 1);
for (let i = 0; i < len; i++) {
const line = decoded[i];
// On the 0th loop, the line will already exist due to a previous section, or the line catch up
// loop above.
const out = i === 0 ? mappings[lineOffset] : (mappings[lineOffset + i] = []);
// On the 0th loop, the section's column offset shifts us forward. On all other lines (since the
// map can be multiple lines), it doesn't.
const cOffset = i === 0 ? columnOffset : 0;
for (let j = 0; j < line.length; j++) {
const seg = line[j];
const column = cOffset + seg[COLUMN];
// If this segment steps into the column range that the next section's map controls, we need
// to stop early.
if (i === stopI && column >= stopColumn) break;
if (seg.length === 1) {
out.push([column]);
continue;
}
const sourcesIndex = sourcesOffset + seg[SOURCES_INDEX];
const sourceLine = seg[SOURCE_LINE];
const sourceColumn = seg[SOURCE_COLUMN];
if (seg.length === 4) {
out.push([column, sourcesIndex, sourceLine, sourceColumn]);
continue;
}
out.push([column, sourcesIndex, sourceLine, sourceColumn, namesOffset + seg[NAMES_INDEX]]);
}
}
}
function append<T>(arr: T[], other: T[]) {
for (let i = 0; i < other.length; i++) arr.push(other[i]);
}
// Sourcemaps don't need to have sourcesContent, and if they don't, we need to create an array of
// equal length to the sources. This is because the sources and sourcesContent are paired arrays,
// where `sourcesContent[i]` is the content of the `sources[i]` file. If we didn't, then joined
// sourcemap would desynchronize the sources/contents.
function fillSourcesContent(len: number): null[] {
const sourcesContent = [];
for (let i = 0; i < len; i++) sourcesContent[i] = null;
return sourcesContent;
}