source: imaps-frontend/node_modules/@jridgewell/trace-mapping/dist/trace-mapping.umd.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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