trace-mapping.umd.js 18 KB

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