You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SingleLineNode.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const base64VLQ = require("./base64-vlq");
  7. const getNumberOfLines = require("./helpers").getNumberOfLines;
  8. const getUnfinishedLine = require("./helpers").getUnfinishedLine;
  9. const LINE_MAPPING = ";AAAA";
  10. class SingleLineNode {
  11. constructor(generatedCode, source, originalSource, line) {
  12. this.generatedCode = generatedCode;
  13. this.originalSource = originalSource;
  14. this.source = source;
  15. this.line = line || 1;
  16. this._numberOfLines = getNumberOfLines(this.generatedCode);
  17. this._endsWithNewLine = generatedCode[generatedCode.length - 1] === "\n";
  18. }
  19. clone() {
  20. return new SingleLineNode(this.generatedCode, this.source, this.originalSource, this.line);
  21. }
  22. getGeneratedCode() {
  23. return this.generatedCode;
  24. }
  25. getMappings(mappingsContext) {
  26. if(!this.generatedCode)
  27. return "";
  28. const lines = this._numberOfLines;
  29. const sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource);
  30. let mappings = "A"; // generated column 0
  31. if(mappingsContext.unfinishedGeneratedLine)
  32. mappings = "," + base64VLQ.encode(mappingsContext.unfinishedGeneratedLine);
  33. mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index
  34. mappings += base64VLQ.encode(this.line - mappingsContext.currentOriginalLine); // original line index
  35. mappings += "A"; // original column 0
  36. mappingsContext.currentSource = sourceIdx;
  37. mappingsContext.currentOriginalLine = this.line;
  38. const unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode)
  39. mappings += Array(lines).join(LINE_MAPPING);
  40. if(unfinishedGeneratedLine === 0) {
  41. mappings += ";";
  42. } else {
  43. if(lines !== 0)
  44. mappings += LINE_MAPPING;
  45. }
  46. return mappings;
  47. }
  48. getNormalizedNodes() {
  49. return [this];
  50. }
  51. mapGeneratedCode(fn) {
  52. const generatedCode = fn(this.generatedCode);
  53. return new SingleLineNode(generatedCode, this.source, this.originalSource, this.line);
  54. }
  55. merge(otherNode) {
  56. if(otherNode instanceof SingleLineNode) {
  57. return this.mergeSingleLineNode(otherNode);
  58. }
  59. return false;
  60. }
  61. mergeSingleLineNode(otherNode) {
  62. if(this.source === otherNode.source &&
  63. this.originalSource === otherNode.originalSource) {
  64. if(this.line === otherNode.line) {
  65. this.generatedCode += otherNode.generatedCode;
  66. this._numberOfLines += otherNode._numberOfLines;
  67. this._endsWithNewLine = otherNode._endsWithNewLine;
  68. return this;
  69. } else if(this.line + 1 === otherNode.line &&
  70. this._endsWithNewLine &&
  71. this._numberOfLines === 1 &&
  72. otherNode._numberOfLines <= 1) {
  73. return new SourceNode(this.generatedCode + otherNode.generatedCode, this.source, this.originalSource, this.line);
  74. }
  75. }
  76. return false;
  77. }
  78. }
  79. module.exports = SingleLineNode;
  80. const SourceNode = require("./SourceNode"); // circular dependency