1 |
|
2 |
|
3 |
|
4 |
|
5 | var base64VLQ = require("./base64-vlq");
|
6 | var SourceNode = require("./SourceNode");
|
7 | var CodeNode = require("./CodeNode");
|
8 | var SourceListMap = require("./SourceListMap");
|
9 |
|
10 | module.exports = function fromStringWithSourceMap(code, map) {
|
11 | var sources = map.sources;
|
12 | var sourcesContent = map.sourcesContent;
|
13 | var mappings = map.mappings.split(";");
|
14 | var lines = code.split("\n");
|
15 | var nodes = [];
|
16 | var currentNode = null;
|
17 | var currentLine = 1;
|
18 | var currentSourceIdx = 0;
|
19 | var currentSourceNodeLine;
|
20 | mappings.forEach(function(mapping, idx) {
|
21 | var line = lines[idx];
|
22 | if(typeof line === 'undefined') return;
|
23 | if(idx !== lines.length - 1) line += "\n";
|
24 | if(!mapping)
|
25 | return addCode(line);
|
26 | mapping = { value: 0, rest: mapping };
|
27 | var lineAdded = false;
|
28 | while(mapping.rest)
|
29 | lineAdded = processMapping(mapping, line, lineAdded) || lineAdded;
|
30 | if(!lineAdded)
|
31 | addCode(line);
|
32 | });
|
33 | if(mappings.length < lines.length) {
|
34 | var idx = mappings.length;
|
35 | while(!lines[idx].trim() && idx < lines.length-1) {
|
36 | addCode(lines[idx] + "\n");
|
37 | idx++;
|
38 | }
|
39 | addCode(lines.slice(idx).join("\n"));
|
40 | }
|
41 | return new SourceListMap(nodes);
|
42 | function processMapping(mapping, line, ignore) {
|
43 | if(mapping.rest && mapping.rest[0] !== ",") {
|
44 | base64VLQ.decode(mapping.rest, mapping);
|
45 | }
|
46 | if(!mapping.rest)
|
47 | return false;
|
48 | if(mapping.rest[0] === ",") {
|
49 | mapping.rest = mapping.rest.substr(1);
|
50 | return false;
|
51 | }
|
52 |
|
53 | base64VLQ.decode(mapping.rest, mapping);
|
54 | var sourceIdx = mapping.value + currentSourceIdx;
|
55 | currentSourceIdx = sourceIdx;
|
56 |
|
57 | if(mapping.rest && mapping.rest[0] !== ",") {
|
58 | base64VLQ.decode(mapping.rest, mapping);
|
59 | var linePosition = mapping.value + currentLine;
|
60 | currentLine = linePosition;
|
61 | } else {
|
62 | var linePosition = currentLine;
|
63 | }
|
64 |
|
65 | if(mapping.rest && mapping.rest[0] === ",") {
|
66 | mapping.rest = mapping.rest.substr(1);
|
67 | }
|
68 |
|
69 | if(!ignore) {
|
70 | addSource(line, sources ? sources[sourceIdx] : null, sourcesContent ? sourcesContent[sourceIdx] : null, linePosition)
|
71 | return true;
|
72 | }
|
73 | }
|
74 | function addCode(generatedCode) {
|
75 | if(currentNode && currentNode instanceof CodeNode) {
|
76 | currentNode.addGeneratedCode(generatedCode);
|
77 | } else if(currentNode && currentNode instanceof SourceNode && !generatedCode.trim()) {
|
78 | currentNode.generatedCode += generatedCode;
|
79 | currentSourceNodeLine++;
|
80 | } else {
|
81 | currentNode = new CodeNode(generatedCode);
|
82 | nodes.push(currentNode);
|
83 | }
|
84 | }
|
85 | function addSource(generatedCode, source, originalSource, linePosition) {
|
86 | if(currentNode && currentNode instanceof SourceNode &&
|
87 | currentNode.source === source &&
|
88 | currentSourceNodeLine === linePosition
|
89 | ) {
|
90 | currentNode.generatedCode += generatedCode;
|
91 | currentSourceNodeLine++;
|
92 | } else {
|
93 | currentNode = new SourceNode(generatedCode, source, originalSource, linePosition);
|
94 | currentSourceNodeLine = linePosition + 1;
|
95 | nodes.push(currentNode);
|
96 | }
|
97 | }
|
98 | };
|