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