gen-mapping.umd.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@jridgewell/set-array'), require('@jridgewell/sourcemap-codec'), require('@jridgewell/trace-mapping')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@jridgewell/set-array', '@jridgewell/sourcemap-codec', '@jridgewell/trace-mapping'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.genMapping = {}, global.setArray, global.sourcemapCodec, global.traceMapping));
  5. })(this, (function (exports, setArray, sourcemapCodec, traceMapping) { 'use strict';
  6. const COLUMN = 0;
  7. const SOURCES_INDEX = 1;
  8. const SOURCE_LINE = 2;
  9. const SOURCE_COLUMN = 3;
  10. const NAMES_INDEX = 4;
  11. const NO_NAME = -1;
  12. /**
  13. * A low-level API to associate a generated position with an original source position. Line and
  14. * column here are 0-based, unlike `addMapping`.
  15. */
  16. exports.addSegment = void 0;
  17. /**
  18. * A high-level API to associate a generated position with an original source position. Line is
  19. * 1-based, but column is 0-based, due to legacy behavior in `source-map` library.
  20. */
  21. exports.addMapping = void 0;
  22. /**
  23. * Same as `addSegment`, but will only add the segment if it generates useful information in the
  24. * resulting map. This only works correctly if segments are added **in order**, meaning you should
  25. * not add a segment with a lower generated line/column than one that came before.
  26. */
  27. exports.maybeAddSegment = void 0;
  28. /**
  29. * Same as `addMapping`, but will only add the mapping if it generates useful information in the
  30. * resulting map. This only works correctly if mappings are added **in order**, meaning you should
  31. * not add a mapping with a lower generated line/column than one that came before.
  32. */
  33. exports.maybeAddMapping = void 0;
  34. /**
  35. * Adds/removes the content of the source file to the source map.
  36. */
  37. exports.setSourceContent = void 0;
  38. /**
  39. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  40. * a sourcemap, or to JSON.stringify.
  41. */
  42. exports.toDecodedMap = void 0;
  43. /**
  44. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  45. * a sourcemap, or to JSON.stringify.
  46. */
  47. exports.toEncodedMap = void 0;
  48. /**
  49. * Constructs a new GenMapping, using the already present mappings of the input.
  50. */
  51. exports.fromMap = void 0;
  52. /**
  53. * Returns an array of high-level mapping objects for every recorded segment, which could then be
  54. * passed to the `source-map` library.
  55. */
  56. exports.allMappings = void 0;
  57. // This split declaration is only so that terser can elminiate the static initialization block.
  58. let addSegmentInternal;
  59. /**
  60. * Provides the state to generate a sourcemap.
  61. */
  62. class GenMapping {
  63. constructor({ file, sourceRoot } = {}) {
  64. this._names = new setArray.SetArray();
  65. this._sources = new setArray.SetArray();
  66. this._sourcesContent = [];
  67. this._mappings = [];
  68. this.file = file;
  69. this.sourceRoot = sourceRoot;
  70. }
  71. }
  72. (() => {
  73. exports.addSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  74. return addSegmentInternal(false, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
  75. };
  76. exports.maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  77. return addSegmentInternal(true, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
  78. };
  79. exports.addMapping = (map, mapping) => {
  80. return addMappingInternal(false, map, mapping);
  81. };
  82. exports.maybeAddMapping = (map, mapping) => {
  83. return addMappingInternal(true, map, mapping);
  84. };
  85. exports.setSourceContent = (map, source, content) => {
  86. const { _sources: sources, _sourcesContent: sourcesContent } = map;
  87. sourcesContent[setArray.put(sources, source)] = content;
  88. };
  89. exports.toDecodedMap = (map) => {
  90. const { file, sourceRoot, _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = map;
  91. removeEmptyFinalLines(mappings);
  92. return {
  93. version: 3,
  94. file: file || undefined,
  95. names: names.array,
  96. sourceRoot: sourceRoot || undefined,
  97. sources: sources.array,
  98. sourcesContent,
  99. mappings,
  100. };
  101. };
  102. exports.toEncodedMap = (map) => {
  103. const decoded = exports.toDecodedMap(map);
  104. return Object.assign(Object.assign({}, decoded), { mappings: sourcemapCodec.encode(decoded.mappings) });
  105. };
  106. exports.allMappings = (map) => {
  107. const out = [];
  108. const { _mappings: mappings, _sources: sources, _names: names } = map;
  109. for (let i = 0; i < mappings.length; i++) {
  110. const line = mappings[i];
  111. for (let j = 0; j < line.length; j++) {
  112. const seg = line[j];
  113. const generated = { line: i + 1, column: seg[COLUMN] };
  114. let source = undefined;
  115. let original = undefined;
  116. let name = undefined;
  117. if (seg.length !== 1) {
  118. source = sources.array[seg[SOURCES_INDEX]];
  119. original = { line: seg[SOURCE_LINE] + 1, column: seg[SOURCE_COLUMN] };
  120. if (seg.length === 5)
  121. name = names.array[seg[NAMES_INDEX]];
  122. }
  123. out.push({ generated, source, original, name });
  124. }
  125. }
  126. return out;
  127. };
  128. exports.fromMap = (input) => {
  129. const map = new traceMapping.TraceMap(input);
  130. const gen = new GenMapping({ file: map.file, sourceRoot: map.sourceRoot });
  131. putAll(gen._names, map.names);
  132. putAll(gen._sources, map.sources);
  133. gen._sourcesContent = map.sourcesContent || map.sources.map(() => null);
  134. gen._mappings = traceMapping.decodedMappings(map);
  135. return gen;
  136. };
  137. // Internal helpers
  138. addSegmentInternal = (skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  139. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = map;
  140. const line = getLine(mappings, genLine);
  141. const index = getColumnIndex(line, genColumn);
  142. if (!source) {
  143. if (skipable && skipSourceless(line, index))
  144. return;
  145. return insert(line, index, [genColumn]);
  146. }
  147. const sourcesIndex = setArray.put(sources, source);
  148. const namesIndex = name ? setArray.put(names, name) : NO_NAME;
  149. if (sourcesIndex === sourcesContent.length)
  150. sourcesContent[sourcesIndex] = content !== null && content !== void 0 ? content : null;
  151. if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  152. return;
  153. }
  154. return insert(line, index, name
  155. ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
  156. : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
  157. };
  158. })();
  159. function getLine(mappings, index) {
  160. for (let i = mappings.length; i <= index; i++) {
  161. mappings[i] = [];
  162. }
  163. return mappings[index];
  164. }
  165. function getColumnIndex(line, genColumn) {
  166. let index = line.length;
  167. for (let i = index - 1; i >= 0; index = i--) {
  168. const current = line[i];
  169. if (genColumn >= current[COLUMN])
  170. break;
  171. }
  172. return index;
  173. }
  174. function insert(array, index, value) {
  175. for (let i = array.length; i > index; i--) {
  176. array[i] = array[i - 1];
  177. }
  178. array[index] = value;
  179. }
  180. function removeEmptyFinalLines(mappings) {
  181. const { length } = mappings;
  182. let len = length;
  183. for (let i = len - 1; i >= 0; len = i, i--) {
  184. if (mappings[i].length > 0)
  185. break;
  186. }
  187. if (len < length)
  188. mappings.length = len;
  189. }
  190. function putAll(strarr, array) {
  191. for (let i = 0; i < array.length; i++)
  192. setArray.put(strarr, array[i]);
  193. }
  194. function skipSourceless(line, index) {
  195. // The start of a line is already sourceless, so adding a sourceless segment to the beginning
  196. // doesn't generate any useful information.
  197. if (index === 0)
  198. return true;
  199. const prev = line[index - 1];
  200. // If the previous segment is also sourceless, then adding another sourceless segment doesn't
  201. // genrate any new information. Else, this segment will end the source/named segment and point to
  202. // a sourceless position, which is useful.
  203. return prev.length === 1;
  204. }
  205. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  206. // A source/named segment at the start of a line gives position at that genColumn
  207. if (index === 0)
  208. return false;
  209. const prev = line[index - 1];
  210. // If the previous segment is sourceless, then we're transitioning to a source.
  211. if (prev.length === 1)
  212. return false;
  213. // If the previous segment maps to the exact same source position, then this segment doesn't
  214. // provide any new position information.
  215. return (sourcesIndex === prev[SOURCES_INDEX] &&
  216. sourceLine === prev[SOURCE_LINE] &&
  217. sourceColumn === prev[SOURCE_COLUMN] &&
  218. namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME));
  219. }
  220. function addMappingInternal(skipable, map, mapping) {
  221. const { generated, source, original, name, content } = mapping;
  222. if (!source) {
  223. return addSegmentInternal(skipable, map, generated.line - 1, generated.column, null, null, null, null, null);
  224. }
  225. const s = source;
  226. return addSegmentInternal(skipable, map, generated.line - 1, generated.column, s, original.line - 1, original.column, name, content);
  227. }
  228. exports.GenMapping = GenMapping;
  229. Object.defineProperty(exports, '__esModule', { value: true });
  230. }));
  231. //# sourceMappingURL=gen-mapping.umd.js.map