source: trip-planner-front/node_modules/source-list-map/lib/SourceNode.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const base64VLQ = require("./base64-vlq");
8const getNumberOfLines = require("./helpers").getNumberOfLines;
9const getUnfinishedLine = require("./helpers").getUnfinishedLine;
10
11const LINE_MAPPING = ";AACA";
12
13class SourceNode {
14
15 constructor(generatedCode, source, originalSource, startingLine) {
16 this.generatedCode = generatedCode;
17 this.originalSource = originalSource;
18 this.source = source;
19 this.startingLine = startingLine || 1;
20 this._numberOfLines = getNumberOfLines(this.generatedCode);
21 this._endsWithNewLine = generatedCode[generatedCode.length - 1] === "\n";
22 }
23
24 clone() {
25 return new SourceNode(this.generatedCode, this.source, this.originalSource, this.startingLine);
26 }
27
28 getGeneratedCode() {
29 return this.generatedCode;
30 }
31
32 addGeneratedCode(code) {
33 this.generatedCode += code;
34 this._numberOfLines += getNumberOfLines(code);
35 this._endsWithNewLine = code[code.length - 1] === "\n";
36 }
37
38 getMappings(mappingsContext) {
39 if(!this.generatedCode)
40 return "";
41 const lines = this._numberOfLines;
42 const sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource);
43 let mappings = "A"; // generated column 0
44 if(mappingsContext.unfinishedGeneratedLine)
45 mappings = "," + base64VLQ.encode(mappingsContext.unfinishedGeneratedLine);
46 mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index
47 mappings += base64VLQ.encode(this.startingLine - mappingsContext.currentOriginalLine); // original line index
48 mappings += "A"; // original column 0
49 mappingsContext.currentSource = sourceIdx;
50 mappingsContext.currentOriginalLine = this.startingLine + lines - 1;
51 const unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode)
52 mappings += Array(lines).join(LINE_MAPPING);
53 if(unfinishedGeneratedLine === 0) {
54 mappings += ";";
55 } else {
56 if(lines !== 0) {
57 mappings += LINE_MAPPING;
58 }
59 mappingsContext.currentOriginalLine++;
60 }
61 return mappings;
62 }
63
64 mapGeneratedCode(fn) {
65 throw new Error("Cannot map generated code on a SourceMap. Normalize to SingleLineNode first.");
66 }
67
68 getNormalizedNodes() {
69 var results = [];
70 var currentLine = this.startingLine;
71 var generatedCode = this.generatedCode;
72 var index = 0;
73 var indexEnd = generatedCode.length;
74 while(index < indexEnd) {
75 // get one generated line
76 var nextLine = generatedCode.indexOf("\n", index) + 1;
77 if(nextLine === 0) nextLine = indexEnd;
78 var lineGenerated = generatedCode.substr(index, nextLine - index);
79
80 results.push(new SingleLineNode(lineGenerated, this.source, this.originalSource, currentLine));
81
82 // move cursors
83 index = nextLine;
84 currentLine++;
85 }
86 return results;
87 }
88
89 merge(otherNode) {
90 if(otherNode instanceof SourceNode) {
91 return this.mergeSourceNode(otherNode);
92 } else if(otherNode instanceof SingleLineNode) {
93 return this.mergeSingleLineNode(otherNode);
94 }
95 return false;
96 }
97
98 mergeSourceNode(otherNode) {
99 if(this.source === otherNode.source &&
100 this._endsWithNewLine &&
101 this.startingLine + this._numberOfLines === otherNode.startingLine) {
102 this.generatedCode += otherNode.generatedCode;
103 this._numberOfLines += otherNode._numberOfLines;
104 this._endsWithNewLine = otherNode._endsWithNewLine;
105 return this;
106 }
107 return false;
108 }
109
110 mergeSingleLineNode(otherNode) {
111 if(this.source === otherNode.source &&
112 this._endsWithNewLine &&
113 this.startingLine + this._numberOfLines === otherNode.line &&
114 otherNode._numberOfLines <= 1) {
115 this.addSingleLineNode(otherNode);
116 return this;
117 }
118 return false;
119 }
120
121 addSingleLineNode(otherNode) {
122 this.generatedCode += otherNode.generatedCode;
123 this._numberOfLines += otherNode._numberOfLines
124 this._endsWithNewLine = otherNode._endsWithNewLine;
125 }
126}
127
128module.exports = SourceNode;
129const SingleLineNode = require("./SingleLineNode"); // circular dependency
Note: See TracBrowser for help on using the repository browser.