trace-mapping.umd.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. (function (global, factory, m) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(module, require('@jridgewell/resolve-uri'), require('@jridgewell/sourcemap-codec')) :
  3. typeof define === 'function' && define.amd ? define(['module', '@jridgewell/resolve-uri', '@jridgewell/sourcemap-codec'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(m = { exports: {} }, global.resolveURI, global.sourcemapCodec), global.traceMapping = 'default' in m.exports ? m.exports.default : m.exports);
  5. })(this, (function (module, require_resolveURI, require_sourcemapCodec) {
  6. "use strict";
  7. var __create = Object.create;
  8. var __defProp = Object.defineProperty;
  9. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  10. var __getOwnPropNames = Object.getOwnPropertyNames;
  11. var __getProtoOf = Object.getPrototypeOf;
  12. var __hasOwnProp = Object.prototype.hasOwnProperty;
  13. var __commonJS = (cb, mod) => function __require() {
  14. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  15. };
  16. var __export = (target, all) => {
  17. for (var name in all)
  18. __defProp(target, name, { get: all[name], enumerable: true });
  19. };
  20. var __copyProps = (to, from, except, desc) => {
  21. if (from && typeof from === "object" || typeof from === "function") {
  22. for (let key of __getOwnPropNames(from))
  23. if (!__hasOwnProp.call(to, key) && key !== except)
  24. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  25. }
  26. return to;
  27. };
  28. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  29. // If the importer is in node compatibility mode or this is not an ESM
  30. // file that has been converted to a CommonJS file using a Babel-
  31. // compatible transform (i.e. "__esModule" has not been set), then set
  32. // "default" to the CommonJS "module.exports" for node compatibility.
  33. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  34. mod
  35. ));
  36. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  37. // umd:@jridgewell/sourcemap-codec
  38. var require_sourcemap_codec = __commonJS({
  39. "umd:@jridgewell/sourcemap-codec"(exports, module2) {
  40. module2.exports = require_sourcemapCodec;
  41. }
  42. });
  43. // umd:@jridgewell/resolve-uri
  44. var require_resolve_uri = __commonJS({
  45. "umd:@jridgewell/resolve-uri"(exports, module2) {
  46. module2.exports = require_resolveURI;
  47. }
  48. });
  49. // src/trace-mapping.ts
  50. var trace_mapping_exports = {};
  51. __export(trace_mapping_exports, {
  52. AnyMap: () => FlattenMap,
  53. FlattenMap: () => FlattenMap,
  54. GREATEST_LOWER_BOUND: () => GREATEST_LOWER_BOUND,
  55. LEAST_UPPER_BOUND: () => LEAST_UPPER_BOUND,
  56. TraceMap: () => TraceMap,
  57. allGeneratedPositionsFor: () => allGeneratedPositionsFor,
  58. decodedMap: () => decodedMap,
  59. decodedMappings: () => decodedMappings,
  60. eachMapping: () => eachMapping,
  61. encodedMap: () => encodedMap,
  62. encodedMappings: () => encodedMappings,
  63. generatedPositionFor: () => generatedPositionFor,
  64. isIgnored: () => isIgnored,
  65. originalPositionFor: () => originalPositionFor,
  66. presortedDecodedMap: () => presortedDecodedMap,
  67. sourceContentFor: () => sourceContentFor,
  68. traceSegment: () => traceSegment
  69. });
  70. module.exports = __toCommonJS(trace_mapping_exports);
  71. var import_sourcemap_codec = __toESM(require_sourcemap_codec());
  72. // src/resolve.ts
  73. var import_resolve_uri = __toESM(require_resolve_uri());
  74. // src/strip-filename.ts
  75. function stripFilename(path) {
  76. if (!path) return "";
  77. const index = path.lastIndexOf("/");
  78. return path.slice(0, index + 1);
  79. }
  80. // src/resolve.ts
  81. function resolver(mapUrl, sourceRoot) {
  82. const from = stripFilename(mapUrl);
  83. const prefix = sourceRoot ? sourceRoot + "/" : "";
  84. return (source) => (0, import_resolve_uri.default)(prefix + (source || ""), from);
  85. }
  86. // src/sourcemap-segment.ts
  87. var COLUMN = 0;
  88. var SOURCES_INDEX = 1;
  89. var SOURCE_LINE = 2;
  90. var SOURCE_COLUMN = 3;
  91. var NAMES_INDEX = 4;
  92. var REV_GENERATED_LINE = 1;
  93. var REV_GENERATED_COLUMN = 2;
  94. // src/sort.ts
  95. function maybeSort(mappings, owned) {
  96. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  97. if (unsortedIndex === mappings.length) return mappings;
  98. if (!owned) mappings = mappings.slice();
  99. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  100. mappings[i] = sortSegments(mappings[i], owned);
  101. }
  102. return mappings;
  103. }
  104. function nextUnsortedSegmentLine(mappings, start) {
  105. for (let i = start; i < mappings.length; i++) {
  106. if (!isSorted(mappings[i])) return i;
  107. }
  108. return mappings.length;
  109. }
  110. function isSorted(line) {
  111. for (let j = 1; j < line.length; j++) {
  112. if (line[j][COLUMN] < line[j - 1][COLUMN]) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. function sortSegments(line, owned) {
  119. if (!owned) line = line.slice();
  120. return line.sort(sortComparator);
  121. }
  122. function sortComparator(a, b) {
  123. return a[COLUMN] - b[COLUMN];
  124. }
  125. // src/binary-search.ts
  126. var found = false;
  127. function binarySearch(haystack, needle, low, high) {
  128. while (low <= high) {
  129. const mid = low + (high - low >> 1);
  130. const cmp = haystack[mid][COLUMN] - needle;
  131. if (cmp === 0) {
  132. found = true;
  133. return mid;
  134. }
  135. if (cmp < 0) {
  136. low = mid + 1;
  137. } else {
  138. high = mid - 1;
  139. }
  140. }
  141. found = false;
  142. return low - 1;
  143. }
  144. function upperBound(haystack, needle, index) {
  145. for (let i = index + 1; i < haystack.length; index = i++) {
  146. if (haystack[i][COLUMN] !== needle) break;
  147. }
  148. return index;
  149. }
  150. function lowerBound(haystack, needle, index) {
  151. for (let i = index - 1; i >= 0; index = i--) {
  152. if (haystack[i][COLUMN] !== needle) break;
  153. }
  154. return index;
  155. }
  156. function memoizedState() {
  157. return {
  158. lastKey: -1,
  159. lastNeedle: -1,
  160. lastIndex: -1
  161. };
  162. }
  163. function memoizedBinarySearch(haystack, needle, state, key) {
  164. const { lastKey, lastNeedle, lastIndex } = state;
  165. let low = 0;
  166. let high = haystack.length - 1;
  167. if (key === lastKey) {
  168. if (needle === lastNeedle) {
  169. found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle;
  170. return lastIndex;
  171. }
  172. if (needle >= lastNeedle) {
  173. low = lastIndex === -1 ? 0 : lastIndex;
  174. } else {
  175. high = lastIndex;
  176. }
  177. }
  178. state.lastKey = key;
  179. state.lastNeedle = needle;
  180. return state.lastIndex = binarySearch(haystack, needle, low, high);
  181. }
  182. // src/by-source.ts
  183. function buildBySources(decoded, memos) {
  184. const sources = memos.map(buildNullArray);
  185. for (let i = 0; i < decoded.length; i++) {
  186. const line = decoded[i];
  187. for (let j = 0; j < line.length; j++) {
  188. const seg = line[j];
  189. if (seg.length === 1) continue;
  190. const sourceIndex2 = seg[SOURCES_INDEX];
  191. const sourceLine = seg[SOURCE_LINE];
  192. const sourceColumn = seg[SOURCE_COLUMN];
  193. const originalSource = sources[sourceIndex2];
  194. const originalLine = originalSource[sourceLine] || (originalSource[sourceLine] = []);
  195. const memo = memos[sourceIndex2];
  196. let index = upperBound(
  197. originalLine,
  198. sourceColumn,
  199. memoizedBinarySearch(originalLine, sourceColumn, memo, sourceLine)
  200. );
  201. memo.lastIndex = ++index;
  202. insert(originalLine, index, [sourceColumn, i, seg[COLUMN]]);
  203. }
  204. }
  205. return sources;
  206. }
  207. function insert(array, index, value) {
  208. for (let i = array.length; i > index; i--) {
  209. array[i] = array[i - 1];
  210. }
  211. array[index] = value;
  212. }
  213. function buildNullArray() {
  214. return { __proto__: null };
  215. }
  216. // src/types.ts
  217. function parse(map) {
  218. return typeof map === "string" ? JSON.parse(map) : map;
  219. }
  220. // src/flatten-map.ts
  221. var FlattenMap = function(map, mapUrl) {
  222. const parsed = parse(map);
  223. if (!("sections" in parsed)) {
  224. return new TraceMap(parsed, mapUrl);
  225. }
  226. const mappings = [];
  227. const sources = [];
  228. const sourcesContent = [];
  229. const names = [];
  230. const ignoreList = [];
  231. recurse(
  232. parsed,
  233. mapUrl,
  234. mappings,
  235. sources,
  236. sourcesContent,
  237. names,
  238. ignoreList,
  239. 0,
  240. 0,
  241. Infinity,
  242. Infinity
  243. );
  244. const joined = {
  245. version: 3,
  246. file: parsed.file,
  247. names,
  248. sources,
  249. sourcesContent,
  250. mappings,
  251. ignoreList
  252. };
  253. return presortedDecodedMap(joined);
  254. };
  255. function recurse(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
  256. const { sections } = input;
  257. for (let i = 0; i < sections.length; i++) {
  258. const { map, offset } = sections[i];
  259. let sl = stopLine;
  260. let sc = stopColumn;
  261. if (i + 1 < sections.length) {
  262. const nextOffset = sections[i + 1].offset;
  263. sl = Math.min(stopLine, lineOffset + nextOffset.line);
  264. if (sl === stopLine) {
  265. sc = Math.min(stopColumn, columnOffset + nextOffset.column);
  266. } else if (sl < stopLine) {
  267. sc = columnOffset + nextOffset.column;
  268. }
  269. }
  270. addSection(
  271. map,
  272. mapUrl,
  273. mappings,
  274. sources,
  275. sourcesContent,
  276. names,
  277. ignoreList,
  278. lineOffset + offset.line,
  279. columnOffset + offset.column,
  280. sl,
  281. sc
  282. );
  283. }
  284. }
  285. function addSection(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
  286. const parsed = parse(input);
  287. if ("sections" in parsed) return recurse(...arguments);
  288. const map = new TraceMap(parsed, mapUrl);
  289. const sourcesOffset = sources.length;
  290. const namesOffset = names.length;
  291. const decoded = decodedMappings(map);
  292. const { resolvedSources, sourcesContent: contents, ignoreList: ignores } = map;
  293. append(sources, resolvedSources);
  294. append(names, map.names);
  295. if (contents) append(sourcesContent, contents);
  296. else for (let i = 0; i < resolvedSources.length; i++) sourcesContent.push(null);
  297. if (ignores) for (let i = 0; i < ignores.length; i++) ignoreList.push(ignores[i] + sourcesOffset);
  298. for (let i = 0; i < decoded.length; i++) {
  299. const lineI = lineOffset + i;
  300. if (lineI > stopLine) return;
  301. const out = getLine(mappings, lineI);
  302. const cOffset = i === 0 ? columnOffset : 0;
  303. const line = decoded[i];
  304. for (let j = 0; j < line.length; j++) {
  305. const seg = line[j];
  306. const column = cOffset + seg[COLUMN];
  307. if (lineI === stopLine && column >= stopColumn) return;
  308. if (seg.length === 1) {
  309. out.push([column]);
  310. continue;
  311. }
  312. const sourcesIndex = sourcesOffset + seg[SOURCES_INDEX];
  313. const sourceLine = seg[SOURCE_LINE];
  314. const sourceColumn = seg[SOURCE_COLUMN];
  315. out.push(
  316. seg.length === 4 ? [column, sourcesIndex, sourceLine, sourceColumn] : [column, sourcesIndex, sourceLine, sourceColumn, namesOffset + seg[NAMES_INDEX]]
  317. );
  318. }
  319. }
  320. }
  321. function append(arr, other) {
  322. for (let i = 0; i < other.length; i++) arr.push(other[i]);
  323. }
  324. function getLine(arr, index) {
  325. for (let i = arr.length; i <= index; i++) arr[i] = [];
  326. return arr[index];
  327. }
  328. // src/trace-mapping.ts
  329. var LINE_GTR_ZERO = "`line` must be greater than 0 (lines start at line 1)";
  330. var COL_GTR_EQ_ZERO = "`column` must be greater than or equal to 0 (columns start at column 0)";
  331. var LEAST_UPPER_BOUND = -1;
  332. var GREATEST_LOWER_BOUND = 1;
  333. var TraceMap = class {
  334. constructor(map, mapUrl) {
  335. const isString = typeof map === "string";
  336. if (!isString && map._decodedMemo) return map;
  337. const parsed = parse(map);
  338. const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
  339. this.version = version;
  340. this.file = file;
  341. this.names = names || [];
  342. this.sourceRoot = sourceRoot;
  343. this.sources = sources;
  344. this.sourcesContent = sourcesContent;
  345. this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || void 0;
  346. const resolve = resolver(mapUrl, sourceRoot);
  347. this.resolvedSources = sources.map(resolve);
  348. const { mappings } = parsed;
  349. if (typeof mappings === "string") {
  350. this._encoded = mappings;
  351. this._decoded = void 0;
  352. } else if (Array.isArray(mappings)) {
  353. this._encoded = void 0;
  354. this._decoded = maybeSort(mappings, isString);
  355. } else if (parsed.sections) {
  356. throw new Error(`TraceMap passed sectioned source map, please use FlattenMap export instead`);
  357. } else {
  358. throw new Error(`invalid source map: ${JSON.stringify(parsed)}`);
  359. }
  360. this._decodedMemo = memoizedState();
  361. this._bySources = void 0;
  362. this._bySourceMemos = void 0;
  363. }
  364. };
  365. function cast(map) {
  366. return map;
  367. }
  368. function encodedMappings(map) {
  369. var _a, _b;
  370. return (_b = (_a = cast(map))._encoded) != null ? _b : _a._encoded = (0, import_sourcemap_codec.encode)(cast(map)._decoded);
  371. }
  372. function decodedMappings(map) {
  373. var _a;
  374. return (_a = cast(map))._decoded || (_a._decoded = (0, import_sourcemap_codec.decode)(cast(map)._encoded));
  375. }
  376. function traceSegment(map, line, column) {
  377. const decoded = decodedMappings(map);
  378. if (line >= decoded.length) return null;
  379. const segments = decoded[line];
  380. const index = traceSegmentInternal(
  381. segments,
  382. cast(map)._decodedMemo,
  383. line,
  384. column,
  385. GREATEST_LOWER_BOUND
  386. );
  387. return index === -1 ? null : segments[index];
  388. }
  389. function originalPositionFor(map, needle) {
  390. let { line, column, bias } = needle;
  391. line--;
  392. if (line < 0) throw new Error(LINE_GTR_ZERO);
  393. if (column < 0) throw new Error(COL_GTR_EQ_ZERO);
  394. const decoded = decodedMappings(map);
  395. if (line >= decoded.length) return OMapping(null, null, null, null);
  396. const segments = decoded[line];
  397. const index = traceSegmentInternal(
  398. segments,
  399. cast(map)._decodedMemo,
  400. line,
  401. column,
  402. bias || GREATEST_LOWER_BOUND
  403. );
  404. if (index === -1) return OMapping(null, null, null, null);
  405. const segment = segments[index];
  406. if (segment.length === 1) return OMapping(null, null, null, null);
  407. const { names, resolvedSources } = map;
  408. return OMapping(
  409. resolvedSources[segment[SOURCES_INDEX]],
  410. segment[SOURCE_LINE] + 1,
  411. segment[SOURCE_COLUMN],
  412. segment.length === 5 ? names[segment[NAMES_INDEX]] : null
  413. );
  414. }
  415. function generatedPositionFor(map, needle) {
  416. const { source, line, column, bias } = needle;
  417. return generatedPosition(map, source, line, column, bias || GREATEST_LOWER_BOUND, false);
  418. }
  419. function allGeneratedPositionsFor(map, needle) {
  420. const { source, line, column, bias } = needle;
  421. return generatedPosition(map, source, line, column, bias || LEAST_UPPER_BOUND, true);
  422. }
  423. function eachMapping(map, cb) {
  424. const decoded = decodedMappings(map);
  425. const { names, resolvedSources } = map;
  426. for (let i = 0; i < decoded.length; i++) {
  427. const line = decoded[i];
  428. for (let j = 0; j < line.length; j++) {
  429. const seg = line[j];
  430. const generatedLine = i + 1;
  431. const generatedColumn = seg[0];
  432. let source = null;
  433. let originalLine = null;
  434. let originalColumn = null;
  435. let name = null;
  436. if (seg.length !== 1) {
  437. source = resolvedSources[seg[1]];
  438. originalLine = seg[2] + 1;
  439. originalColumn = seg[3];
  440. }
  441. if (seg.length === 5) name = names[seg[4]];
  442. cb({
  443. generatedLine,
  444. generatedColumn,
  445. source,
  446. originalLine,
  447. originalColumn,
  448. name
  449. });
  450. }
  451. }
  452. }
  453. function sourceIndex(map, source) {
  454. const { sources, resolvedSources } = map;
  455. let index = sources.indexOf(source);
  456. if (index === -1) index = resolvedSources.indexOf(source);
  457. return index;
  458. }
  459. function sourceContentFor(map, source) {
  460. const { sourcesContent } = map;
  461. if (sourcesContent == null) return null;
  462. const index = sourceIndex(map, source);
  463. return index === -1 ? null : sourcesContent[index];
  464. }
  465. function isIgnored(map, source) {
  466. const { ignoreList } = map;
  467. if (ignoreList == null) return false;
  468. const index = sourceIndex(map, source);
  469. return index === -1 ? false : ignoreList.includes(index);
  470. }
  471. function presortedDecodedMap(map, mapUrl) {
  472. const tracer = new TraceMap(clone(map, []), mapUrl);
  473. cast(tracer)._decoded = map.mappings;
  474. return tracer;
  475. }
  476. function decodedMap(map) {
  477. return clone(map, decodedMappings(map));
  478. }
  479. function encodedMap(map) {
  480. return clone(map, encodedMappings(map));
  481. }
  482. function clone(map, mappings) {
  483. return {
  484. version: map.version,
  485. file: map.file,
  486. names: map.names,
  487. sourceRoot: map.sourceRoot,
  488. sources: map.sources,
  489. sourcesContent: map.sourcesContent,
  490. mappings,
  491. ignoreList: map.ignoreList || map.x_google_ignoreList
  492. };
  493. }
  494. function OMapping(source, line, column, name) {
  495. return { source, line, column, name };
  496. }
  497. function GMapping(line, column) {
  498. return { line, column };
  499. }
  500. function traceSegmentInternal(segments, memo, line, column, bias) {
  501. let index = memoizedBinarySearch(segments, column, memo, line);
  502. if (found) {
  503. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  504. } else if (bias === LEAST_UPPER_BOUND) index++;
  505. if (index === -1 || index === segments.length) return -1;
  506. return index;
  507. }
  508. function sliceGeneratedPositions(segments, memo, line, column, bias) {
  509. let min = traceSegmentInternal(segments, memo, line, column, GREATEST_LOWER_BOUND);
  510. if (!found && bias === LEAST_UPPER_BOUND) min++;
  511. if (min === -1 || min === segments.length) return [];
  512. const matchedColumn = found ? column : segments[min][COLUMN];
  513. if (!found) min = lowerBound(segments, matchedColumn, min);
  514. const max = upperBound(segments, matchedColumn, min);
  515. const result = [];
  516. for (; min <= max; min++) {
  517. const segment = segments[min];
  518. result.push(GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]));
  519. }
  520. return result;
  521. }
  522. function generatedPosition(map, source, line, column, bias, all) {
  523. var _a;
  524. line--;
  525. if (line < 0) throw new Error(LINE_GTR_ZERO);
  526. if (column < 0) throw new Error(COL_GTR_EQ_ZERO);
  527. const { sources, resolvedSources } = map;
  528. let sourceIndex2 = sources.indexOf(source);
  529. if (sourceIndex2 === -1) sourceIndex2 = resolvedSources.indexOf(source);
  530. if (sourceIndex2 === -1) return all ? [] : GMapping(null, null);
  531. const generated = (_a = cast(map))._bySources || (_a._bySources = buildBySources(
  532. decodedMappings(map),
  533. cast(map)._bySourceMemos = sources.map(memoizedState)
  534. ));
  535. const segments = generated[sourceIndex2][line];
  536. if (segments == null) return all ? [] : GMapping(null, null);
  537. const memo = cast(map)._bySourceMemos[sourceIndex2];
  538. if (all) return sliceGeneratedPositions(segments, memo, line, column, bias);
  539. const index = traceSegmentInternal(segments, memo, line, column, bias);
  540. if (index === -1) return GMapping(null, null);
  541. const segment = segments[index];
  542. return GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]);
  543. }
  544. }));
  545. //# sourceMappingURL=trace-mapping.umd.js.map