gen-mapping.mjs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // src/set-array.ts
  2. var SetArray = class {
  3. constructor() {
  4. this._indexes = { __proto__: null };
  5. this.array = [];
  6. }
  7. };
  8. function cast(set) {
  9. return set;
  10. }
  11. function get(setarr, key) {
  12. return cast(setarr)._indexes[key];
  13. }
  14. function put(setarr, key) {
  15. const index = get(setarr, key);
  16. if (index !== void 0) return index;
  17. const { array, _indexes: indexes } = cast(setarr);
  18. const length = array.push(key);
  19. return indexes[key] = length - 1;
  20. }
  21. function remove(setarr, key) {
  22. const index = get(setarr, key);
  23. if (index === void 0) return;
  24. const { array, _indexes: indexes } = cast(setarr);
  25. for (let i = index + 1; i < array.length; i++) {
  26. const k = array[i];
  27. array[i - 1] = k;
  28. indexes[k]--;
  29. }
  30. indexes[key] = void 0;
  31. array.pop();
  32. }
  33. // src/gen-mapping.ts
  34. import {
  35. encode
  36. } from "@jridgewell/sourcemap-codec";
  37. import { TraceMap, decodedMappings } from "@jridgewell/trace-mapping";
  38. // src/sourcemap-segment.ts
  39. var COLUMN = 0;
  40. var SOURCES_INDEX = 1;
  41. var SOURCE_LINE = 2;
  42. var SOURCE_COLUMN = 3;
  43. var NAMES_INDEX = 4;
  44. // src/gen-mapping.ts
  45. var NO_NAME = -1;
  46. var GenMapping = class {
  47. constructor({ file, sourceRoot } = {}) {
  48. this._names = new SetArray();
  49. this._sources = new SetArray();
  50. this._sourcesContent = [];
  51. this._mappings = [];
  52. this.file = file;
  53. this.sourceRoot = sourceRoot;
  54. this._ignoreList = new SetArray();
  55. }
  56. };
  57. function cast2(map) {
  58. return map;
  59. }
  60. function addSegment(map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  61. return addSegmentInternal(
  62. false,
  63. map,
  64. genLine,
  65. genColumn,
  66. source,
  67. sourceLine,
  68. sourceColumn,
  69. name,
  70. content
  71. );
  72. }
  73. function addMapping(map, mapping) {
  74. return addMappingInternal(false, map, mapping);
  75. }
  76. var maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  77. return addSegmentInternal(
  78. true,
  79. map,
  80. genLine,
  81. genColumn,
  82. source,
  83. sourceLine,
  84. sourceColumn,
  85. name,
  86. content
  87. );
  88. };
  89. var maybeAddMapping = (map, mapping) => {
  90. return addMappingInternal(true, map, mapping);
  91. };
  92. function setSourceContent(map, source, content) {
  93. const {
  94. _sources: sources,
  95. _sourcesContent: sourcesContent
  96. // _originalScopes: originalScopes,
  97. } = cast2(map);
  98. const index = put(sources, source);
  99. sourcesContent[index] = content;
  100. }
  101. function setIgnore(map, source, ignore = true) {
  102. const {
  103. _sources: sources,
  104. _sourcesContent: sourcesContent,
  105. _ignoreList: ignoreList
  106. // _originalScopes: originalScopes,
  107. } = cast2(map);
  108. const index = put(sources, source);
  109. if (index === sourcesContent.length) sourcesContent[index] = null;
  110. if (ignore) put(ignoreList, index);
  111. else remove(ignoreList, index);
  112. }
  113. function toDecodedMap(map) {
  114. const {
  115. _mappings: mappings,
  116. _sources: sources,
  117. _sourcesContent: sourcesContent,
  118. _names: names,
  119. _ignoreList: ignoreList
  120. // _originalScopes: originalScopes,
  121. // _generatedRanges: generatedRanges,
  122. } = cast2(map);
  123. removeEmptyFinalLines(mappings);
  124. return {
  125. version: 3,
  126. file: map.file || void 0,
  127. names: names.array,
  128. sourceRoot: map.sourceRoot || void 0,
  129. sources: sources.array,
  130. sourcesContent,
  131. mappings,
  132. // originalScopes,
  133. // generatedRanges,
  134. ignoreList: ignoreList.array
  135. };
  136. }
  137. function toEncodedMap(map) {
  138. const decoded = toDecodedMap(map);
  139. return Object.assign({}, decoded, {
  140. // originalScopes: decoded.originalScopes.map((os) => encodeOriginalScopes(os)),
  141. // generatedRanges: encodeGeneratedRanges(decoded.generatedRanges as GeneratedRange[]),
  142. mappings: encode(decoded.mappings)
  143. });
  144. }
  145. function fromMap(input) {
  146. const map = new TraceMap(input);
  147. const gen = new GenMapping({ file: map.file, sourceRoot: map.sourceRoot });
  148. putAll(cast2(gen)._names, map.names);
  149. putAll(cast2(gen)._sources, map.sources);
  150. cast2(gen)._sourcesContent = map.sourcesContent || map.sources.map(() => null);
  151. cast2(gen)._mappings = decodedMappings(map);
  152. if (map.ignoreList) putAll(cast2(gen)._ignoreList, map.ignoreList);
  153. return gen;
  154. }
  155. function allMappings(map) {
  156. const out = [];
  157. const { _mappings: mappings, _sources: sources, _names: names } = cast2(map);
  158. for (let i = 0; i < mappings.length; i++) {
  159. const line = mappings[i];
  160. for (let j = 0; j < line.length; j++) {
  161. const seg = line[j];
  162. const generated = { line: i + 1, column: seg[COLUMN] };
  163. let source = void 0;
  164. let original = void 0;
  165. let name = void 0;
  166. if (seg.length !== 1) {
  167. source = sources.array[seg[SOURCES_INDEX]];
  168. original = { line: seg[SOURCE_LINE] + 1, column: seg[SOURCE_COLUMN] };
  169. if (seg.length === 5) name = names.array[seg[NAMES_INDEX]];
  170. }
  171. out.push({ generated, source, original, name });
  172. }
  173. }
  174. return out;
  175. }
  176. function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  177. const {
  178. _mappings: mappings,
  179. _sources: sources,
  180. _sourcesContent: sourcesContent,
  181. _names: names
  182. // _originalScopes: originalScopes,
  183. } = cast2(map);
  184. const line = getIndex(mappings, genLine);
  185. const index = getColumnIndex(line, genColumn);
  186. if (!source) {
  187. if (skipable && skipSourceless(line, index)) return;
  188. return insert(line, index, [genColumn]);
  189. }
  190. assert(sourceLine);
  191. assert(sourceColumn);
  192. const sourcesIndex = put(sources, source);
  193. const namesIndex = name ? put(names, name) : NO_NAME;
  194. if (sourcesIndex === sourcesContent.length) sourcesContent[sourcesIndex] = content != null ? content : null;
  195. if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  196. return;
  197. }
  198. return insert(
  199. line,
  200. index,
  201. name ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex] : [genColumn, sourcesIndex, sourceLine, sourceColumn]
  202. );
  203. }
  204. function assert(_val) {
  205. }
  206. function getIndex(arr, index) {
  207. for (let i = arr.length; i <= index; i++) {
  208. arr[i] = [];
  209. }
  210. return arr[index];
  211. }
  212. function getColumnIndex(line, genColumn) {
  213. let index = line.length;
  214. for (let i = index - 1; i >= 0; index = i--) {
  215. const current = line[i];
  216. if (genColumn >= current[COLUMN]) break;
  217. }
  218. return index;
  219. }
  220. function insert(array, index, value) {
  221. for (let i = array.length; i > index; i--) {
  222. array[i] = array[i - 1];
  223. }
  224. array[index] = value;
  225. }
  226. function removeEmptyFinalLines(mappings) {
  227. const { length } = mappings;
  228. let len = length;
  229. for (let i = len - 1; i >= 0; len = i, i--) {
  230. if (mappings[i].length > 0) break;
  231. }
  232. if (len < length) mappings.length = len;
  233. }
  234. function putAll(setarr, array) {
  235. for (let i = 0; i < array.length; i++) put(setarr, array[i]);
  236. }
  237. function skipSourceless(line, index) {
  238. if (index === 0) return true;
  239. const prev = line[index - 1];
  240. return prev.length === 1;
  241. }
  242. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  243. if (index === 0) return false;
  244. const prev = line[index - 1];
  245. if (prev.length === 1) return false;
  246. return sourcesIndex === prev[SOURCES_INDEX] && sourceLine === prev[SOURCE_LINE] && sourceColumn === prev[SOURCE_COLUMN] && namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME);
  247. }
  248. function addMappingInternal(skipable, map, mapping) {
  249. const { generated, source, original, name, content } = mapping;
  250. if (!source) {
  251. return addSegmentInternal(
  252. skipable,
  253. map,
  254. generated.line - 1,
  255. generated.column,
  256. null,
  257. null,
  258. null,
  259. null,
  260. null
  261. );
  262. }
  263. assert(original);
  264. return addSegmentInternal(
  265. skipable,
  266. map,
  267. generated.line - 1,
  268. generated.column,
  269. source,
  270. original.line - 1,
  271. original.column,
  272. name,
  273. content
  274. );
  275. }
  276. export {
  277. GenMapping,
  278. addMapping,
  279. addSegment,
  280. allMappings,
  281. fromMap,
  282. maybeAddMapping,
  283. maybeAddSegment,
  284. setIgnore,
  285. setSourceContent,
  286. toDecodedMap,
  287. toEncodedMap
  288. };
  289. //# sourceMappingURL=gen-mapping.mjs.map