source-map.umd.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sourceMap = {}));
  5. })(this, (function (exports) { 'use strict';
  6. const comma = ','.charCodeAt(0);
  7. const semicolon = ';'.charCodeAt(0);
  8. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  9. const intToChar = new Uint8Array(64); // 64 possible chars.
  10. const charToInt = new Uint8Array(128); // z is 122 in ASCII
  11. for (let i = 0; i < chars.length; i++) {
  12. const c = chars.charCodeAt(i);
  13. intToChar[i] = c;
  14. charToInt[c] = i;
  15. }
  16. // Provide a fallback for older environments.
  17. const td = typeof TextDecoder !== 'undefined'
  18. ? /* #__PURE__ */ new TextDecoder()
  19. : typeof Buffer !== 'undefined'
  20. ? {
  21. decode(buf) {
  22. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  23. return out.toString();
  24. },
  25. }
  26. : {
  27. decode(buf) {
  28. let out = '';
  29. for (let i = 0; i < buf.length; i++) {
  30. out += String.fromCharCode(buf[i]);
  31. }
  32. return out;
  33. },
  34. };
  35. function decode(mappings) {
  36. const state = new Int32Array(5);
  37. const decoded = [];
  38. let index = 0;
  39. do {
  40. const semi = indexOf(mappings, index);
  41. const line = [];
  42. let sorted = true;
  43. let lastCol = 0;
  44. state[0] = 0;
  45. for (let i = index; i < semi; i++) {
  46. let seg;
  47. i = decodeInteger(mappings, i, state, 0); // genColumn
  48. const col = state[0];
  49. if (col < lastCol)
  50. sorted = false;
  51. lastCol = col;
  52. if (hasMoreVlq(mappings, i, semi)) {
  53. i = decodeInteger(mappings, i, state, 1); // sourcesIndex
  54. i = decodeInteger(mappings, i, state, 2); // sourceLine
  55. i = decodeInteger(mappings, i, state, 3); // sourceColumn
  56. if (hasMoreVlq(mappings, i, semi)) {
  57. i = decodeInteger(mappings, i, state, 4); // namesIndex
  58. seg = [col, state[1], state[2], state[3], state[4]];
  59. }
  60. else {
  61. seg = [col, state[1], state[2], state[3]];
  62. }
  63. }
  64. else {
  65. seg = [col];
  66. }
  67. line.push(seg);
  68. }
  69. if (!sorted)
  70. sort(line);
  71. decoded.push(line);
  72. index = semi + 1;
  73. } while (index <= mappings.length);
  74. return decoded;
  75. }
  76. function indexOf(mappings, index) {
  77. const idx = mappings.indexOf(';', index);
  78. return idx === -1 ? mappings.length : idx;
  79. }
  80. function decodeInteger(mappings, pos, state, j) {
  81. let value = 0;
  82. let shift = 0;
  83. let integer = 0;
  84. do {
  85. const c = mappings.charCodeAt(pos++);
  86. integer = charToInt[c];
  87. value |= (integer & 31) << shift;
  88. shift += 5;
  89. } while (integer & 32);
  90. const shouldNegate = value & 1;
  91. value >>>= 1;
  92. if (shouldNegate) {
  93. value = -0x80000000 | -value;
  94. }
  95. state[j] += value;
  96. return pos;
  97. }
  98. function hasMoreVlq(mappings, i, length) {
  99. if (i >= length)
  100. return false;
  101. return mappings.charCodeAt(i) !== comma;
  102. }
  103. function sort(line) {
  104. line.sort(sortComparator$1);
  105. }
  106. function sortComparator$1(a, b) {
  107. return a[0] - b[0];
  108. }
  109. function encode(decoded) {
  110. const state = new Int32Array(5);
  111. const bufLength = 1024 * 16;
  112. const subLength = bufLength - 36;
  113. const buf = new Uint8Array(bufLength);
  114. const sub = buf.subarray(0, subLength);
  115. let pos = 0;
  116. let out = '';
  117. for (let i = 0; i < decoded.length; i++) {
  118. const line = decoded[i];
  119. if (i > 0) {
  120. if (pos === bufLength) {
  121. out += td.decode(buf);
  122. pos = 0;
  123. }
  124. buf[pos++] = semicolon;
  125. }
  126. if (line.length === 0)
  127. continue;
  128. state[0] = 0;
  129. for (let j = 0; j < line.length; j++) {
  130. const segment = line[j];
  131. // We can push up to 5 ints, each int can take at most 7 chars, and we
  132. // may push a comma.
  133. if (pos > subLength) {
  134. out += td.decode(sub);
  135. buf.copyWithin(0, subLength, pos);
  136. pos -= subLength;
  137. }
  138. if (j > 0)
  139. buf[pos++] = comma;
  140. pos = encodeInteger(buf, pos, state, segment, 0); // genColumn
  141. if (segment.length === 1)
  142. continue;
  143. pos = encodeInteger(buf, pos, state, segment, 1); // sourcesIndex
  144. pos = encodeInteger(buf, pos, state, segment, 2); // sourceLine
  145. pos = encodeInteger(buf, pos, state, segment, 3); // sourceColumn
  146. if (segment.length === 4)
  147. continue;
  148. pos = encodeInteger(buf, pos, state, segment, 4); // namesIndex
  149. }
  150. }
  151. return out + td.decode(buf.subarray(0, pos));
  152. }
  153. function encodeInteger(buf, pos, state, segment, j) {
  154. const next = segment[j];
  155. let num = next - state[j];
  156. state[j] = next;
  157. num = num < 0 ? (-num << 1) | 1 : num << 1;
  158. do {
  159. let clamped = num & 0b011111;
  160. num >>>= 5;
  161. if (num > 0)
  162. clamped |= 0b100000;
  163. buf[pos++] = intToChar[clamped];
  164. } while (num > 0);
  165. return pos;
  166. }
  167. // Matches the scheme of a URL, eg "http://"
  168. const schemeRegex = /^[\w+.-]+:\/\//;
  169. /**
  170. * Matches the parts of a URL:
  171. * 1. Scheme, including ":", guaranteed.
  172. * 2. User/password, including "@", optional.
  173. * 3. Host, guaranteed.
  174. * 4. Port, including ":", optional.
  175. * 5. Path, including "/", optional.
  176. * 6. Query, including "?", optional.
  177. * 7. Hash, including "#", optional.
  178. */
  179. const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/;
  180. /**
  181. * File URLs are weird. They dont' need the regular `//` in the scheme, they may or may not start
  182. * with a leading `/`, they can have a domain (but only if they don't start with a Windows drive).
  183. *
  184. * 1. Host, optional.
  185. * 2. Path, which may include "/", guaranteed.
  186. * 3. Query, including "?", optional.
  187. * 4. Hash, including "#", optional.
  188. */
  189. const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i;
  190. function isAbsoluteUrl(input) {
  191. return schemeRegex.test(input);
  192. }
  193. function isSchemeRelativeUrl(input) {
  194. return input.startsWith('//');
  195. }
  196. function isAbsolutePath(input) {
  197. return input.startsWith('/');
  198. }
  199. function isFileUrl(input) {
  200. return input.startsWith('file:');
  201. }
  202. function isRelative(input) {
  203. return /^[.?#]/.test(input);
  204. }
  205. function parseAbsoluteUrl(input) {
  206. const match = urlRegex.exec(input);
  207. return makeUrl(match[1], match[2] || '', match[3], match[4] || '', match[5] || '/', match[6] || '', match[7] || '');
  208. }
  209. function parseFileUrl(input) {
  210. const match = fileRegex.exec(input);
  211. const path = match[2];
  212. return makeUrl('file:', '', match[1] || '', '', isAbsolutePath(path) ? path : '/' + path, match[3] || '', match[4] || '');
  213. }
  214. function makeUrl(scheme, user, host, port, path, query, hash) {
  215. return {
  216. scheme,
  217. user,
  218. host,
  219. port,
  220. path,
  221. query,
  222. hash,
  223. type: 7 /* Absolute */,
  224. };
  225. }
  226. function parseUrl(input) {
  227. if (isSchemeRelativeUrl(input)) {
  228. const url = parseAbsoluteUrl('http:' + input);
  229. url.scheme = '';
  230. url.type = 6 /* SchemeRelative */;
  231. return url;
  232. }
  233. if (isAbsolutePath(input)) {
  234. const url = parseAbsoluteUrl('http://foo.com' + input);
  235. url.scheme = '';
  236. url.host = '';
  237. url.type = 5 /* AbsolutePath */;
  238. return url;
  239. }
  240. if (isFileUrl(input))
  241. return parseFileUrl(input);
  242. if (isAbsoluteUrl(input))
  243. return parseAbsoluteUrl(input);
  244. const url = parseAbsoluteUrl('http://foo.com/' + input);
  245. url.scheme = '';
  246. url.host = '';
  247. url.type = input
  248. ? input.startsWith('?')
  249. ? 3 /* Query */
  250. : input.startsWith('#')
  251. ? 2 /* Hash */
  252. : 4 /* RelativePath */
  253. : 1 /* Empty */;
  254. return url;
  255. }
  256. function stripPathFilename(path) {
  257. // If a path ends with a parent directory "..", then it's a relative path with excess parent
  258. // paths. It's not a file, so we can't strip it.
  259. if (path.endsWith('/..'))
  260. return path;
  261. const index = path.lastIndexOf('/');
  262. return path.slice(0, index + 1);
  263. }
  264. function mergePaths(url, base) {
  265. normalizePath(base, base.type);
  266. // If the path is just a "/", then it was an empty path to begin with (remember, we're a relative
  267. // path).
  268. if (url.path === '/') {
  269. url.path = base.path;
  270. }
  271. else {
  272. // Resolution happens relative to the base path's directory, not the file.
  273. url.path = stripPathFilename(base.path) + url.path;
  274. }
  275. }
  276. /**
  277. * The path can have empty directories "//", unneeded parents "foo/..", or current directory
  278. * "foo/.". We need to normalize to a standard representation.
  279. */
  280. function normalizePath(url, type) {
  281. const rel = type <= 4 /* RelativePath */;
  282. const pieces = url.path.split('/');
  283. // We need to preserve the first piece always, so that we output a leading slash. The item at
  284. // pieces[0] is an empty string.
  285. let pointer = 1;
  286. // Positive is the number of real directories we've output, used for popping a parent directory.
  287. // Eg, "foo/bar/.." will have a positive 2, and we can decrement to be left with just "foo".
  288. let positive = 0;
  289. // We need to keep a trailing slash if we encounter an empty directory (eg, splitting "foo/" will
  290. // generate `["foo", ""]` pieces). And, if we pop a parent directory. But once we encounter a
  291. // real directory, we won't need to append, unless the other conditions happen again.
  292. let addTrailingSlash = false;
  293. for (let i = 1; i < pieces.length; i++) {
  294. const piece = pieces[i];
  295. // An empty directory, could be a trailing slash, or just a double "//" in the path.
  296. if (!piece) {
  297. addTrailingSlash = true;
  298. continue;
  299. }
  300. // If we encounter a real directory, then we don't need to append anymore.
  301. addTrailingSlash = false;
  302. // A current directory, which we can always drop.
  303. if (piece === '.')
  304. continue;
  305. // A parent directory, we need to see if there are any real directories we can pop. Else, we
  306. // have an excess of parents, and we'll need to keep the "..".
  307. if (piece === '..') {
  308. if (positive) {
  309. addTrailingSlash = true;
  310. positive--;
  311. pointer--;
  312. }
  313. else if (rel) {
  314. // If we're in a relativePath, then we need to keep the excess parents. Else, in an absolute
  315. // URL, protocol relative URL, or an absolute path, we don't need to keep excess.
  316. pieces[pointer++] = piece;
  317. }
  318. continue;
  319. }
  320. // We've encountered a real directory. Move it to the next insertion pointer, which accounts for
  321. // any popped or dropped directories.
  322. pieces[pointer++] = piece;
  323. positive++;
  324. }
  325. let path = '';
  326. for (let i = 1; i < pointer; i++) {
  327. path += '/' + pieces[i];
  328. }
  329. if (!path || (addTrailingSlash && !path.endsWith('/..'))) {
  330. path += '/';
  331. }
  332. url.path = path;
  333. }
  334. /**
  335. * Attempts to resolve `input` URL/path relative to `base`.
  336. */
  337. function resolve$1(input, base) {
  338. if (!input && !base)
  339. return '';
  340. const url = parseUrl(input);
  341. let inputType = url.type;
  342. if (base && inputType !== 7 /* Absolute */) {
  343. const baseUrl = parseUrl(base);
  344. const baseType = baseUrl.type;
  345. switch (inputType) {
  346. case 1 /* Empty */:
  347. url.hash = baseUrl.hash;
  348. // fall through
  349. case 2 /* Hash */:
  350. url.query = baseUrl.query;
  351. // fall through
  352. case 3 /* Query */:
  353. case 4 /* RelativePath */:
  354. mergePaths(url, baseUrl);
  355. // fall through
  356. case 5 /* AbsolutePath */:
  357. // The host, user, and port are joined, you can't copy one without the others.
  358. url.user = baseUrl.user;
  359. url.host = baseUrl.host;
  360. url.port = baseUrl.port;
  361. // fall through
  362. case 6 /* SchemeRelative */:
  363. // The input doesn't have a schema at least, so we need to copy at least that over.
  364. url.scheme = baseUrl.scheme;
  365. }
  366. if (baseType > inputType)
  367. inputType = baseType;
  368. }
  369. normalizePath(url, inputType);
  370. const queryHash = url.query + url.hash;
  371. switch (inputType) {
  372. // This is impossible, because of the empty checks at the start of the function.
  373. // case UrlType.Empty:
  374. case 2 /* Hash */:
  375. case 3 /* Query */:
  376. return queryHash;
  377. case 4 /* RelativePath */: {
  378. // The first char is always a "/", and we need it to be relative.
  379. const path = url.path.slice(1);
  380. if (!path)
  381. return queryHash || '.';
  382. if (isRelative(base || input) && !isRelative(path)) {
  383. // If base started with a leading ".", or there is no base and input started with a ".",
  384. // then we need to ensure that the relative path starts with a ".". We don't know if
  385. // relative starts with a "..", though, so check before prepending.
  386. return './' + path + queryHash;
  387. }
  388. return path + queryHash;
  389. }
  390. case 5 /* AbsolutePath */:
  391. return url.path + queryHash;
  392. default:
  393. return url.scheme + '//' + url.user + url.host + url.port + url.path + queryHash;
  394. }
  395. }
  396. function resolve(input, base) {
  397. // The base is always treated as a directory, if it's not empty.
  398. // https://github.com/mozilla/source-map/blob/8cb3ee57/lib/util.js#L327
  399. // https://github.com/chromium/chromium/blob/da4adbb3/third_party/blink/renderer/devtools/front_end/sdk/SourceMap.js#L400-L401
  400. if (base && !base.endsWith('/'))
  401. base += '/';
  402. return resolve$1(input, base);
  403. }
  404. /**
  405. * Removes everything after the last "/", but leaves the slash.
  406. */
  407. function stripFilename(path) {
  408. if (!path)
  409. return '';
  410. const index = path.lastIndexOf('/');
  411. return path.slice(0, index + 1);
  412. }
  413. const COLUMN$1 = 0;
  414. const SOURCES_INDEX$1 = 1;
  415. const SOURCE_LINE$1 = 2;
  416. const SOURCE_COLUMN$1 = 3;
  417. const NAMES_INDEX$1 = 4;
  418. const REV_GENERATED_LINE = 1;
  419. const REV_GENERATED_COLUMN = 2;
  420. function maybeSort(mappings, owned) {
  421. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  422. if (unsortedIndex === mappings.length)
  423. return mappings;
  424. // If we own the array (meaning we parsed it from JSON), then we're free to directly mutate it. If
  425. // not, we do not want to modify the consumer's input array.
  426. if (!owned)
  427. mappings = mappings.slice();
  428. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  429. mappings[i] = sortSegments(mappings[i], owned);
  430. }
  431. return mappings;
  432. }
  433. function nextUnsortedSegmentLine(mappings, start) {
  434. for (let i = start; i < mappings.length; i++) {
  435. if (!isSorted(mappings[i]))
  436. return i;
  437. }
  438. return mappings.length;
  439. }
  440. function isSorted(line) {
  441. for (let j = 1; j < line.length; j++) {
  442. if (line[j][COLUMN$1] < line[j - 1][COLUMN$1]) {
  443. return false;
  444. }
  445. }
  446. return true;
  447. }
  448. function sortSegments(line, owned) {
  449. if (!owned)
  450. line = line.slice();
  451. return line.sort(sortComparator);
  452. }
  453. function sortComparator(a, b) {
  454. return a[COLUMN$1] - b[COLUMN$1];
  455. }
  456. let found = false;
  457. /**
  458. * A binary search implementation that returns the index if a match is found.
  459. * If no match is found, then the left-index (the index associated with the item that comes just
  460. * before the desired index) is returned. To maintain proper sort order, a splice would happen at
  461. * the next index:
  462. *
  463. * ```js
  464. * const array = [1, 3];
  465. * const needle = 2;
  466. * const index = binarySearch(array, needle, (item, needle) => item - needle);
  467. *
  468. * assert.equal(index, 0);
  469. * array.splice(index + 1, 0, needle);
  470. * assert.deepEqual(array, [1, 2, 3]);
  471. * ```
  472. */
  473. function binarySearch(haystack, needle, low, high) {
  474. while (low <= high) {
  475. const mid = low + ((high - low) >> 1);
  476. const cmp = haystack[mid][COLUMN$1] - needle;
  477. if (cmp === 0) {
  478. found = true;
  479. return mid;
  480. }
  481. if (cmp < 0) {
  482. low = mid + 1;
  483. }
  484. else {
  485. high = mid - 1;
  486. }
  487. }
  488. found = false;
  489. return low - 1;
  490. }
  491. function upperBound(haystack, needle, index) {
  492. for (let i = index + 1; i < haystack.length; index = i++) {
  493. if (haystack[i][COLUMN$1] !== needle)
  494. break;
  495. }
  496. return index;
  497. }
  498. function lowerBound(haystack, needle, index) {
  499. for (let i = index - 1; i >= 0; index = i--) {
  500. if (haystack[i][COLUMN$1] !== needle)
  501. break;
  502. }
  503. return index;
  504. }
  505. function memoizedState() {
  506. return {
  507. lastKey: -1,
  508. lastNeedle: -1,
  509. lastIndex: -1,
  510. };
  511. }
  512. /**
  513. * This overly complicated beast is just to record the last tested line/column and the resulting
  514. * index, allowing us to skip a few tests if mappings are monotonically increasing.
  515. */
  516. function memoizedBinarySearch(haystack, needle, state, key) {
  517. const { lastKey, lastNeedle, lastIndex } = state;
  518. let low = 0;
  519. let high = haystack.length - 1;
  520. if (key === lastKey) {
  521. if (needle === lastNeedle) {
  522. found = lastIndex !== -1 && haystack[lastIndex][COLUMN$1] === needle;
  523. return lastIndex;
  524. }
  525. if (needle >= lastNeedle) {
  526. // lastIndex may be -1 if the previous needle was not found.
  527. low = lastIndex === -1 ? 0 : lastIndex;
  528. }
  529. else {
  530. high = lastIndex;
  531. }
  532. }
  533. state.lastKey = key;
  534. state.lastNeedle = needle;
  535. return (state.lastIndex = binarySearch(haystack, needle, low, high));
  536. }
  537. // Rebuilds the original source files, with mappings that are ordered by source line/column instead
  538. // of generated line/column.
  539. function buildBySources(decoded, memos) {
  540. const sources = memos.map(buildNullArray);
  541. for (let i = 0; i < decoded.length; i++) {
  542. const line = decoded[i];
  543. for (let j = 0; j < line.length; j++) {
  544. const seg = line[j];
  545. if (seg.length === 1)
  546. continue;
  547. const sourceIndex = seg[SOURCES_INDEX$1];
  548. const sourceLine = seg[SOURCE_LINE$1];
  549. const sourceColumn = seg[SOURCE_COLUMN$1];
  550. const originalSource = sources[sourceIndex];
  551. const originalLine = (originalSource[sourceLine] || (originalSource[sourceLine] = []));
  552. const memo = memos[sourceIndex];
  553. // The binary search either found a match, or it found the left-index just before where the
  554. // segment should go. Either way, we want to insert after that. And there may be multiple
  555. // generated segments associated with an original location, so there may need to move several
  556. // indexes before we find where we need to insert.
  557. let index = upperBound(originalLine, sourceColumn, memoizedBinarySearch(originalLine, sourceColumn, memo, sourceLine));
  558. memo.lastIndex = ++index;
  559. insert$1(originalLine, index, [sourceColumn, i, seg[COLUMN$1]]);
  560. }
  561. }
  562. return sources;
  563. }
  564. function insert$1(array, index, value) {
  565. for (let i = array.length; i > index; i--) {
  566. array[i] = array[i - 1];
  567. }
  568. array[index] = value;
  569. }
  570. // Null arrays allow us to use ordered index keys without actually allocating contiguous memory like
  571. // a real array. We use a null-prototype object to avoid prototype pollution and deoptimizations.
  572. // Numeric properties on objects are magically sorted in ascending order by the engine regardless of
  573. // the insertion order. So, by setting any numeric keys, even out of order, we'll get ascending
  574. // order when iterating with for-in.
  575. function buildNullArray() {
  576. return { __proto__: null };
  577. }
  578. const AnyMap = function (map, mapUrl) {
  579. const parsed = parse(map);
  580. if (!('sections' in parsed)) {
  581. return new TraceMap(parsed, mapUrl);
  582. }
  583. const mappings = [];
  584. const sources = [];
  585. const sourcesContent = [];
  586. const names = [];
  587. const ignoreList = [];
  588. recurse(parsed, mapUrl, mappings, sources, sourcesContent, names, ignoreList, 0, 0, Infinity, Infinity);
  589. const joined = {
  590. version: 3,
  591. file: parsed.file,
  592. names,
  593. sources,
  594. sourcesContent,
  595. mappings,
  596. ignoreList,
  597. };
  598. return presortedDecodedMap(joined);
  599. };
  600. function parse(map) {
  601. return typeof map === 'string' ? JSON.parse(map) : map;
  602. }
  603. function recurse(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
  604. const { sections } = input;
  605. for (let i = 0; i < sections.length; i++) {
  606. const { map, offset } = sections[i];
  607. let sl = stopLine;
  608. let sc = stopColumn;
  609. if (i + 1 < sections.length) {
  610. const nextOffset = sections[i + 1].offset;
  611. sl = Math.min(stopLine, lineOffset + nextOffset.line);
  612. if (sl === stopLine) {
  613. sc = Math.min(stopColumn, columnOffset + nextOffset.column);
  614. }
  615. else if (sl < stopLine) {
  616. sc = columnOffset + nextOffset.column;
  617. }
  618. }
  619. addSection(map, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset + offset.line, columnOffset + offset.column, sl, sc);
  620. }
  621. }
  622. function addSection(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
  623. const parsed = parse(input);
  624. if ('sections' in parsed)
  625. return recurse(...arguments);
  626. const map = new TraceMap(parsed, mapUrl);
  627. const sourcesOffset = sources.length;
  628. const namesOffset = names.length;
  629. const decoded = decodedMappings(map);
  630. const { resolvedSources, sourcesContent: contents, ignoreList: ignores } = map;
  631. append(sources, resolvedSources);
  632. append(names, map.names);
  633. if (contents)
  634. append(sourcesContent, contents);
  635. else
  636. for (let i = 0; i < resolvedSources.length; i++)
  637. sourcesContent.push(null);
  638. if (ignores)
  639. for (let i = 0; i < ignores.length; i++)
  640. ignoreList.push(ignores[i] + sourcesOffset);
  641. for (let i = 0; i < decoded.length; i++) {
  642. const lineI = lineOffset + i;
  643. // We can only add so many lines before we step into the range that the next section's map
  644. // controls. When we get to the last line, then we'll start checking the segments to see if
  645. // they've crossed into the column range. But it may not have any columns that overstep, so we
  646. // still need to check that we don't overstep lines, too.
  647. if (lineI > stopLine)
  648. return;
  649. // The out line may already exist in mappings (if we're continuing the line started by a
  650. // previous section). Or, we may have jumped ahead several lines to start this section.
  651. const out = getLine$1(mappings, lineI);
  652. // On the 0th loop, the section's column offset shifts us forward. On all other lines (since the
  653. // map can be multiple lines), it doesn't.
  654. const cOffset = i === 0 ? columnOffset : 0;
  655. const line = decoded[i];
  656. for (let j = 0; j < line.length; j++) {
  657. const seg = line[j];
  658. const column = cOffset + seg[COLUMN$1];
  659. // If this segment steps into the column range that the next section's map controls, we need
  660. // to stop early.
  661. if (lineI === stopLine && column >= stopColumn)
  662. return;
  663. if (seg.length === 1) {
  664. out.push([column]);
  665. continue;
  666. }
  667. const sourcesIndex = sourcesOffset + seg[SOURCES_INDEX$1];
  668. const sourceLine = seg[SOURCE_LINE$1];
  669. const sourceColumn = seg[SOURCE_COLUMN$1];
  670. out.push(seg.length === 4
  671. ? [column, sourcesIndex, sourceLine, sourceColumn]
  672. : [column, sourcesIndex, sourceLine, sourceColumn, namesOffset + seg[NAMES_INDEX$1]]);
  673. }
  674. }
  675. }
  676. function append(arr, other) {
  677. for (let i = 0; i < other.length; i++)
  678. arr.push(other[i]);
  679. }
  680. function getLine$1(arr, index) {
  681. for (let i = arr.length; i <= index; i++)
  682. arr[i] = [];
  683. return arr[index];
  684. }
  685. const LINE_GTR_ZERO = '`line` must be greater than 0 (lines start at line 1)';
  686. const COL_GTR_EQ_ZERO = '`column` must be greater than or equal to 0 (columns start at column 0)';
  687. const LEAST_UPPER_BOUND = -1;
  688. const GREATEST_LOWER_BOUND = 1;
  689. class TraceMap {
  690. constructor(map, mapUrl) {
  691. const isString = typeof map === 'string';
  692. if (!isString && map._decodedMemo)
  693. return map;
  694. const parsed = (isString ? JSON.parse(map) : map);
  695. const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
  696. this.version = version;
  697. this.file = file;
  698. this.names = names || [];
  699. this.sourceRoot = sourceRoot;
  700. this.sources = sources;
  701. this.sourcesContent = sourcesContent;
  702. this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || undefined;
  703. const from = resolve(sourceRoot || '', stripFilename(mapUrl));
  704. this.resolvedSources = sources.map((s) => resolve(s || '', from));
  705. const { mappings } = parsed;
  706. if (typeof mappings === 'string') {
  707. this._encoded = mappings;
  708. this._decoded = undefined;
  709. }
  710. else {
  711. this._encoded = undefined;
  712. this._decoded = maybeSort(mappings, isString);
  713. }
  714. this._decodedMemo = memoizedState();
  715. this._bySources = undefined;
  716. this._bySourceMemos = undefined;
  717. }
  718. }
  719. /**
  720. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  721. * with public access modifiers.
  722. */
  723. function cast$2(map) {
  724. return map;
  725. }
  726. /**
  727. * Returns the encoded (VLQ string) form of the SourceMap's mappings field.
  728. */
  729. function encodedMappings(map) {
  730. var _a;
  731. var _b;
  732. return ((_a = (_b = cast$2(map))._encoded) !== null && _a !== void 0 ? _a : (_b._encoded = encode(cast$2(map)._decoded)));
  733. }
  734. /**
  735. * Returns the decoded (array of lines of segments) form of the SourceMap's mappings field.
  736. */
  737. function decodedMappings(map) {
  738. var _a;
  739. return ((_a = cast$2(map))._decoded || (_a._decoded = decode(cast$2(map)._encoded)));
  740. }
  741. /**
  742. * A higher-level API to find the source/line/column associated with a generated line/column
  743. * (think, from a stack trace). Line is 1-based, but column is 0-based, due to legacy behavior in
  744. * `source-map` library.
  745. */
  746. function originalPositionFor(map, needle) {
  747. let { line, column, bias } = needle;
  748. line--;
  749. if (line < 0)
  750. throw new Error(LINE_GTR_ZERO);
  751. if (column < 0)
  752. throw new Error(COL_GTR_EQ_ZERO);
  753. const decoded = decodedMappings(map);
  754. // It's common for parent source maps to have pointers to lines that have no
  755. // mapping (like a "//# sourceMappingURL=") at the end of the child file.
  756. if (line >= decoded.length)
  757. return OMapping(null, null, null, null);
  758. const segments = decoded[line];
  759. const index = traceSegmentInternal(segments, cast$2(map)._decodedMemo, line, column, bias || GREATEST_LOWER_BOUND);
  760. if (index === -1)
  761. return OMapping(null, null, null, null);
  762. const segment = segments[index];
  763. if (segment.length === 1)
  764. return OMapping(null, null, null, null);
  765. const { names, resolvedSources } = map;
  766. return OMapping(resolvedSources[segment[SOURCES_INDEX$1]], segment[SOURCE_LINE$1] + 1, segment[SOURCE_COLUMN$1], segment.length === 5 ? names[segment[NAMES_INDEX$1]] : null);
  767. }
  768. /**
  769. * Finds the generated line/column position of the provided source/line/column source position.
  770. */
  771. function generatedPositionFor(map, needle) {
  772. const { source, line, column, bias } = needle;
  773. return generatedPosition(map, source, line, column, bias || GREATEST_LOWER_BOUND, false);
  774. }
  775. /**
  776. * Finds all generated line/column positions of the provided source/line/column source position.
  777. */
  778. function allGeneratedPositionsFor(map, needle) {
  779. const { source, line, column, bias } = needle;
  780. // SourceMapConsumer uses LEAST_UPPER_BOUND for some reason, so we follow suit.
  781. return generatedPosition(map, source, line, column, bias || LEAST_UPPER_BOUND, true);
  782. }
  783. /**
  784. * Iterates each mapping in generated position order.
  785. */
  786. function eachMapping(map, cb) {
  787. const decoded = decodedMappings(map);
  788. const { names, resolvedSources } = map;
  789. for (let i = 0; i < decoded.length; i++) {
  790. const line = decoded[i];
  791. for (let j = 0; j < line.length; j++) {
  792. const seg = line[j];
  793. const generatedLine = i + 1;
  794. const generatedColumn = seg[0];
  795. let source = null;
  796. let originalLine = null;
  797. let originalColumn = null;
  798. let name = null;
  799. if (seg.length !== 1) {
  800. source = resolvedSources[seg[1]];
  801. originalLine = seg[2] + 1;
  802. originalColumn = seg[3];
  803. }
  804. if (seg.length === 5)
  805. name = names[seg[4]];
  806. cb({
  807. generatedLine,
  808. generatedColumn,
  809. source,
  810. originalLine,
  811. originalColumn,
  812. name,
  813. });
  814. }
  815. }
  816. }
  817. function sourceIndex(map, source) {
  818. const { sources, resolvedSources } = map;
  819. let index = sources.indexOf(source);
  820. if (index === -1)
  821. index = resolvedSources.indexOf(source);
  822. return index;
  823. }
  824. /**
  825. * Retrieves the source content for a particular source, if its found. Returns null if not.
  826. */
  827. function sourceContentFor(map, source) {
  828. const { sourcesContent } = map;
  829. if (sourcesContent == null)
  830. return null;
  831. const index = sourceIndex(map, source);
  832. return index === -1 ? null : sourcesContent[index];
  833. }
  834. /**
  835. * A helper that skips sorting of the input map's mappings array, which can be expensive for larger
  836. * maps.
  837. */
  838. function presortedDecodedMap(map, mapUrl) {
  839. const tracer = new TraceMap(clone(map, []), mapUrl);
  840. cast$2(tracer)._decoded = map.mappings;
  841. return tracer;
  842. }
  843. function clone(map, mappings) {
  844. return {
  845. version: map.version,
  846. file: map.file,
  847. names: map.names,
  848. sourceRoot: map.sourceRoot,
  849. sources: map.sources,
  850. sourcesContent: map.sourcesContent,
  851. mappings,
  852. ignoreList: map.ignoreList || map.x_google_ignoreList,
  853. };
  854. }
  855. function OMapping(source, line, column, name) {
  856. return { source, line, column, name };
  857. }
  858. function GMapping(line, column) {
  859. return { line, column };
  860. }
  861. function traceSegmentInternal(segments, memo, line, column, bias) {
  862. let index = memoizedBinarySearch(segments, column, memo, line);
  863. if (found) {
  864. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  865. }
  866. else if (bias === LEAST_UPPER_BOUND)
  867. index++;
  868. if (index === -1 || index === segments.length)
  869. return -1;
  870. return index;
  871. }
  872. function sliceGeneratedPositions(segments, memo, line, column, bias) {
  873. let min = traceSegmentInternal(segments, memo, line, column, GREATEST_LOWER_BOUND);
  874. // We ignored the bias when tracing the segment so that we're guarnateed to find the first (in
  875. // insertion order) segment that matched. Even if we did respect the bias when tracing, we would
  876. // still need to call `lowerBound()` to find the first segment, which is slower than just looking
  877. // for the GREATEST_LOWER_BOUND to begin with. The only difference that matters for us is when the
  878. // binary search didn't match, in which case GREATEST_LOWER_BOUND just needs to increment to
  879. // match LEAST_UPPER_BOUND.
  880. if (!found && bias === LEAST_UPPER_BOUND)
  881. min++;
  882. if (min === -1 || min === segments.length)
  883. return [];
  884. // We may have found the segment that started at an earlier column. If this is the case, then we
  885. // need to slice all generated segments that match _that_ column, because all such segments span
  886. // to our desired column.
  887. const matchedColumn = found ? column : segments[min][COLUMN$1];
  888. // The binary search is not guaranteed to find the lower bound when a match wasn't found.
  889. if (!found)
  890. min = lowerBound(segments, matchedColumn, min);
  891. const max = upperBound(segments, matchedColumn, min);
  892. const result = [];
  893. for (; min <= max; min++) {
  894. const segment = segments[min];
  895. result.push(GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]));
  896. }
  897. return result;
  898. }
  899. function generatedPosition(map, source, line, column, bias, all) {
  900. var _a;
  901. line--;
  902. if (line < 0)
  903. throw new Error(LINE_GTR_ZERO);
  904. if (column < 0)
  905. throw new Error(COL_GTR_EQ_ZERO);
  906. const { sources, resolvedSources } = map;
  907. let sourceIndex = sources.indexOf(source);
  908. if (sourceIndex === -1)
  909. sourceIndex = resolvedSources.indexOf(source);
  910. if (sourceIndex === -1)
  911. return all ? [] : GMapping(null, null);
  912. const generated = ((_a = cast$2(map))._bySources || (_a._bySources = buildBySources(decodedMappings(map), (cast$2(map)._bySourceMemos = sources.map(memoizedState)))));
  913. const segments = generated[sourceIndex][line];
  914. if (segments == null)
  915. return all ? [] : GMapping(null, null);
  916. const memo = cast$2(map)._bySourceMemos[sourceIndex];
  917. if (all)
  918. return sliceGeneratedPositions(segments, memo, line, column, bias);
  919. const index = traceSegmentInternal(segments, memo, line, column, bias);
  920. if (index === -1)
  921. return GMapping(null, null);
  922. const segment = segments[index];
  923. return GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]);
  924. }
  925. /**
  926. * SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
  927. * index of the `key` in the backing array.
  928. *
  929. * This is designed to allow synchronizing a second array with the contents of the backing array,
  930. * like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
  931. * and there are never duplicates.
  932. */
  933. class SetArray {
  934. constructor() {
  935. this._indexes = { __proto__: null };
  936. this.array = [];
  937. }
  938. }
  939. /**
  940. * Typescript doesn't allow friend access to private fields, so this just casts the set into a type
  941. * with public access modifiers.
  942. */
  943. function cast$1(set) {
  944. return set;
  945. }
  946. /**
  947. * Gets the index associated with `key` in the backing array, if it is already present.
  948. */
  949. function get(setarr, key) {
  950. return cast$1(setarr)._indexes[key];
  951. }
  952. /**
  953. * Puts `key` into the backing array, if it is not already present. Returns
  954. * the index of the `key` in the backing array.
  955. */
  956. function put(setarr, key) {
  957. // The key may or may not be present. If it is present, it's a number.
  958. const index = get(setarr, key);
  959. if (index !== undefined)
  960. return index;
  961. const { array, _indexes: indexes } = cast$1(setarr);
  962. const length = array.push(key);
  963. return (indexes[key] = length - 1);
  964. }
  965. const COLUMN = 0;
  966. const SOURCES_INDEX = 1;
  967. const SOURCE_LINE = 2;
  968. const SOURCE_COLUMN = 3;
  969. const NAMES_INDEX = 4;
  970. const NO_NAME = -1;
  971. /**
  972. * Provides the state to generate a sourcemap.
  973. */
  974. class GenMapping {
  975. constructor({ file, sourceRoot } = {}) {
  976. this._names = new SetArray();
  977. this._sources = new SetArray();
  978. this._sourcesContent = [];
  979. this._mappings = [];
  980. this.file = file;
  981. this.sourceRoot = sourceRoot;
  982. this._ignoreList = new SetArray();
  983. }
  984. }
  985. /**
  986. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  987. * with public access modifiers.
  988. */
  989. function cast(map) {
  990. return map;
  991. }
  992. /**
  993. * Same as `addMapping`, but will only add the mapping if it generates useful information in the
  994. * resulting map. This only works correctly if mappings are added **in order**, meaning you should
  995. * not add a mapping with a lower generated line/column than one that came before.
  996. */
  997. const maybeAddMapping = (map, mapping) => {
  998. return addMappingInternal(true, map, mapping);
  999. };
  1000. /**
  1001. * Adds/removes the content of the source file to the source map.
  1002. */
  1003. function setSourceContent(map, source, content) {
  1004. const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
  1005. const index = put(sources, source);
  1006. sourcesContent[index] = content;
  1007. }
  1008. /**
  1009. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  1010. * a sourcemap, or to JSON.stringify.
  1011. */
  1012. function toDecodedMap(map) {
  1013. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, _ignoreList: ignoreList, } = cast(map);
  1014. removeEmptyFinalLines(mappings);
  1015. return {
  1016. version: 3,
  1017. file: map.file || undefined,
  1018. names: names.array,
  1019. sourceRoot: map.sourceRoot || undefined,
  1020. sources: sources.array,
  1021. sourcesContent,
  1022. mappings,
  1023. ignoreList: ignoreList.array,
  1024. };
  1025. }
  1026. /**
  1027. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  1028. * a sourcemap, or to JSON.stringify.
  1029. */
  1030. function toEncodedMap(map) {
  1031. const decoded = toDecodedMap(map);
  1032. return Object.assign(Object.assign({}, decoded), { mappings: encode(decoded.mappings) });
  1033. }
  1034. /**
  1035. * Constructs a new GenMapping, using the already present mappings of the input.
  1036. */
  1037. function fromMap(input) {
  1038. const map = new TraceMap(input);
  1039. const gen = new GenMapping({ file: map.file, sourceRoot: map.sourceRoot });
  1040. putAll(cast(gen)._names, map.names);
  1041. putAll(cast(gen)._sources, map.sources);
  1042. cast(gen)._sourcesContent = map.sourcesContent || map.sources.map(() => null);
  1043. cast(gen)._mappings = decodedMappings(map);
  1044. if (map.ignoreList)
  1045. putAll(cast(gen)._ignoreList, map.ignoreList);
  1046. return gen;
  1047. }
  1048. // This split declaration is only so that terser can elminiate the static initialization block.
  1049. function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  1050. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = cast(map);
  1051. const line = getLine(mappings, genLine);
  1052. const index = getColumnIndex(line, genColumn);
  1053. if (!source) {
  1054. if (skipable && skipSourceless(line, index))
  1055. return;
  1056. return insert(line, index, [genColumn]);
  1057. }
  1058. const sourcesIndex = put(sources, source);
  1059. const namesIndex = name ? put(names, name) : NO_NAME;
  1060. if (sourcesIndex === sourcesContent.length)
  1061. sourcesContent[sourcesIndex] = content !== null && content !== void 0 ? content : null;
  1062. if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  1063. return;
  1064. }
  1065. return insert(line, index, name
  1066. ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
  1067. : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
  1068. }
  1069. function getLine(mappings, index) {
  1070. for (let i = mappings.length; i <= index; i++) {
  1071. mappings[i] = [];
  1072. }
  1073. return mappings[index];
  1074. }
  1075. function getColumnIndex(line, genColumn) {
  1076. let index = line.length;
  1077. for (let i = index - 1; i >= 0; index = i--) {
  1078. const current = line[i];
  1079. if (genColumn >= current[COLUMN])
  1080. break;
  1081. }
  1082. return index;
  1083. }
  1084. function insert(array, index, value) {
  1085. for (let i = array.length; i > index; i--) {
  1086. array[i] = array[i - 1];
  1087. }
  1088. array[index] = value;
  1089. }
  1090. function removeEmptyFinalLines(mappings) {
  1091. const { length } = mappings;
  1092. let len = length;
  1093. for (let i = len - 1; i >= 0; len = i, i--) {
  1094. if (mappings[i].length > 0)
  1095. break;
  1096. }
  1097. if (len < length)
  1098. mappings.length = len;
  1099. }
  1100. function putAll(setarr, array) {
  1101. for (let i = 0; i < array.length; i++)
  1102. put(setarr, array[i]);
  1103. }
  1104. function skipSourceless(line, index) {
  1105. // The start of a line is already sourceless, so adding a sourceless segment to the beginning
  1106. // doesn't generate any useful information.
  1107. if (index === 0)
  1108. return true;
  1109. const prev = line[index - 1];
  1110. // If the previous segment is also sourceless, then adding another sourceless segment doesn't
  1111. // genrate any new information. Else, this segment will end the source/named segment and point to
  1112. // a sourceless position, which is useful.
  1113. return prev.length === 1;
  1114. }
  1115. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  1116. // A source/named segment at the start of a line gives position at that genColumn
  1117. if (index === 0)
  1118. return false;
  1119. const prev = line[index - 1];
  1120. // If the previous segment is sourceless, then we're transitioning to a source.
  1121. if (prev.length === 1)
  1122. return false;
  1123. // If the previous segment maps to the exact same source position, then this segment doesn't
  1124. // provide any new position information.
  1125. return (sourcesIndex === prev[SOURCES_INDEX] &&
  1126. sourceLine === prev[SOURCE_LINE] &&
  1127. sourceColumn === prev[SOURCE_COLUMN] &&
  1128. namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME));
  1129. }
  1130. function addMappingInternal(skipable, map, mapping) {
  1131. const { generated, source, original, name, content } = mapping;
  1132. if (!source) {
  1133. return addSegmentInternal(skipable, map, generated.line - 1, generated.column, null, null, null, null, null);
  1134. }
  1135. return addSegmentInternal(skipable, map, generated.line - 1, generated.column, source, original.line - 1, original.column, name, content);
  1136. }
  1137. class SourceMapConsumer {
  1138. constructor(map, mapUrl) {
  1139. const trace = (this._map = new AnyMap(map, mapUrl));
  1140. this.file = trace.file;
  1141. this.names = trace.names;
  1142. this.sourceRoot = trace.sourceRoot;
  1143. this.sources = trace.resolvedSources;
  1144. this.sourcesContent = trace.sourcesContent;
  1145. this.version = trace.version;
  1146. }
  1147. static fromSourceMap(map, mapUrl) {
  1148. // This is more performant if we receive
  1149. // a @jridgewell/source-map SourceMapGenerator
  1150. if (map.toDecodedMap) {
  1151. return new SourceMapConsumer(map.toDecodedMap(), mapUrl);
  1152. }
  1153. // This is a fallback for `source-map` and `source-map-js`
  1154. return new SourceMapConsumer(map.toJSON(), mapUrl);
  1155. }
  1156. get mappings() {
  1157. return encodedMappings(this._map);
  1158. }
  1159. originalPositionFor(needle) {
  1160. return originalPositionFor(this._map, needle);
  1161. }
  1162. generatedPositionFor(originalPosition) {
  1163. return generatedPositionFor(this._map, originalPosition);
  1164. }
  1165. allGeneratedPositionsFor(originalPosition) {
  1166. return allGeneratedPositionsFor(this._map, originalPosition);
  1167. }
  1168. hasContentsOfAllSources() {
  1169. if (!this.sourcesContent || this.sourcesContent.length !== this.sources.length) {
  1170. return false;
  1171. }
  1172. for (const content of this.sourcesContent) {
  1173. if (content == null) {
  1174. return false;
  1175. }
  1176. }
  1177. return true;
  1178. }
  1179. sourceContentFor(source, nullOnMissing) {
  1180. const sourceContent = sourceContentFor(this._map, source);
  1181. if (sourceContent != null) {
  1182. return sourceContent;
  1183. }
  1184. if (nullOnMissing) {
  1185. return null;
  1186. }
  1187. throw new Error(`"${source}" is not in the SourceMap.`);
  1188. }
  1189. eachMapping(callback, context /*, order?: number*/) {
  1190. // order is ignored as @jridgewell/trace-map doesn't implement it
  1191. eachMapping(this._map, context ? callback.bind(context) : callback);
  1192. }
  1193. destroy() {
  1194. // noop.
  1195. }
  1196. }
  1197. class SourceMapGenerator {
  1198. constructor(opts) {
  1199. // TODO :: should this be duck-typed ?
  1200. this._map = opts instanceof GenMapping ? opts : new GenMapping(opts);
  1201. }
  1202. static fromSourceMap(consumer) {
  1203. return new SourceMapGenerator(fromMap(consumer));
  1204. }
  1205. addMapping(mapping) {
  1206. maybeAddMapping(this._map, mapping);
  1207. }
  1208. setSourceContent(source, content) {
  1209. setSourceContent(this._map, source, content);
  1210. }
  1211. toJSON() {
  1212. return toEncodedMap(this._map);
  1213. }
  1214. toString() {
  1215. return JSON.stringify(this.toJSON());
  1216. }
  1217. toDecodedMap() {
  1218. return toDecodedMap(this._map);
  1219. }
  1220. }
  1221. exports.SourceMapConsumer = SourceMapConsumer;
  1222. exports.SourceMapGenerator = SourceMapGenerator;
  1223. Object.defineProperty(exports, '__esModule', { value: true });
  1224. }));
  1225. //# sourceMappingURL=source-map.umd.js.map