ReplaceSource.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Source = require("./Source");
  7. const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
  8. const splitIntoLines = require("./helpers/splitIntoLines");
  9. const streamChunks = require("./helpers/streamChunks");
  10. /** @typedef {import("./Source").HashLike} HashLike */
  11. /** @typedef {import("./Source").MapOptions} MapOptions */
  12. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  13. /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
  14. /** @typedef {import("./Source").SourceValue} SourceValue */
  15. /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  16. /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
  17. /** @typedef {import("./helpers/streamChunks").OnName} OnName */
  18. /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
  19. /** @typedef {import("./helpers/streamChunks").Options} Options */
  20. // since v8 7.0, Array.prototype.sort is stable
  21. const hasStableSort =
  22. typeof process === "object" &&
  23. process.versions &&
  24. typeof process.versions.v8 === "string" &&
  25. !/^[0-6]\./.test(process.versions.v8);
  26. // This is larger than max string length
  27. const MAX_SOURCE_POSITION = 0x20000000;
  28. /**
  29. * Stable comparator hoisted to module scope so each `_sortReplacements()`
  30. * call doesn't allocate a fresh closure.
  31. * @param {Replacement} a a
  32. * @param {Replacement} b b
  33. * @returns {number} order
  34. */
  35. const compareStable = (a, b) => {
  36. const diff1 = a.start - b.start;
  37. if (diff1 !== 0) return diff1;
  38. const diff2 = a.end - b.end;
  39. if (diff2 !== 0) return diff2;
  40. return 0;
  41. };
  42. /**
  43. * Index-stabilising comparator for v8 < 7.0 (pre-stable Array.prototype.sort).
  44. * @param {Replacement} a a
  45. * @param {Replacement} b b
  46. * @returns {number} order
  47. */
  48. const compareUnstableFallback = (a, b) => {
  49. const diff1 = a.start - b.start;
  50. if (diff1 !== 0) return diff1;
  51. const diff2 = a.end - b.end;
  52. if (diff2 !== 0) return diff2;
  53. return /** @type {number} */ (a.index) - /** @type {number} */ (b.index);
  54. };
  55. class Replacement {
  56. /**
  57. * @param {number} start start
  58. * @param {number} end end
  59. * @param {string} content content
  60. * @param {string=} name name
  61. */
  62. constructor(start, end, content, name) {
  63. this.start = start;
  64. this.end = end;
  65. this.content = content;
  66. this.name = name;
  67. if (!hasStableSort) {
  68. this.index = -1;
  69. }
  70. }
  71. }
  72. class ReplaceSource extends Source {
  73. /**
  74. * @param {Source} source source
  75. * @param {string=} name name
  76. */
  77. constructor(source, name) {
  78. super();
  79. /**
  80. * @private
  81. * @type {Source}
  82. */
  83. this._source = source;
  84. /**
  85. * @private
  86. * @type {string | undefined}
  87. */
  88. this._name = name;
  89. /** @type {Replacement[]} */
  90. this._replacements = [];
  91. /**
  92. * @private
  93. * @type {boolean}
  94. */
  95. this._isSorted = true;
  96. }
  97. getName() {
  98. return this._name;
  99. }
  100. getReplacements() {
  101. this._sortReplacements();
  102. return this._replacements;
  103. }
  104. /**
  105. * @param {number} start start
  106. * @param {number} end end
  107. * @param {string} newValue new value
  108. * @param {string=} name name
  109. * @returns {void}
  110. */
  111. replace(start, end, newValue, name) {
  112. if (typeof newValue !== "string") {
  113. throw new Error(
  114. `insertion must be a string, but is a ${typeof newValue}`,
  115. );
  116. }
  117. this._replacements.push(new Replacement(start, end, newValue, name));
  118. this._isSorted = false;
  119. }
  120. /**
  121. * @param {number} pos pos
  122. * @param {string} newValue new value
  123. * @param {string=} name name
  124. * @returns {void}
  125. */
  126. insert(pos, newValue, name) {
  127. if (typeof newValue !== "string") {
  128. throw new Error(
  129. `insertion must be a string, but is a ${typeof newValue}: ${newValue}`,
  130. );
  131. }
  132. this._replacements.push(new Replacement(pos, pos - 1, newValue, name));
  133. this._isSorted = false;
  134. }
  135. /**
  136. * @returns {SourceValue} source
  137. */
  138. source() {
  139. if (this._replacements.length === 0) {
  140. return this._source.source();
  141. }
  142. const current = /** @type {string} */ (this._source.source());
  143. let pos = 0;
  144. const result = [];
  145. this._sortReplacements();
  146. for (const replacement of this._replacements) {
  147. const start = Math.floor(replacement.start);
  148. const end = Math.floor(replacement.end + 1);
  149. if (pos < start) {
  150. // slice directly from the original string rather than repeatedly
  151. // producing smaller intermediate strings, which avoids O(n) copies.
  152. result.push(current.slice(pos, start));
  153. pos = start;
  154. }
  155. result.push(replacement.content);
  156. if (pos < end) {
  157. pos = end;
  158. }
  159. }
  160. if (pos < current.length) {
  161. result.push(pos === 0 ? current : current.slice(pos));
  162. }
  163. return result.join("");
  164. }
  165. /**
  166. * @returns {Buffer} buffer
  167. */
  168. buffer() {
  169. if (this._replacements.length === 0) {
  170. return this._source.buffer();
  171. }
  172. return super.buffer();
  173. }
  174. /**
  175. * @returns {Buffer[]} buffers
  176. */
  177. buffers() {
  178. if (this._replacements.length === 0) {
  179. // TODO remove in the next major release
  180. return typeof this._source.buffers === "function"
  181. ? this._source.buffers()
  182. : [this._source.buffer()];
  183. }
  184. return [this.buffer()];
  185. }
  186. /**
  187. * @param {MapOptions=} options map options
  188. * @returns {RawSourceMap | null} map
  189. */
  190. map(options) {
  191. if (this._replacements.length === 0) {
  192. return this._source.map(options);
  193. }
  194. return getMap(this, options);
  195. }
  196. /**
  197. * @param {MapOptions=} options map options
  198. * @returns {SourceAndMap} source and map
  199. */
  200. sourceAndMap(options) {
  201. if (this._replacements.length === 0) {
  202. return this._source.sourceAndMap(options);
  203. }
  204. return getSourceAndMap(this, options);
  205. }
  206. original() {
  207. return this._source;
  208. }
  209. _sortReplacements() {
  210. if (this._isSorted) return;
  211. if (hasStableSort) {
  212. this._replacements.sort(compareStable);
  213. } else {
  214. for (const [i, repl] of this._replacements.entries()) repl.index = i;
  215. this._replacements.sort(compareUnstableFallback);
  216. }
  217. this._isSorted = true;
  218. }
  219. /**
  220. * @param {Options} options options
  221. * @param {OnChunk} onChunk called for each chunk of code
  222. * @param {OnSource} onSource called for each source
  223. * @param {OnName} onName called for each name
  224. * @returns {GeneratedSourceInfo} generated source info
  225. */
  226. streamChunks(options, onChunk, onSource, onName) {
  227. this._sortReplacements();
  228. const replacements = this._replacements;
  229. let pos = 0;
  230. let i = 0;
  231. let replacementEnd = -1;
  232. let nextReplacement =
  233. i < replacements.length
  234. ? Math.floor(replacements[i].start)
  235. : MAX_SOURCE_POSITION;
  236. let generatedLineOffset = 0;
  237. let generatedColumnOffset = 0;
  238. let generatedColumnOffsetLine = 0;
  239. /** @type {(string | string[] | undefined)[]} */
  240. const sourceContents = [];
  241. /** @type {Map<string, number>} */
  242. const nameMapping = new Map();
  243. /** @type {number[]} */
  244. const nameIndexMapping = [];
  245. /**
  246. * @param {number} sourceIndex source index
  247. * @param {number} line line
  248. * @param {number} column column
  249. * @param {string} expectedChunk expected chunk
  250. * @returns {boolean} result
  251. */
  252. const checkOriginalContent = (sourceIndex, line, column, expectedChunk) => {
  253. /** @type {undefined | string | string[]} */
  254. let content =
  255. sourceIndex < sourceContents.length
  256. ? sourceContents[sourceIndex]
  257. : undefined;
  258. if (content === undefined) return false;
  259. if (typeof content === "string") {
  260. content = splitIntoLines(content);
  261. sourceContents[sourceIndex] = content;
  262. }
  263. const contentLine = line <= content.length ? content[line - 1] : null;
  264. if (contentLine === null) return false;
  265. return (
  266. contentLine.slice(column, column + expectedChunk.length) ===
  267. expectedChunk
  268. );
  269. };
  270. const { generatedLine, generatedColumn } = streamChunks(
  271. this._source,
  272. { ...options, finalSource: false },
  273. (
  274. _chunk,
  275. generatedLine,
  276. generatedColumn,
  277. sourceIndex,
  278. originalLine,
  279. originalColumn,
  280. nameIndex,
  281. ) => {
  282. let chunkPos = 0;
  283. const chunk = /** @type {string} */ (_chunk);
  284. const endPos = pos + chunk.length;
  285. // Skip over when it has been replaced
  286. if (replacementEnd > pos) {
  287. // Skip over the whole chunk
  288. if (replacementEnd >= endPos) {
  289. const line = generatedLine + generatedLineOffset;
  290. if (chunk.endsWith("\n")) {
  291. generatedLineOffset--;
  292. if (generatedColumnOffsetLine === line) {
  293. // undo exiting corrections form the current line
  294. generatedColumnOffset += generatedColumn;
  295. }
  296. } else if (generatedColumnOffsetLine === line) {
  297. generatedColumnOffset -= chunk.length;
  298. } else {
  299. generatedColumnOffset = -chunk.length;
  300. generatedColumnOffsetLine = line;
  301. }
  302. pos = endPos;
  303. return;
  304. }
  305. // Partially skip over chunk
  306. chunkPos = replacementEnd - pos;
  307. if (
  308. checkOriginalContent(
  309. sourceIndex,
  310. originalLine,
  311. originalColumn,
  312. chunk.slice(0, chunkPos),
  313. )
  314. ) {
  315. originalColumn += chunkPos;
  316. }
  317. pos += chunkPos;
  318. const line = generatedLine + generatedLineOffset;
  319. if (generatedColumnOffsetLine === line) {
  320. generatedColumnOffset -= chunkPos;
  321. } else {
  322. generatedColumnOffset = -chunkPos;
  323. generatedColumnOffsetLine = line;
  324. }
  325. generatedColumn += chunkPos;
  326. }
  327. // Is a replacement in the chunk?
  328. if (nextReplacement < endPos) {
  329. do {
  330. let line = generatedLine + generatedLineOffset;
  331. if (nextReplacement > pos) {
  332. // Emit chunk until replacement
  333. const offset = nextReplacement - pos;
  334. const chunkSlice = chunk.slice(chunkPos, chunkPos + offset);
  335. onChunk(
  336. chunkSlice,
  337. line,
  338. generatedColumn +
  339. (line === generatedColumnOffsetLine
  340. ? generatedColumnOffset
  341. : 0),
  342. sourceIndex,
  343. originalLine,
  344. originalColumn,
  345. nameIndex < 0 || nameIndex >= nameIndexMapping.length
  346. ? -1
  347. : nameIndexMapping[nameIndex],
  348. );
  349. generatedColumn += offset;
  350. chunkPos += offset;
  351. pos = nextReplacement;
  352. if (
  353. checkOriginalContent(
  354. sourceIndex,
  355. originalLine,
  356. originalColumn,
  357. chunkSlice,
  358. )
  359. ) {
  360. originalColumn += chunkSlice.length;
  361. }
  362. }
  363. // Insert replacement content splitted into chunks by lines
  364. const { content, name } = replacements[i];
  365. const matches = splitIntoLines(content);
  366. let replacementNameIndex = nameIndex;
  367. if (sourceIndex >= 0 && name) {
  368. let globalIndex = nameMapping.get(name);
  369. if (globalIndex === undefined) {
  370. globalIndex = nameMapping.size;
  371. nameMapping.set(name, globalIndex);
  372. onName(globalIndex, name);
  373. }
  374. replacementNameIndex = globalIndex;
  375. }
  376. for (let m = 0; m < matches.length; m++) {
  377. const contentLine = matches[m];
  378. onChunk(
  379. contentLine,
  380. line,
  381. generatedColumn +
  382. (line === generatedColumnOffsetLine
  383. ? generatedColumnOffset
  384. : 0),
  385. sourceIndex,
  386. originalLine,
  387. originalColumn,
  388. replacementNameIndex,
  389. );
  390. // Only the first chunk has name assigned
  391. replacementNameIndex = -1;
  392. if (m === matches.length - 1 && !contentLine.endsWith("\n")) {
  393. if (generatedColumnOffsetLine === line) {
  394. generatedColumnOffset += contentLine.length;
  395. } else {
  396. generatedColumnOffset = contentLine.length;
  397. generatedColumnOffsetLine = line;
  398. }
  399. } else {
  400. generatedLineOffset++;
  401. line++;
  402. generatedColumnOffset = -generatedColumn;
  403. generatedColumnOffsetLine = line;
  404. }
  405. }
  406. // Remove replaced content by settings this variable
  407. replacementEnd = Math.max(
  408. replacementEnd,
  409. Math.floor(replacements[i].end + 1),
  410. );
  411. // Move to next replacement
  412. i++;
  413. nextReplacement =
  414. i < replacements.length
  415. ? Math.floor(replacements[i].start)
  416. : MAX_SOURCE_POSITION;
  417. // Skip over when it has been replaced
  418. const offset = chunk.length - endPos + replacementEnd - chunkPos;
  419. if (offset > 0) {
  420. // Skip over whole chunk
  421. if (replacementEnd >= endPos) {
  422. const line = generatedLine + generatedLineOffset;
  423. if (chunk.endsWith("\n")) {
  424. generatedLineOffset--;
  425. if (generatedColumnOffsetLine === line) {
  426. // undo exiting corrections form the current line
  427. generatedColumnOffset += generatedColumn;
  428. }
  429. } else if (generatedColumnOffsetLine === line) {
  430. generatedColumnOffset -= chunk.length - chunkPos;
  431. } else {
  432. generatedColumnOffset = chunkPos - chunk.length;
  433. generatedColumnOffsetLine = line;
  434. }
  435. pos = endPos;
  436. return;
  437. }
  438. // Partially skip over chunk
  439. const line = generatedLine + generatedLineOffset;
  440. if (
  441. checkOriginalContent(
  442. sourceIndex,
  443. originalLine,
  444. originalColumn,
  445. chunk.slice(chunkPos, chunkPos + offset),
  446. )
  447. ) {
  448. originalColumn += offset;
  449. }
  450. chunkPos += offset;
  451. pos += offset;
  452. if (generatedColumnOffsetLine === line) {
  453. generatedColumnOffset -= offset;
  454. } else {
  455. generatedColumnOffset = -offset;
  456. generatedColumnOffsetLine = line;
  457. }
  458. generatedColumn += offset;
  459. }
  460. } while (nextReplacement < endPos);
  461. }
  462. // Emit remaining chunk
  463. if (chunkPos < chunk.length) {
  464. const chunkSlice = chunkPos === 0 ? chunk : chunk.slice(chunkPos);
  465. const line = generatedLine + generatedLineOffset;
  466. onChunk(
  467. chunkSlice,
  468. line,
  469. generatedColumn +
  470. (line === generatedColumnOffsetLine ? generatedColumnOffset : 0),
  471. sourceIndex,
  472. originalLine,
  473. originalColumn,
  474. nameIndex < 0 ? -1 : nameIndexMapping[nameIndex],
  475. );
  476. }
  477. pos = endPos;
  478. },
  479. (sourceIndex, source, sourceContent) => {
  480. while (sourceContents.length < sourceIndex) {
  481. sourceContents.push(undefined);
  482. }
  483. sourceContents[sourceIndex] = sourceContent;
  484. onSource(sourceIndex, source, sourceContent);
  485. },
  486. (nameIndex, name) => {
  487. let globalIndex = nameMapping.get(name);
  488. if (globalIndex === undefined) {
  489. globalIndex = nameMapping.size;
  490. nameMapping.set(name, globalIndex);
  491. onName(globalIndex, name);
  492. }
  493. nameIndexMapping[nameIndex] = globalIndex;
  494. },
  495. );
  496. // Handle remaining replacements
  497. let remainer = "";
  498. for (; i < replacements.length; i++) {
  499. remainer += replacements[i].content;
  500. }
  501. // Insert remaining replacements content splitted into chunks by lines
  502. let line = /** @type {number} */ (generatedLine) + generatedLineOffset;
  503. const matches = splitIntoLines(remainer);
  504. for (let m = 0; m < matches.length; m++) {
  505. const contentLine = matches[m];
  506. onChunk(
  507. contentLine,
  508. line,
  509. /** @type {number} */
  510. (generatedColumn) +
  511. (line === generatedColumnOffsetLine ? generatedColumnOffset : 0),
  512. -1,
  513. -1,
  514. -1,
  515. -1,
  516. );
  517. if (m === matches.length - 1 && !contentLine.endsWith("\n")) {
  518. if (generatedColumnOffsetLine === line) {
  519. generatedColumnOffset += contentLine.length;
  520. } else {
  521. generatedColumnOffset = contentLine.length;
  522. generatedColumnOffsetLine = line;
  523. }
  524. } else {
  525. generatedLineOffset++;
  526. line++;
  527. generatedColumnOffset = -(/** @type {number} */ (generatedColumn));
  528. generatedColumnOffsetLine = line;
  529. }
  530. }
  531. return {
  532. generatedLine: line,
  533. generatedColumn:
  534. /** @type {number} */
  535. (generatedColumn) +
  536. (line === generatedColumnOffsetLine ? generatedColumnOffset : 0),
  537. };
  538. }
  539. /**
  540. * @param {HashLike} hash hash
  541. * @returns {void}
  542. */
  543. updateHash(hash) {
  544. this._sortReplacements();
  545. hash.update("ReplaceSource");
  546. this._source.updateHash(hash);
  547. hash.update(this._name || "");
  548. // Feed each replacement as multiple updates instead of building one
  549. // combined template literal per replacement. The resulting digest is
  550. // identical (hash.update is additive over bytes), but we avoid
  551. // allocating a new string per replacement.
  552. for (const repl of this._replacements) {
  553. hash.update(`${repl.start}${repl.end}`);
  554. hash.update(repl.content);
  555. if (repl.name) hash.update(repl.name);
  556. }
  557. }
  558. }
  559. module.exports = ReplaceSource;
  560. module.exports.Replacement = Replacement;