getGeneratedSourceInfo.js 692 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const CHAR_CODE_NEW_LINE = "\n".charCodeAt(0);
  7. const getGeneratedSourceInfo = source => {
  8. if (source === undefined) {
  9. return {};
  10. }
  11. const lastLineStart = source.lastIndexOf("\n");
  12. if (lastLineStart === -1) {
  13. return {
  14. generatedLine: 1,
  15. generatedColumn: source.length,
  16. source
  17. };
  18. }
  19. let generatedLine = 2;
  20. for (let i = 0; i < lastLineStart; i++) {
  21. if (source.charCodeAt(i) === CHAR_CODE_NEW_LINE) generatedLine++;
  22. }
  23. return {
  24. generatedLine,
  25. generatedColumn: source.length - lastLineStart - 1,
  26. source
  27. };
  28. };
  29. module.exports = getGeneratedSourceInfo;