gen-mapping.umd.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@jridgewell/set-array'), require('@jridgewell/sourcemap-codec')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@jridgewell/set-array', '@jridgewell/sourcemap-codec'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.genMapping = {}, global.setArray, global.sourcemapCodec));
  5. })(this, (function (exports, setArray, sourcemapCodec) { 'use strict';
  6. /**
  7. * A low-level API to associate a generated position with an original source position. Line and
  8. * column here are 0-based, unlike `addMapping`.
  9. */
  10. exports.addSegment = void 0;
  11. /**
  12. * A high-level API to associate a generated position with an original source position. Line is
  13. * 1-based, but column is 0-based, due to legacy behavior in `source-map` library.
  14. */
  15. exports.addMapping = void 0;
  16. /**
  17. * Adds/removes the content of the source file to the source map.
  18. */
  19. exports.setSourceContent = void 0;
  20. /**
  21. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  22. * a sourcemap, or to JSON.stringify.
  23. */
  24. exports.decodedMap = void 0;
  25. /**
  26. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  27. * a sourcemap, or to JSON.stringify.
  28. */
  29. exports.encodedMap = void 0;
  30. /**
  31. * Returns an array of high-level mapping objects for every recorded segment, which could then be
  32. * passed to the `source-map` library.
  33. */
  34. exports.allMappings = void 0;
  35. /**
  36. * Provides the state to generate a sourcemap.
  37. */
  38. class GenMapping {
  39. constructor({ file, sourceRoot } = {}) {
  40. this._names = new setArray.SetArray();
  41. this._sources = new setArray.SetArray();
  42. this._sourcesContent = [];
  43. this._mappings = [];
  44. this.file = file;
  45. this.sourceRoot = sourceRoot;
  46. }
  47. }
  48. (() => {
  49. exports.addSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name) => {
  50. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = map;
  51. const line = getLine(mappings, genLine);
  52. if (source == null) {
  53. const seg = [genColumn];
  54. const index = getColumnIndex(line, genColumn, seg);
  55. return insert(line, index, seg);
  56. }
  57. const sourcesIndex = setArray.put(sources, source);
  58. const seg = name
  59. ? [genColumn, sourcesIndex, sourceLine, sourceColumn, setArray.put(names, name)]
  60. : [genColumn, sourcesIndex, sourceLine, sourceColumn];
  61. const index = getColumnIndex(line, genColumn, seg);
  62. if (sourcesIndex === sourcesContent.length)
  63. sourcesContent[sourcesIndex] = null;
  64. insert(line, index, seg);
  65. };
  66. exports.addMapping = (map, mapping) => {
  67. const { generated, source, original, name } = mapping;
  68. return exports.addSegment(map, generated.line - 1, generated.column, source, original == null ? undefined : original.line - 1, original === null || original === void 0 ? void 0 : original.column, name);
  69. };
  70. exports.setSourceContent = (map, source, content) => {
  71. const { _sources: sources, _sourcesContent: sourcesContent } = map;
  72. sourcesContent[setArray.put(sources, source)] = content;
  73. };
  74. exports.decodedMap = (map) => {
  75. const { file, sourceRoot, _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = map;
  76. return {
  77. version: 3,
  78. file,
  79. names: names.array,
  80. sourceRoot: sourceRoot || undefined,
  81. sources: sources.array,
  82. sourcesContent,
  83. mappings,
  84. };
  85. };
  86. exports.encodedMap = (map) => {
  87. const decoded = exports.decodedMap(map);
  88. return Object.assign(Object.assign({}, decoded), { mappings: sourcemapCodec.encode(decoded.mappings) });
  89. };
  90. exports.allMappings = (map) => {
  91. const out = [];
  92. const { _mappings: mappings, _sources: sources, _names: names } = map;
  93. for (let i = 0; i < mappings.length; i++) {
  94. const line = mappings[i];
  95. for (let j = 0; j < line.length; j++) {
  96. const seg = line[j];
  97. const generated = { line: i + 1, column: seg[0] };
  98. let source = undefined;
  99. let original = undefined;
  100. let name = undefined;
  101. if (seg.length !== 1) {
  102. source = sources.array[seg[1]];
  103. original = { line: seg[2] + 1, column: seg[3] };
  104. if (seg.length === 5)
  105. name = names.array[seg[4]];
  106. }
  107. out.push({ generated, source, original, name });
  108. }
  109. }
  110. return out;
  111. };
  112. })();
  113. function getLine(mappings, index) {
  114. for (let i = mappings.length; i <= index; i++) {
  115. mappings[i] = [];
  116. }
  117. return mappings[index];
  118. }
  119. function getColumnIndex(line, column, seg) {
  120. let index = line.length;
  121. for (let i = index - 1; i >= 0; i--, index--) {
  122. const current = line[i];
  123. const col = current[0];
  124. if (col > column)
  125. continue;
  126. if (col < column)
  127. break;
  128. const cmp = compare(current, seg);
  129. if (cmp === 0)
  130. return index;
  131. if (cmp < 0)
  132. break;
  133. }
  134. return index;
  135. }
  136. function compare(a, b) {
  137. let cmp = compareNum(a.length, b.length);
  138. if (cmp !== 0)
  139. return cmp;
  140. // We've already checked genColumn
  141. if (a.length === 1)
  142. return 0;
  143. cmp = compareNum(a[1], b[1]);
  144. if (cmp !== 0)
  145. return cmp;
  146. cmp = compareNum(a[2], b[2]);
  147. if (cmp !== 0)
  148. return cmp;
  149. cmp = compareNum(a[3], b[3]);
  150. if (cmp !== 0)
  151. return cmp;
  152. if (a.length === 4)
  153. return 0;
  154. return compareNum(a[4], b[4]);
  155. }
  156. function compareNum(a, b) {
  157. return a - b;
  158. }
  159. function insert(array, index, value) {
  160. if (index === -1)
  161. return;
  162. for (let i = array.length; i > index; i--) {
  163. array[i] = array[i - 1];
  164. }
  165. array[index] = value;
  166. }
  167. exports.GenMapping = GenMapping;
  168. Object.defineProperty(exports, '__esModule', { value: true });
  169. }));
  170. //# sourceMappingURL=gen-mapping.umd.js.map