1 | import { encode, decode } from '@jridgewell/sourcemap-codec';
|
---|
2 | import resolveUri from '@jridgewell/resolve-uri';
|
---|
3 |
|
---|
4 | function resolve(input, base) {
|
---|
5 | // The base is always treated as a directory, if it's not empty.
|
---|
6 | // https://github.com/mozilla/source-map/blob/8cb3ee57/lib/util.js#L327
|
---|
7 | // https://github.com/chromium/chromium/blob/da4adbb3/third_party/blink/renderer/devtools/front_end/sdk/SourceMap.js#L400-L401
|
---|
8 | if (base && !base.endsWith('/'))
|
---|
9 | base += '/';
|
---|
10 | return resolveUri(input, base);
|
---|
11 | }
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Removes everything after the last "/", but leaves the slash.
|
---|
15 | */
|
---|
16 | function stripFilename(path) {
|
---|
17 | if (!path)
|
---|
18 | return '';
|
---|
19 | const index = path.lastIndexOf('/');
|
---|
20 | return path.slice(0, index + 1);
|
---|
21 | }
|
---|
22 |
|
---|
23 | const COLUMN = 0;
|
---|
24 | const SOURCES_INDEX = 1;
|
---|
25 | const SOURCE_LINE = 2;
|
---|
26 | const SOURCE_COLUMN = 3;
|
---|
27 | const NAMES_INDEX = 4;
|
---|
28 | const REV_GENERATED_LINE = 1;
|
---|
29 | const REV_GENERATED_COLUMN = 2;
|
---|
30 |
|
---|
31 | function maybeSort(mappings, owned) {
|
---|
32 | const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
|
---|
33 | if (unsortedIndex === mappings.length)
|
---|
34 | return mappings;
|
---|
35 | // If we own the array (meaning we parsed it from JSON), then we're free to directly mutate it. If
|
---|
36 | // not, we do not want to modify the consumer's input array.
|
---|
37 | if (!owned)
|
---|
38 | mappings = mappings.slice();
|
---|
39 | for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
|
---|
40 | mappings[i] = sortSegments(mappings[i], owned);
|
---|
41 | }
|
---|
42 | return mappings;
|
---|
43 | }
|
---|
44 | function nextUnsortedSegmentLine(mappings, start) {
|
---|
45 | for (let i = start; i < mappings.length; i++) {
|
---|
46 | if (!isSorted(mappings[i]))
|
---|
47 | return i;
|
---|
48 | }
|
---|
49 | return mappings.length;
|
---|
50 | }
|
---|
51 | function isSorted(line) {
|
---|
52 | for (let j = 1; j < line.length; j++) {
|
---|
53 | if (line[j][COLUMN] < line[j - 1][COLUMN]) {
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 | function sortSegments(line, owned) {
|
---|
60 | if (!owned)
|
---|
61 | line = line.slice();
|
---|
62 | return line.sort(sortComparator);
|
---|
63 | }
|
---|
64 | function sortComparator(a, b) {
|
---|
65 | return a[COLUMN] - b[COLUMN];
|
---|
66 | }
|
---|
67 |
|
---|
68 | let found = false;
|
---|
69 | /**
|
---|
70 | * A binary search implementation that returns the index if a match is found.
|
---|
71 | * If no match is found, then the left-index (the index associated with the item that comes just
|
---|
72 | * before the desired index) is returned. To maintain proper sort order, a splice would happen at
|
---|
73 | * the next index:
|
---|
74 | *
|
---|
75 | * ```js
|
---|
76 | * const array = [1, 3];
|
---|
77 | * const needle = 2;
|
---|
78 | * const index = binarySearch(array, needle, (item, needle) => item - needle);
|
---|
79 | *
|
---|
80 | * assert.equal(index, 0);
|
---|
81 | * array.splice(index + 1, 0, needle);
|
---|
82 | * assert.deepEqual(array, [1, 2, 3]);
|
---|
83 | * ```
|
---|
84 | */
|
---|
85 | function binarySearch(haystack, needle, low, high) {
|
---|
86 | while (low <= high) {
|
---|
87 | const mid = low + ((high - low) >> 1);
|
---|
88 | const cmp = haystack[mid][COLUMN] - needle;
|
---|
89 | if (cmp === 0) {
|
---|
90 | found = true;
|
---|
91 | return mid;
|
---|
92 | }
|
---|
93 | if (cmp < 0) {
|
---|
94 | low = mid + 1;
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | high = mid - 1;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | found = false;
|
---|
101 | return low - 1;
|
---|
102 | }
|
---|
103 | function upperBound(haystack, needle, index) {
|
---|
104 | for (let i = index + 1; i < haystack.length; index = i++) {
|
---|
105 | if (haystack[i][COLUMN] !== needle)
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | return index;
|
---|
109 | }
|
---|
110 | function lowerBound(haystack, needle, index) {
|
---|
111 | for (let i = index - 1; i >= 0; index = i--) {
|
---|
112 | if (haystack[i][COLUMN] !== needle)
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | return index;
|
---|
116 | }
|
---|
117 | function memoizedState() {
|
---|
118 | return {
|
---|
119 | lastKey: -1,
|
---|
120 | lastNeedle: -1,
|
---|
121 | lastIndex: -1,
|
---|
122 | };
|
---|
123 | }
|
---|
124 | /**
|
---|
125 | * This overly complicated beast is just to record the last tested line/column and the resulting
|
---|
126 | * index, allowing us to skip a few tests if mappings are monotonically increasing.
|
---|
127 | */
|
---|
128 | function memoizedBinarySearch(haystack, needle, state, key) {
|
---|
129 | const { lastKey, lastNeedle, lastIndex } = state;
|
---|
130 | let low = 0;
|
---|
131 | let high = haystack.length - 1;
|
---|
132 | if (key === lastKey) {
|
---|
133 | if (needle === lastNeedle) {
|
---|
134 | found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle;
|
---|
135 | return lastIndex;
|
---|
136 | }
|
---|
137 | if (needle >= lastNeedle) {
|
---|
138 | // lastIndex may be -1 if the previous needle was not found.
|
---|
139 | low = lastIndex === -1 ? 0 : lastIndex;
|
---|
140 | }
|
---|
141 | else {
|
---|
142 | high = lastIndex;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | state.lastKey = key;
|
---|
146 | state.lastNeedle = needle;
|
---|
147 | return (state.lastIndex = binarySearch(haystack, needle, low, high));
|
---|
148 | }
|
---|
149 |
|
---|
150 | // Rebuilds the original source files, with mappings that are ordered by source line/column instead
|
---|
151 | // of generated line/column.
|
---|
152 | function buildBySources(decoded, memos) {
|
---|
153 | const sources = memos.map(buildNullArray);
|
---|
154 | for (let i = 0; i < decoded.length; i++) {
|
---|
155 | const line = decoded[i];
|
---|
156 | for (let j = 0; j < line.length; j++) {
|
---|
157 | const seg = line[j];
|
---|
158 | if (seg.length === 1)
|
---|
159 | continue;
|
---|
160 | const sourceIndex = seg[SOURCES_INDEX];
|
---|
161 | const sourceLine = seg[SOURCE_LINE];
|
---|
162 | const sourceColumn = seg[SOURCE_COLUMN];
|
---|
163 | const originalSource = sources[sourceIndex];
|
---|
164 | const originalLine = (originalSource[sourceLine] || (originalSource[sourceLine] = []));
|
---|
165 | const memo = memos[sourceIndex];
|
---|
166 | // The binary search either found a match, or it found the left-index just before where the
|
---|
167 | // segment should go. Either way, we want to insert after that. And there may be multiple
|
---|
168 | // generated segments associated with an original location, so there may need to move several
|
---|
169 | // indexes before we find where we need to insert.
|
---|
170 | let index = upperBound(originalLine, sourceColumn, memoizedBinarySearch(originalLine, sourceColumn, memo, sourceLine));
|
---|
171 | memo.lastIndex = ++index;
|
---|
172 | insert(originalLine, index, [sourceColumn, i, seg[COLUMN]]);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | return sources;
|
---|
176 | }
|
---|
177 | function insert(array, index, value) {
|
---|
178 | for (let i = array.length; i > index; i--) {
|
---|
179 | array[i] = array[i - 1];
|
---|
180 | }
|
---|
181 | array[index] = value;
|
---|
182 | }
|
---|
183 | // Null arrays allow us to use ordered index keys without actually allocating contiguous memory like
|
---|
184 | // a real array. We use a null-prototype object to avoid prototype pollution and deoptimizations.
|
---|
185 | // Numeric properties on objects are magically sorted in ascending order by the engine regardless of
|
---|
186 | // the insertion order. So, by setting any numeric keys, even out of order, we'll get ascending
|
---|
187 | // order when iterating with for-in.
|
---|
188 | function buildNullArray() {
|
---|
189 | return { __proto__: null };
|
---|
190 | }
|
---|
191 |
|
---|
192 | const AnyMap = function (map, mapUrl) {
|
---|
193 | const parsed = parse(map);
|
---|
194 | if (!('sections' in parsed)) {
|
---|
195 | return new TraceMap(parsed, mapUrl);
|
---|
196 | }
|
---|
197 | const mappings = [];
|
---|
198 | const sources = [];
|
---|
199 | const sourcesContent = [];
|
---|
200 | const names = [];
|
---|
201 | const ignoreList = [];
|
---|
202 | recurse(parsed, mapUrl, mappings, sources, sourcesContent, names, ignoreList, 0, 0, Infinity, Infinity);
|
---|
203 | const joined = {
|
---|
204 | version: 3,
|
---|
205 | file: parsed.file,
|
---|
206 | names,
|
---|
207 | sources,
|
---|
208 | sourcesContent,
|
---|
209 | mappings,
|
---|
210 | ignoreList,
|
---|
211 | };
|
---|
212 | return presortedDecodedMap(joined);
|
---|
213 | };
|
---|
214 | function parse(map) {
|
---|
215 | return typeof map === 'string' ? JSON.parse(map) : map;
|
---|
216 | }
|
---|
217 | function recurse(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
|
---|
218 | const { sections } = input;
|
---|
219 | for (let i = 0; i < sections.length; i++) {
|
---|
220 | const { map, offset } = sections[i];
|
---|
221 | let sl = stopLine;
|
---|
222 | let sc = stopColumn;
|
---|
223 | if (i + 1 < sections.length) {
|
---|
224 | const nextOffset = sections[i + 1].offset;
|
---|
225 | sl = Math.min(stopLine, lineOffset + nextOffset.line);
|
---|
226 | if (sl === stopLine) {
|
---|
227 | sc = Math.min(stopColumn, columnOffset + nextOffset.column);
|
---|
228 | }
|
---|
229 | else if (sl < stopLine) {
|
---|
230 | sc = columnOffset + nextOffset.column;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | addSection(map, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset + offset.line, columnOffset + offset.column, sl, sc);
|
---|
234 | }
|
---|
235 | }
|
---|
236 | function addSection(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
|
---|
237 | const parsed = parse(input);
|
---|
238 | if ('sections' in parsed)
|
---|
239 | return recurse(...arguments);
|
---|
240 | const map = new TraceMap(parsed, mapUrl);
|
---|
241 | const sourcesOffset = sources.length;
|
---|
242 | const namesOffset = names.length;
|
---|
243 | const decoded = decodedMappings(map);
|
---|
244 | const { resolvedSources, sourcesContent: contents, ignoreList: ignores } = map;
|
---|
245 | append(sources, resolvedSources);
|
---|
246 | append(names, map.names);
|
---|
247 | if (contents)
|
---|
248 | append(sourcesContent, contents);
|
---|
249 | else
|
---|
250 | for (let i = 0; i < resolvedSources.length; i++)
|
---|
251 | sourcesContent.push(null);
|
---|
252 | if (ignores)
|
---|
253 | for (let i = 0; i < ignores.length; i++)
|
---|
254 | ignoreList.push(ignores[i] + sourcesOffset);
|
---|
255 | for (let i = 0; i < decoded.length; i++) {
|
---|
256 | const lineI = lineOffset + i;
|
---|
257 | // We can only add so many lines before we step into the range that the next section's map
|
---|
258 | // controls. When we get to the last line, then we'll start checking the segments to see if
|
---|
259 | // they've crossed into the column range. But it may not have any columns that overstep, so we
|
---|
260 | // still need to check that we don't overstep lines, too.
|
---|
261 | if (lineI > stopLine)
|
---|
262 | return;
|
---|
263 | // The out line may already exist in mappings (if we're continuing the line started by a
|
---|
264 | // previous section). Or, we may have jumped ahead several lines to start this section.
|
---|
265 | const out = getLine(mappings, lineI);
|
---|
266 | // On the 0th loop, the section's column offset shifts us forward. On all other lines (since the
|
---|
267 | // map can be multiple lines), it doesn't.
|
---|
268 | const cOffset = i === 0 ? columnOffset : 0;
|
---|
269 | const line = decoded[i];
|
---|
270 | for (let j = 0; j < line.length; j++) {
|
---|
271 | const seg = line[j];
|
---|
272 | const column = cOffset + seg[COLUMN];
|
---|
273 | // If this segment steps into the column range that the next section's map controls, we need
|
---|
274 | // to stop early.
|
---|
275 | if (lineI === stopLine && column >= stopColumn)
|
---|
276 | return;
|
---|
277 | if (seg.length === 1) {
|
---|
278 | out.push([column]);
|
---|
279 | continue;
|
---|
280 | }
|
---|
281 | const sourcesIndex = sourcesOffset + seg[SOURCES_INDEX];
|
---|
282 | const sourceLine = seg[SOURCE_LINE];
|
---|
283 | const sourceColumn = seg[SOURCE_COLUMN];
|
---|
284 | out.push(seg.length === 4
|
---|
285 | ? [column, sourcesIndex, sourceLine, sourceColumn]
|
---|
286 | : [column, sourcesIndex, sourceLine, sourceColumn, namesOffset + seg[NAMES_INDEX]]);
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 | function append(arr, other) {
|
---|
291 | for (let i = 0; i < other.length; i++)
|
---|
292 | arr.push(other[i]);
|
---|
293 | }
|
---|
294 | function getLine(arr, index) {
|
---|
295 | for (let i = arr.length; i <= index; i++)
|
---|
296 | arr[i] = [];
|
---|
297 | return arr[index];
|
---|
298 | }
|
---|
299 |
|
---|
300 | const LINE_GTR_ZERO = '`line` must be greater than 0 (lines start at line 1)';
|
---|
301 | const COL_GTR_EQ_ZERO = '`column` must be greater than or equal to 0 (columns start at column 0)';
|
---|
302 | const LEAST_UPPER_BOUND = -1;
|
---|
303 | const GREATEST_LOWER_BOUND = 1;
|
---|
304 | class TraceMap {
|
---|
305 | constructor(map, mapUrl) {
|
---|
306 | const isString = typeof map === 'string';
|
---|
307 | if (!isString && map._decodedMemo)
|
---|
308 | return map;
|
---|
309 | const parsed = (isString ? JSON.parse(map) : map);
|
---|
310 | const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
|
---|
311 | this.version = version;
|
---|
312 | this.file = file;
|
---|
313 | this.names = names || [];
|
---|
314 | this.sourceRoot = sourceRoot;
|
---|
315 | this.sources = sources;
|
---|
316 | this.sourcesContent = sourcesContent;
|
---|
317 | this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || undefined;
|
---|
318 | const from = resolve(sourceRoot || '', stripFilename(mapUrl));
|
---|
319 | this.resolvedSources = sources.map((s) => resolve(s || '', from));
|
---|
320 | const { mappings } = parsed;
|
---|
321 | if (typeof mappings === 'string') {
|
---|
322 | this._encoded = mappings;
|
---|
323 | this._decoded = undefined;
|
---|
324 | }
|
---|
325 | else {
|
---|
326 | this._encoded = undefined;
|
---|
327 | this._decoded = maybeSort(mappings, isString);
|
---|
328 | }
|
---|
329 | this._decodedMemo = memoizedState();
|
---|
330 | this._bySources = undefined;
|
---|
331 | this._bySourceMemos = undefined;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | /**
|
---|
335 | * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
|
---|
336 | * with public access modifiers.
|
---|
337 | */
|
---|
338 | function cast(map) {
|
---|
339 | return map;
|
---|
340 | }
|
---|
341 | /**
|
---|
342 | * Returns the encoded (VLQ string) form of the SourceMap's mappings field.
|
---|
343 | */
|
---|
344 | function encodedMappings(map) {
|
---|
345 | var _a;
|
---|
346 | var _b;
|
---|
347 | return ((_a = (_b = cast(map))._encoded) !== null && _a !== void 0 ? _a : (_b._encoded = encode(cast(map)._decoded)));
|
---|
348 | }
|
---|
349 | /**
|
---|
350 | * Returns the decoded (array of lines of segments) form of the SourceMap's mappings field.
|
---|
351 | */
|
---|
352 | function decodedMappings(map) {
|
---|
353 | var _a;
|
---|
354 | return ((_a = cast(map))._decoded || (_a._decoded = decode(cast(map)._encoded)));
|
---|
355 | }
|
---|
356 | /**
|
---|
357 | * A low-level API to find the segment associated with a generated line/column (think, from a
|
---|
358 | * stack trace). Line and column here are 0-based, unlike `originalPositionFor`.
|
---|
359 | */
|
---|
360 | function traceSegment(map, line, column) {
|
---|
361 | const decoded = decodedMappings(map);
|
---|
362 | // It's common for parent source maps to have pointers to lines that have no
|
---|
363 | // mapping (like a "//# sourceMappingURL=") at the end of the child file.
|
---|
364 | if (line >= decoded.length)
|
---|
365 | return null;
|
---|
366 | const segments = decoded[line];
|
---|
367 | const index = traceSegmentInternal(segments, cast(map)._decodedMemo, line, column, GREATEST_LOWER_BOUND);
|
---|
368 | return index === -1 ? null : segments[index];
|
---|
369 | }
|
---|
370 | /**
|
---|
371 | * A higher-level API to find the source/line/column associated with a generated line/column
|
---|
372 | * (think, from a stack trace). Line is 1-based, but column is 0-based, due to legacy behavior in
|
---|
373 | * `source-map` library.
|
---|
374 | */
|
---|
375 | function originalPositionFor(map, needle) {
|
---|
376 | let { line, column, bias } = needle;
|
---|
377 | line--;
|
---|
378 | if (line < 0)
|
---|
379 | throw new Error(LINE_GTR_ZERO);
|
---|
380 | if (column < 0)
|
---|
381 | throw new Error(COL_GTR_EQ_ZERO);
|
---|
382 | const decoded = decodedMappings(map);
|
---|
383 | // It's common for parent source maps to have pointers to lines that have no
|
---|
384 | // mapping (like a "//# sourceMappingURL=") at the end of the child file.
|
---|
385 | if (line >= decoded.length)
|
---|
386 | return OMapping(null, null, null, null);
|
---|
387 | const segments = decoded[line];
|
---|
388 | const index = traceSegmentInternal(segments, cast(map)._decodedMemo, line, column, bias || GREATEST_LOWER_BOUND);
|
---|
389 | if (index === -1)
|
---|
390 | return OMapping(null, null, null, null);
|
---|
391 | const segment = segments[index];
|
---|
392 | if (segment.length === 1)
|
---|
393 | return OMapping(null, null, null, null);
|
---|
394 | const { names, resolvedSources } = map;
|
---|
395 | return OMapping(resolvedSources[segment[SOURCES_INDEX]], segment[SOURCE_LINE] + 1, segment[SOURCE_COLUMN], segment.length === 5 ? names[segment[NAMES_INDEX]] : null);
|
---|
396 | }
|
---|
397 | /**
|
---|
398 | * Finds the generated line/column position of the provided source/line/column source position.
|
---|
399 | */
|
---|
400 | function generatedPositionFor(map, needle) {
|
---|
401 | const { source, line, column, bias } = needle;
|
---|
402 | return generatedPosition(map, source, line, column, bias || GREATEST_LOWER_BOUND, false);
|
---|
403 | }
|
---|
404 | /**
|
---|
405 | * Finds all generated line/column positions of the provided source/line/column source position.
|
---|
406 | */
|
---|
407 | function allGeneratedPositionsFor(map, needle) {
|
---|
408 | const { source, line, column, bias } = needle;
|
---|
409 | // SourceMapConsumer uses LEAST_UPPER_BOUND for some reason, so we follow suit.
|
---|
410 | return generatedPosition(map, source, line, column, bias || LEAST_UPPER_BOUND, true);
|
---|
411 | }
|
---|
412 | /**
|
---|
413 | * Iterates each mapping in generated position order.
|
---|
414 | */
|
---|
415 | function eachMapping(map, cb) {
|
---|
416 | const decoded = decodedMappings(map);
|
---|
417 | const { names, resolvedSources } = map;
|
---|
418 | for (let i = 0; i < decoded.length; i++) {
|
---|
419 | const line = decoded[i];
|
---|
420 | for (let j = 0; j < line.length; j++) {
|
---|
421 | const seg = line[j];
|
---|
422 | const generatedLine = i + 1;
|
---|
423 | const generatedColumn = seg[0];
|
---|
424 | let source = null;
|
---|
425 | let originalLine = null;
|
---|
426 | let originalColumn = null;
|
---|
427 | let name = null;
|
---|
428 | if (seg.length !== 1) {
|
---|
429 | source = resolvedSources[seg[1]];
|
---|
430 | originalLine = seg[2] + 1;
|
---|
431 | originalColumn = seg[3];
|
---|
432 | }
|
---|
433 | if (seg.length === 5)
|
---|
434 | name = names[seg[4]];
|
---|
435 | cb({
|
---|
436 | generatedLine,
|
---|
437 | generatedColumn,
|
---|
438 | source,
|
---|
439 | originalLine,
|
---|
440 | originalColumn,
|
---|
441 | name,
|
---|
442 | });
|
---|
443 | }
|
---|
444 | }
|
---|
445 | }
|
---|
446 | function sourceIndex(map, source) {
|
---|
447 | const { sources, resolvedSources } = map;
|
---|
448 | let index = sources.indexOf(source);
|
---|
449 | if (index === -1)
|
---|
450 | index = resolvedSources.indexOf(source);
|
---|
451 | return index;
|
---|
452 | }
|
---|
453 | /**
|
---|
454 | * Retrieves the source content for a particular source, if its found. Returns null if not.
|
---|
455 | */
|
---|
456 | function sourceContentFor(map, source) {
|
---|
457 | const { sourcesContent } = map;
|
---|
458 | if (sourcesContent == null)
|
---|
459 | return null;
|
---|
460 | const index = sourceIndex(map, source);
|
---|
461 | return index === -1 ? null : sourcesContent[index];
|
---|
462 | }
|
---|
463 | /**
|
---|
464 | * Determines if the source is marked to ignore by the source map.
|
---|
465 | */
|
---|
466 | function isIgnored(map, source) {
|
---|
467 | const { ignoreList } = map;
|
---|
468 | if (ignoreList == null)
|
---|
469 | return false;
|
---|
470 | const index = sourceIndex(map, source);
|
---|
471 | return index === -1 ? false : ignoreList.includes(index);
|
---|
472 | }
|
---|
473 | /**
|
---|
474 | * A helper that skips sorting of the input map's mappings array, which can be expensive for larger
|
---|
475 | * maps.
|
---|
476 | */
|
---|
477 | function presortedDecodedMap(map, mapUrl) {
|
---|
478 | const tracer = new TraceMap(clone(map, []), mapUrl);
|
---|
479 | cast(tracer)._decoded = map.mappings;
|
---|
480 | return tracer;
|
---|
481 | }
|
---|
482 | /**
|
---|
483 | * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
|
---|
484 | * a sourcemap, or to JSON.stringify.
|
---|
485 | */
|
---|
486 | function decodedMap(map) {
|
---|
487 | return clone(map, decodedMappings(map));
|
---|
488 | }
|
---|
489 | /**
|
---|
490 | * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
|
---|
491 | * a sourcemap, or to JSON.stringify.
|
---|
492 | */
|
---|
493 | function encodedMap(map) {
|
---|
494 | return clone(map, encodedMappings(map));
|
---|
495 | }
|
---|
496 | function clone(map, mappings) {
|
---|
497 | return {
|
---|
498 | version: map.version,
|
---|
499 | file: map.file,
|
---|
500 | names: map.names,
|
---|
501 | sourceRoot: map.sourceRoot,
|
---|
502 | sources: map.sources,
|
---|
503 | sourcesContent: map.sourcesContent,
|
---|
504 | mappings,
|
---|
505 | ignoreList: map.ignoreList || map.x_google_ignoreList,
|
---|
506 | };
|
---|
507 | }
|
---|
508 | function OMapping(source, line, column, name) {
|
---|
509 | return { source, line, column, name };
|
---|
510 | }
|
---|
511 | function GMapping(line, column) {
|
---|
512 | return { line, column };
|
---|
513 | }
|
---|
514 | function traceSegmentInternal(segments, memo, line, column, bias) {
|
---|
515 | let index = memoizedBinarySearch(segments, column, memo, line);
|
---|
516 | if (found) {
|
---|
517 | index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
|
---|
518 | }
|
---|
519 | else if (bias === LEAST_UPPER_BOUND)
|
---|
520 | index++;
|
---|
521 | if (index === -1 || index === segments.length)
|
---|
522 | return -1;
|
---|
523 | return index;
|
---|
524 | }
|
---|
525 | function sliceGeneratedPositions(segments, memo, line, column, bias) {
|
---|
526 | let min = traceSegmentInternal(segments, memo, line, column, GREATEST_LOWER_BOUND);
|
---|
527 | // We ignored the bias when tracing the segment so that we're guarnateed to find the first (in
|
---|
528 | // insertion order) segment that matched. Even if we did respect the bias when tracing, we would
|
---|
529 | // still need to call `lowerBound()` to find the first segment, which is slower than just looking
|
---|
530 | // for the GREATEST_LOWER_BOUND to begin with. The only difference that matters for us is when the
|
---|
531 | // binary search didn't match, in which case GREATEST_LOWER_BOUND just needs to increment to
|
---|
532 | // match LEAST_UPPER_BOUND.
|
---|
533 | if (!found && bias === LEAST_UPPER_BOUND)
|
---|
534 | min++;
|
---|
535 | if (min === -1 || min === segments.length)
|
---|
536 | return [];
|
---|
537 | // We may have found the segment that started at an earlier column. If this is the case, then we
|
---|
538 | // need to slice all generated segments that match _that_ column, because all such segments span
|
---|
539 | // to our desired column.
|
---|
540 | const matchedColumn = found ? column : segments[min][COLUMN];
|
---|
541 | // The binary search is not guaranteed to find the lower bound when a match wasn't found.
|
---|
542 | if (!found)
|
---|
543 | min = lowerBound(segments, matchedColumn, min);
|
---|
544 | const max = upperBound(segments, matchedColumn, min);
|
---|
545 | const result = [];
|
---|
546 | for (; min <= max; min++) {
|
---|
547 | const segment = segments[min];
|
---|
548 | result.push(GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]));
|
---|
549 | }
|
---|
550 | return result;
|
---|
551 | }
|
---|
552 | function generatedPosition(map, source, line, column, bias, all) {
|
---|
553 | var _a;
|
---|
554 | line--;
|
---|
555 | if (line < 0)
|
---|
556 | throw new Error(LINE_GTR_ZERO);
|
---|
557 | if (column < 0)
|
---|
558 | throw new Error(COL_GTR_EQ_ZERO);
|
---|
559 | const { sources, resolvedSources } = map;
|
---|
560 | let sourceIndex = sources.indexOf(source);
|
---|
561 | if (sourceIndex === -1)
|
---|
562 | sourceIndex = resolvedSources.indexOf(source);
|
---|
563 | if (sourceIndex === -1)
|
---|
564 | return all ? [] : GMapping(null, null);
|
---|
565 | const generated = ((_a = cast(map))._bySources || (_a._bySources = buildBySources(decodedMappings(map), (cast(map)._bySourceMemos = sources.map(memoizedState)))));
|
---|
566 | const segments = generated[sourceIndex][line];
|
---|
567 | if (segments == null)
|
---|
568 | return all ? [] : GMapping(null, null);
|
---|
569 | const memo = cast(map)._bySourceMemos[sourceIndex];
|
---|
570 | if (all)
|
---|
571 | return sliceGeneratedPositions(segments, memo, line, column, bias);
|
---|
572 | const index = traceSegmentInternal(segments, memo, line, column, bias);
|
---|
573 | if (index === -1)
|
---|
574 | return GMapping(null, null);
|
---|
575 | const segment = segments[index];
|
---|
576 | return GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]);
|
---|
577 | }
|
---|
578 |
|
---|
579 | export { AnyMap, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND, TraceMap, allGeneratedPositionsFor, decodedMap, decodedMappings, eachMapping, encodedMap, encodedMappings, generatedPositionFor, isIgnored, originalPositionFor, presortedDecodedMap, sourceContentFor, traceSegment };
|
---|
580 | //# sourceMappingURL=trace-mapping.mjs.map
|
---|