ReplaceSource.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. var Source = require("./Source");
  7. var SourceNode = require("source-map").SourceNode;
  8. var SourceListMap = require("source-list-map").SourceListMap;
  9. var fromStringWithSourceMap = require("source-list-map").fromStringWithSourceMap;
  10. var SourceMapConsumer = require("source-map").SourceMapConsumer;
  11. class Replacement {
  12. constructor(start, end, content, insertIndex, name) {
  13. this.start = start;
  14. this.end = end;
  15. this.content = content;
  16. this.insertIndex = insertIndex;
  17. this.name = name;
  18. }
  19. }
  20. class ReplaceSource extends Source {
  21. constructor(source, name) {
  22. super();
  23. this._source = source;
  24. this._name = name;
  25. /** @type {Replacement[]} */
  26. this.replacements = [];
  27. }
  28. replace(start, end, newValue, name) {
  29. if(typeof newValue !== "string")
  30. throw new Error("insertion must be a string, but is a " + typeof newValue);
  31. this.replacements.push(new Replacement(start, end, newValue, this.replacements.length, name));
  32. }
  33. insert(pos, newValue, name) {
  34. if(typeof newValue !== "string")
  35. throw new Error("insertion must be a string, but is a " + typeof newValue + ": " + newValue);
  36. this.replacements.push(new Replacement(pos, pos - 1, newValue, this.replacements.length, name));
  37. }
  38. source(options) {
  39. return this._replaceString(this._source.source());
  40. }
  41. original() {
  42. return this._source;
  43. }
  44. _sortReplacements() {
  45. this.replacements.sort(function(a, b) {
  46. var diff = b.end - a.end;
  47. if(diff !== 0)
  48. return diff;
  49. diff = b.start - a.start;
  50. if(diff !== 0)
  51. return diff;
  52. return b.insertIndex - a.insertIndex;
  53. });
  54. }
  55. _replaceString(str) {
  56. if(typeof str !== "string")
  57. throw new Error("str must be a string, but is a " + typeof str + ": " + str);
  58. this._sortReplacements();
  59. var result = [str];
  60. this.replacements.forEach(function(repl) {
  61. var remSource = result.pop();
  62. var splitted1 = this._splitString(remSource, Math.floor(repl.end + 1));
  63. var splitted2 = this._splitString(splitted1[0], Math.floor(repl.start));
  64. result.push(splitted1[1], repl.content, splitted2[0]);
  65. }, this);
  66. // write out result array in reverse order
  67. let resultStr = "";
  68. for(let i = result.length - 1; i >= 0; --i) {
  69. resultStr += result[i];
  70. }
  71. return resultStr;
  72. }
  73. node(options) {
  74. var node = this._source.node(options);
  75. if(this.replacements.length === 0) {
  76. return node;
  77. }
  78. this._sortReplacements();
  79. var replace = new ReplacementEnumerator(this.replacements);
  80. var output = [];
  81. var position = 0;
  82. var sources = Object.create(null);
  83. var sourcesInLines = Object.create(null);
  84. // We build a new list of SourceNodes in "output"
  85. // from the original mapping data
  86. var result = new SourceNode();
  87. // We need to add source contents manually
  88. // because "walk" will not handle it
  89. node.walkSourceContents(function(sourceFile, sourceContent) {
  90. result.setSourceContent(sourceFile, sourceContent);
  91. sources["$" + sourceFile] = sourceContent;
  92. });
  93. var replaceInStringNode = this._replaceInStringNode.bind(this, output, replace, function getOriginalSource(mapping) {
  94. var key = "$" + mapping.source;
  95. var lines = sourcesInLines[key];
  96. if(!lines) {
  97. var source = sources[key];
  98. if(!source) return null;
  99. lines = source.split("\n").map(function(line) {
  100. return line + "\n";
  101. });
  102. sourcesInLines[key] = lines;
  103. }
  104. // line is 1-based
  105. if(mapping.line > lines.length) return null;
  106. var line = lines[mapping.line - 1];
  107. return line.substr(mapping.column);
  108. });
  109. node.walk(function(chunk, mapping) {
  110. position = replaceInStringNode(chunk, position, mapping);
  111. });
  112. // If any replacements occur after the end of the original file, then we append them
  113. // directly to the end of the output
  114. var remaining = replace.footer();
  115. if(remaining) {
  116. output.push(remaining);
  117. }
  118. result.add(output);
  119. return result;
  120. }
  121. listMap(options) {
  122. this._sortReplacements();
  123. var map = this._source.listMap(options);
  124. var currentIndex = 0;
  125. var replacements = this.replacements;
  126. var idxReplacement = replacements.length - 1;
  127. var removeChars = 0;
  128. map = map.mapGeneratedCode(function(str) {
  129. var newCurrentIndex = currentIndex + str.length;
  130. if(removeChars > str.length) {
  131. removeChars -= str.length;
  132. str = "";
  133. } else {
  134. if(removeChars > 0) {
  135. str = str.substr(removeChars);
  136. currentIndex += removeChars;
  137. removeChars = 0;
  138. }
  139. var finalStr = "";
  140. while(idxReplacement >= 0 && replacements[idxReplacement].start < newCurrentIndex) {
  141. var repl = replacements[idxReplacement];
  142. var start = Math.floor(repl.start);
  143. var end = Math.floor(repl.end + 1);
  144. var before = str.substr(0, Math.max(0, start - currentIndex));
  145. if(end <= newCurrentIndex) {
  146. var after = str.substr(Math.max(0, end - currentIndex));
  147. finalStr += before + repl.content;
  148. str = after;
  149. currentIndex = Math.max(currentIndex, end);
  150. } else {
  151. finalStr += before + repl.content;
  152. str = "";
  153. removeChars = end - newCurrentIndex;
  154. }
  155. idxReplacement--;
  156. }
  157. str = finalStr + str;
  158. }
  159. currentIndex = newCurrentIndex;
  160. return str;
  161. });
  162. var extraCode = "";
  163. while(idxReplacement >= 0) {
  164. extraCode += replacements[idxReplacement].content;
  165. idxReplacement--;
  166. }
  167. if(extraCode) {
  168. map.add(extraCode);
  169. }
  170. return map;
  171. }
  172. _splitString(str, position) {
  173. return position <= 0 ? ["", str] : [str.substr(0, position), str.substr(position)];
  174. }
  175. _replaceInStringNode(output, replace, getOriginalSource, node, position, mapping) {
  176. var original = undefined;
  177. do {
  178. var splitPosition = replace.position - position;
  179. // If multiple replaces occur in the same location then the splitPosition may be
  180. // before the current position for the subsequent splits. Ensure it is >= 0
  181. if(splitPosition < 0) {
  182. splitPosition = 0;
  183. }
  184. if(splitPosition >= node.length || replace.done) {
  185. if(replace.emit) {
  186. var nodeEnd = new SourceNode(
  187. mapping.line,
  188. mapping.column,
  189. mapping.source,
  190. node,
  191. mapping.name
  192. );
  193. output.push(nodeEnd);
  194. }
  195. return position + node.length;
  196. }
  197. var originalColumn = mapping.column;
  198. // Try to figure out if generated code matches original code of this segement
  199. // If this is the case we assume that it's allowed to move mapping.column
  200. // Because getOriginalSource can be expensive we only do it when neccessary
  201. var nodePart;
  202. if(splitPosition > 0) {
  203. nodePart = node.slice(0, splitPosition);
  204. if(original === undefined) {
  205. original = getOriginalSource(mapping);
  206. }
  207. if(original && original.length >= splitPosition && original.startsWith(nodePart)) {
  208. mapping.column += splitPosition;
  209. original = original.substr(splitPosition);
  210. }
  211. }
  212. var emit = replace.next();
  213. if(!emit) {
  214. // Stop emitting when we have found the beginning of the string to replace.
  215. // Emit the part of the string before splitPosition
  216. if(splitPosition > 0) {
  217. var nodeStart = new SourceNode(
  218. mapping.line,
  219. originalColumn,
  220. mapping.source,
  221. nodePart,
  222. mapping.name
  223. );
  224. output.push(nodeStart);
  225. }
  226. // Emit the replacement value
  227. if(replace.value) {
  228. output.push(new SourceNode(
  229. mapping.line,
  230. mapping.column,
  231. mapping.source,
  232. replace.value,
  233. mapping.name || replace.name
  234. ));
  235. }
  236. }
  237. // Recurse with remainder of the string as there may be multiple replaces within a single node
  238. node = node.substr(splitPosition);
  239. position += splitPosition;
  240. } while (true);
  241. }
  242. }
  243. class ReplacementEnumerator {
  244. /**
  245. * @param {Replacement[]} replacements list of replacements
  246. */
  247. constructor(replacements) {
  248. this.replacements = replacements || [];
  249. this.index = this.replacements.length;
  250. this.done = false;
  251. this.emit = false;
  252. // Set initial start position
  253. this.next();
  254. }
  255. next() {
  256. if(this.done)
  257. return true;
  258. if(this.emit) {
  259. // Start point found. stop emitting. set position to find end
  260. var repl = this.replacements[this.index];
  261. var end = Math.floor(repl.end + 1);
  262. this.position = end;
  263. this.value = repl.content;
  264. this.name = repl.name;
  265. } else {
  266. // End point found. start emitting. set position to find next start
  267. this.index--;
  268. if(this.index < 0) {
  269. this.done = true;
  270. } else {
  271. var nextRepl = this.replacements[this.index];
  272. var start = Math.floor(nextRepl.start);
  273. this.position = start;
  274. }
  275. }
  276. if(this.position < 0)
  277. this.position = 0;
  278. this.emit = !this.emit;
  279. return this.emit;
  280. }
  281. footer() {
  282. if(!this.done && !this.emit)
  283. this.next(); // If we finished _replaceInNode mid emit we advance to next entry
  284. if(this.done) {
  285. return [];
  286. } else {
  287. var resultStr = "";
  288. for(var i = this.index; i >= 0; i--) {
  289. var repl = this.replacements[i];
  290. // this doesn't need to handle repl.name, because in SourceMaps generated code
  291. // without pointer to original source can't have a name
  292. resultStr += repl.content;
  293. }
  294. return resultStr;
  295. }
  296. }
  297. }
  298. require("./SourceAndMapMixin")(ReplaceSource.prototype);
  299. module.exports = ReplaceSource;