UNPKG

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