streamChunksOfRawSource.js 866 B

12345678910111213141516171819202122232425262728293031323334
  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. const streamChunksOfRawSource = (source, onChunk, onSource, onName) => {
  9. let line = 1;
  10. const matches = splitIntoLines(source);
  11. let match;
  12. for (match of matches) {
  13. onChunk(match, line, 0, -1, -1, -1, -1);
  14. line++;
  15. }
  16. return matches.length === 0 || match.endsWith("\n")
  17. ? {
  18. generatedLine: matches.length + 1,
  19. generatedColumn: 0
  20. }
  21. : {
  22. generatedLine: matches.length,
  23. generatedColumn: match.length
  24. };
  25. };
  26. module.exports = (source, onChunk, onSource, onName, finalSource) => {
  27. return finalSource
  28. ? getGeneratedSourceInfo(source)
  29. : streamChunksOfRawSource(source, onChunk, onSource, onName);
  30. };