trace-mapping.mjs 15 KB

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