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