SourceNode.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var base64VLQ = require("./base64-vlq");
  6. var getNumberOfLines = require("./helpers").getNumberOfLines;
  7. function SourceNode(generatedCode, source, originalSource, startingLine) {
  8. this.generatedCode = generatedCode;
  9. this.originalSource = originalSource;
  10. this.source = source;
  11. this.startingLine = startingLine || 1;
  12. }
  13. module.exports = SourceNode;
  14. SourceNode.prototype.clone = function() {
  15. return new SourceNode(this.generatedCode, this.source, this.originalSource, this.startingLine);
  16. }
  17. var LINE_MAPPING = "AACA;";
  18. var LAST_LINE_MAPPING = "AACA";
  19. SourceNode.prototype.getGeneratedCode = function() {
  20. return this.generatedCode;
  21. };
  22. SourceNode.prototype.getMappings = function(mappingsContext) {
  23. var lines = getNumberOfLines(this.generatedCode);
  24. var sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource);
  25. var mappings = "A"; // generated column 0
  26. mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index
  27. mappings += base64VLQ.encode(this.startingLine - mappingsContext.currentOriginalLine); // original line index
  28. mappings += "A"; // original column 0
  29. if(lines !== 0)
  30. mappings += ";"
  31. mappingsContext.currentSource = sourceIdx;
  32. mappingsContext.currentOriginalLine = (lines || 1) + this.startingLine - 1;
  33. mappings += Array(lines).join(LINE_MAPPING);
  34. if(lines !== 0 && this.generatedCode[this.generatedCode.length - 1] !== "\n") {
  35. mappings += LAST_LINE_MAPPING;
  36. mappingsContext.currentOriginalLine++;
  37. }
  38. return mappings;
  39. };
  40. SourceNode.prototype.mapGeneratedCode = function(fn) {
  41. this.generatedCode = fn(this.generatedCode);
  42. };