gen-mapping.umd.js 11 KB

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