trace-mapping.mjs 15 KB

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