gen-mapping.mjs 5.6 KB

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