streamChunksOfRawSource.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const getGeneratedSourceInfo = require("./getGeneratedSourceInfo");
  7. const splitIntoLines = require("./splitIntoLines");
  8. /** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  9. /** @typedef {import("./streamChunks").OnChunk} OnChunk */
  10. /** @typedef {import("./streamChunks").OnName} OnName */
  11. /** @typedef {import("./streamChunks").OnSource} OnSource */
  12. /**
  13. * @param {string} source source
  14. * @param {OnChunk} onChunk on chunk
  15. * @param {OnSource} _onSource on source
  16. * @param {OnName} _onName on name
  17. * @returns {GeneratedSourceInfo} source info
  18. */
  19. const streamChunksOfRawSource = (source, onChunk, _onSource, _onName) => {
  20. let line = 1;
  21. const matches = splitIntoLines(source);
  22. /** @type {undefined | string} */
  23. let match;
  24. for (match of matches) {
  25. onChunk(match, line, 0, -1, -1, -1, -1);
  26. line++;
  27. }
  28. return matches.length === 0 || /** @type {string} */ (match).endsWith("\n")
  29. ? {
  30. generatedLine: matches.length + 1,
  31. generatedColumn: 0,
  32. }
  33. : {
  34. generatedLine: matches.length,
  35. generatedColumn: /** @type {string} */ (match).length,
  36. };
  37. };
  38. /**
  39. * @param {string} source source
  40. * @param {OnChunk} onChunk on chunk
  41. * @param {OnSource} onSource on source
  42. * @param {OnName} onName on name
  43. * @param {boolean} finalSource is final source
  44. * @returns {GeneratedSourceInfo} source info
  45. */
  46. module.exports = (source, onChunk, onSource, onName, finalSource) =>
  47. finalSource
  48. ? getGeneratedSourceInfo(source)
  49. : streamChunksOfRawSource(source, onChunk, onSource, onName);