streamChunksOfCombinedSourceMap.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const streamChunksOfSourceMap = require("./streamChunksOfSourceMap");
  7. const splitIntoLines = require("./splitIntoLines");
  8. const streamChunksOfCombinedSourceMap = (
  9. source,
  10. sourceMap,
  11. innerSourceName,
  12. innerSource,
  13. innerSourceMap,
  14. removeInnerSource,
  15. onChunk,
  16. onSource,
  17. onName,
  18. finalSource,
  19. columns
  20. ) => {
  21. let sourceMapping = new Map();
  22. let nameMapping = new Map();
  23. const sourceIndexMapping = [];
  24. const nameIndexMapping = [];
  25. const nameIndexValueMapping = [];
  26. let innerSourceIndex = -2;
  27. const innerSourceIndexMapping = [];
  28. const innerSourceIndexValueMapping = [];
  29. const innerSourceContents = [];
  30. const innerSourceContentLines = [];
  31. const innerNameIndexMapping = [];
  32. const innerNameIndexValueMapping = [];
  33. const innerSourceMapLineData = [];
  34. const findInnerMapping = (line, column) => {
  35. if (line > innerSourceMapLineData.length) return -1;
  36. const { mappingsData } = innerSourceMapLineData[line - 1];
  37. let l = 0;
  38. let r = mappingsData.length / 5;
  39. while (l < r) {
  40. let m = (l + r) >> 1;
  41. if (mappingsData[m * 5] <= column) {
  42. l = m + 1;
  43. } else {
  44. r = m;
  45. }
  46. }
  47. if (l === 0) return -1;
  48. return l - 1;
  49. };
  50. return streamChunksOfSourceMap(
  51. source,
  52. sourceMap,
  53. (
  54. chunk,
  55. generatedLine,
  56. generatedColumn,
  57. sourceIndex,
  58. originalLine,
  59. originalColumn,
  60. nameIndex
  61. ) => {
  62. // Check if this is a mapping to the inner source
  63. if (sourceIndex === innerSourceIndex) {
  64. // Check if there is a mapping in the inner source
  65. const idx = findInnerMapping(originalLine, originalColumn);
  66. if (idx !== -1) {
  67. const { chunks, mappingsData } = innerSourceMapLineData[
  68. originalLine - 1
  69. ];
  70. const mi = idx * 5;
  71. const innerSourceIndex = mappingsData[mi + 1];
  72. const innerOriginalLine = mappingsData[mi + 2];
  73. let innerOriginalColumn = mappingsData[mi + 3];
  74. let innerNameIndex = mappingsData[mi + 4];
  75. if (innerSourceIndex >= 0) {
  76. // Check for an identity mapping
  77. // where we are allowed to adjust the original column
  78. const innerChunk = chunks[idx];
  79. const innerGeneratedColumn = mappingsData[mi];
  80. const locationInChunk = originalColumn - innerGeneratedColumn;
  81. if (locationInChunk > 0) {
  82. let originalSourceLines =
  83. innerSourceIndex < innerSourceContentLines.length
  84. ? innerSourceContentLines[innerSourceIndex]
  85. : null;
  86. if (originalSourceLines === undefined) {
  87. const originalSource = innerSourceContents[innerSourceIndex];
  88. originalSourceLines = originalSource
  89. ? splitIntoLines(originalSource)
  90. : null;
  91. innerSourceContentLines[innerSourceIndex] = originalSourceLines;
  92. }
  93. if (originalSourceLines !== null) {
  94. const originalChunk =
  95. innerOriginalLine <= originalSourceLines.length
  96. ? originalSourceLines[innerOriginalLine - 1].slice(
  97. innerOriginalColumn,
  98. innerOriginalColumn + locationInChunk
  99. )
  100. : "";
  101. if (innerChunk.slice(0, locationInChunk) === originalChunk) {
  102. innerOriginalColumn += locationInChunk;
  103. innerNameIndex = -1;
  104. }
  105. }
  106. }
  107. // We have a inner mapping to original source
  108. // emit source when needed and compute global source index
  109. let sourceIndex =
  110. innerSourceIndex < innerSourceIndexMapping.length
  111. ? innerSourceIndexMapping[innerSourceIndex]
  112. : -2;
  113. if (sourceIndex === -2) {
  114. const [source, sourceContent] =
  115. innerSourceIndex < innerSourceIndexValueMapping.length
  116. ? innerSourceIndexValueMapping[innerSourceIndex]
  117. : [null, undefined];
  118. let globalIndex = sourceMapping.get(source);
  119. if (globalIndex === undefined) {
  120. sourceMapping.set(source, (globalIndex = sourceMapping.size));
  121. onSource(globalIndex, source, sourceContent);
  122. }
  123. sourceIndex = globalIndex;
  124. innerSourceIndexMapping[innerSourceIndex] = sourceIndex;
  125. }
  126. // emit name when needed and compute global name index
  127. let finalNameIndex = -1;
  128. if (innerNameIndex >= 0) {
  129. // when we have a inner name
  130. finalNameIndex =
  131. innerNameIndex < innerNameIndexMapping.length
  132. ? innerNameIndexMapping[innerNameIndex]
  133. : -2;
  134. if (finalNameIndex === -2) {
  135. const name =
  136. innerNameIndex < innerNameIndexValueMapping.length
  137. ? innerNameIndexValueMapping[innerNameIndex]
  138. : undefined;
  139. if (name) {
  140. let globalIndex = nameMapping.get(name);
  141. if (globalIndex === undefined) {
  142. nameMapping.set(name, (globalIndex = nameMapping.size));
  143. onName(globalIndex, name);
  144. }
  145. finalNameIndex = globalIndex;
  146. } else {
  147. finalNameIndex = -1;
  148. }
  149. innerNameIndexMapping[innerNameIndex] = finalNameIndex;
  150. }
  151. } else if (nameIndex >= 0) {
  152. // when we don't have an inner name,
  153. // but we have an outer name
  154. // it can be used when inner original code equals to the name
  155. let originalSourceLines =
  156. innerSourceContentLines[innerSourceIndex];
  157. if (originalSourceLines === undefined) {
  158. const originalSource = innerSourceContents[innerSourceIndex];
  159. originalSourceLines = originalSource
  160. ? splitIntoLines(originalSource)
  161. : null;
  162. innerSourceContentLines[innerSourceIndex] = originalSourceLines;
  163. }
  164. if (originalSourceLines !== null) {
  165. const name = nameIndexValueMapping[nameIndex];
  166. const originalName =
  167. innerOriginalLine <= originalSourceLines.length
  168. ? originalSourceLines[innerOriginalLine - 1].slice(
  169. innerOriginalColumn,
  170. innerOriginalColumn + name.length
  171. )
  172. : "";
  173. if (name === originalName) {
  174. finalNameIndex =
  175. nameIndex < nameIndexMapping.length
  176. ? nameIndexMapping[nameIndex]
  177. : -2;
  178. if (finalNameIndex === -2) {
  179. const name = nameIndexValueMapping[nameIndex];
  180. if (name) {
  181. let globalIndex = nameMapping.get(name);
  182. if (globalIndex === undefined) {
  183. nameMapping.set(name, (globalIndex = nameMapping.size));
  184. onName(globalIndex, name);
  185. }
  186. finalNameIndex = globalIndex;
  187. } else {
  188. finalNameIndex = -1;
  189. }
  190. nameIndexMapping[nameIndex] = finalNameIndex;
  191. }
  192. }
  193. }
  194. }
  195. onChunk(
  196. chunk,
  197. generatedLine,
  198. generatedColumn,
  199. sourceIndex,
  200. innerOriginalLine,
  201. innerOriginalColumn,
  202. finalNameIndex
  203. );
  204. return;
  205. }
  206. }
  207. // We have a mapping to the inner source, but no inner mapping
  208. if (removeInnerSource) {
  209. onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
  210. return;
  211. } else {
  212. if (sourceIndexMapping[sourceIndex] === -2) {
  213. let globalIndex = sourceMapping.get(innerSourceName);
  214. if (globalIndex === undefined) {
  215. sourceMapping.set(source, (globalIndex = sourceMapping.size));
  216. onSource(globalIndex, innerSourceName, innerSource);
  217. }
  218. sourceIndexMapping[sourceIndex] = globalIndex;
  219. }
  220. }
  221. }
  222. const finalSourceIndex =
  223. sourceIndex < 0 || sourceIndex >= sourceIndexMapping.length
  224. ? -1
  225. : sourceIndexMapping[sourceIndex];
  226. if (finalSourceIndex < 0) {
  227. // no source, so we make it a generated chunk
  228. onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
  229. } else {
  230. // Pass through the chunk with mapping
  231. let finalNameIndex = -1;
  232. if (nameIndex >= 0 && nameIndex < nameIndexMapping.length) {
  233. finalNameIndex = nameIndexMapping[nameIndex];
  234. if (finalNameIndex === -2) {
  235. const name = nameIndexValueMapping[nameIndex];
  236. let globalIndex = nameMapping.get(name);
  237. if (globalIndex === undefined) {
  238. nameMapping.set(name, (globalIndex = nameMapping.size));
  239. onName(globalIndex, name);
  240. }
  241. finalNameIndex = globalIndex;
  242. nameIndexMapping[nameIndex] = finalNameIndex;
  243. }
  244. }
  245. onChunk(
  246. chunk,
  247. generatedLine,
  248. generatedColumn,
  249. finalSourceIndex,
  250. originalLine,
  251. originalColumn,
  252. finalNameIndex
  253. );
  254. }
  255. },
  256. (i, source, sourceContent) => {
  257. if (source === innerSourceName) {
  258. innerSourceIndex = i;
  259. if (innerSource !== undefined) sourceContent = innerSource;
  260. else innerSource = sourceContent;
  261. sourceIndexMapping[i] = -2;
  262. streamChunksOfSourceMap(
  263. sourceContent,
  264. innerSourceMap,
  265. (
  266. chunk,
  267. generatedLine,
  268. generatedColumn,
  269. sourceIndex,
  270. originalLine,
  271. originalColumn,
  272. nameIndex
  273. ) => {
  274. while (innerSourceMapLineData.length < generatedLine) {
  275. innerSourceMapLineData.push({
  276. mappingsData: [],
  277. chunks: []
  278. });
  279. }
  280. const data = innerSourceMapLineData[generatedLine - 1];
  281. data.mappingsData.push(
  282. generatedColumn,
  283. sourceIndex,
  284. originalLine,
  285. originalColumn,
  286. nameIndex
  287. );
  288. data.chunks.push(chunk);
  289. },
  290. (i, source, sourceContent) => {
  291. innerSourceContents[i] = sourceContent;
  292. innerSourceContentLines[i] = undefined;
  293. innerSourceIndexMapping[i] = -2;
  294. innerSourceIndexValueMapping[i] = [source, sourceContent];
  295. },
  296. (i, name) => {
  297. innerNameIndexMapping[i] = -2;
  298. innerNameIndexValueMapping[i] = name;
  299. },
  300. false,
  301. columns
  302. );
  303. } else {
  304. let globalIndex = sourceMapping.get(source);
  305. if (globalIndex === undefined) {
  306. sourceMapping.set(source, (globalIndex = sourceMapping.size));
  307. onSource(globalIndex, source, sourceContent);
  308. }
  309. sourceIndexMapping[i] = globalIndex;
  310. }
  311. },
  312. (i, name) => {
  313. nameIndexMapping[i] = -2;
  314. nameIndexValueMapping[i] = name;
  315. },
  316. finalSource,
  317. columns
  318. );
  319. };
  320. module.exports = streamChunksOfCombinedSourceMap;