source-list-map
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -5,58 +5,63 @@ /* | ||
*/ | ||
var getNumberOfLines = require("./helpers").getNumberOfLines; | ||
var getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
"use strict"; | ||
function CodeNode(generatedCode) { | ||
this.generatedCode = generatedCode; | ||
} | ||
module.exports = CodeNode; | ||
const getNumberOfLines = require("./helpers").getNumberOfLines; | ||
const getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
CodeNode.prototype.clone = function() { | ||
return new CodeNode(this.generatedCode); | ||
} | ||
class CodeNode { | ||
constructor(generatedCode) { | ||
this.generatedCode = generatedCode; | ||
} | ||
CodeNode.prototype.getGeneratedCode = function() { | ||
return this.generatedCode; | ||
}; | ||
clone() { | ||
return new CodeNode(this.generatedCode); | ||
} | ||
CodeNode.prototype.getMappings = function(mappingsContext) { | ||
var lines = getNumberOfLines(this.generatedCode); | ||
var mapping = Array(lines+1).join(";"); | ||
if(lines > 0) { | ||
mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode); | ||
if(mappingsContext.unfinishedGeneratedLine > 0) { | ||
return mapping + "A"; | ||
getGeneratedCode() { | ||
return this.generatedCode; | ||
} | ||
getMappings(mappingsContext) { | ||
const lines = getNumberOfLines(this.generatedCode); | ||
const mapping = Array(lines+1).join(";"); | ||
if(lines > 0) { | ||
mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode); | ||
if(mappingsContext.unfinishedGeneratedLine > 0) { | ||
return mapping + "A"; | ||
} else { | ||
return mapping; | ||
} | ||
} else { | ||
return mapping; | ||
const prevUnfinished = mappingsContext.unfinishedGeneratedLine; | ||
mappingsContext.unfinishedGeneratedLine += getUnfinishedLine(this.generatedCode); | ||
if(prevUnfinished === 0 && mappingsContext.unfinishedGeneratedLine > 0) { | ||
return "A"; | ||
} else { | ||
return ""; | ||
} | ||
} | ||
} else { | ||
var prevUnfinished = mappingsContext.unfinishedGeneratedLine; | ||
mappingsContext.unfinishedGeneratedLine += getUnfinishedLine(this.generatedCode); | ||
if(prevUnfinished === 0 && mappingsContext.unfinishedGeneratedLine > 0) { | ||
return "A"; | ||
} else { | ||
return ""; | ||
} | ||
} | ||
}; | ||
CodeNode.prototype.addGeneratedCode = function(generatedCode) { | ||
this.generatedCode += generatedCode; | ||
}; | ||
addGeneratedCode(generatedCode) { | ||
this.generatedCode += generatedCode; | ||
} | ||
CodeNode.prototype.mapGeneratedCode = function(fn) { | ||
var generatedCode = fn(this.generatedCode); | ||
return new CodeNode(generatedCode); | ||
}; | ||
mapGeneratedCode(fn) { | ||
const generatedCode = fn(this.generatedCode); | ||
return new CodeNode(generatedCode); | ||
} | ||
CodeNode.prototype.getNormalizedNodes = function() { | ||
return [this]; | ||
}; | ||
getNormalizedNodes() { | ||
return [this]; | ||
} | ||
CodeNode.prototype.merge = function merge(otherNode) { | ||
if(otherNode instanceof CodeNode) { | ||
this.generatedCode += otherNode.generatedCode; | ||
return this; | ||
merge(otherNode) { | ||
if(otherNode instanceof CodeNode) { | ||
this.generatedCode += otherNode.generatedCode; | ||
return this; | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
} | ||
module.exports = CodeNode; |
@@ -5,19 +5,21 @@ /* | ||
*/ | ||
var base64VLQ = require("./base64-vlq"); | ||
var SourceNode = require("./SourceNode"); | ||
var CodeNode = require("./CodeNode"); | ||
var SourceListMap = require("./SourceListMap"); | ||
"use strict"; | ||
const base64VLQ = require("./base64-vlq"); | ||
const SourceNode = require("./SourceNode"); | ||
const CodeNode = require("./CodeNode"); | ||
const SourceListMap = require("./SourceListMap"); | ||
module.exports = function fromStringWithSourceMap(code, map) { | ||
var sources = map.sources; | ||
var sourcesContent = map.sourcesContent; | ||
var mappings = map.mappings.split(";"); | ||
var lines = code.split("\n"); | ||
var nodes = []; | ||
var currentNode = null; | ||
var currentLine = 1; | ||
var currentSourceIdx = 0; | ||
var currentSourceNodeLine; | ||
const sources = map.sources; | ||
const sourcesContent = map.sourcesContent; | ||
const mappings = map.mappings.split(";"); | ||
const lines = code.split("\n"); | ||
const nodes = []; | ||
let currentNode = null; | ||
let currentLine = 1; | ||
let currentSourceIdx = 0; | ||
let currentSourceNodeLine; | ||
mappings.forEach(function(mapping, idx) { | ||
var line = lines[idx]; | ||
let line = lines[idx]; | ||
if(typeof line === 'undefined') return; | ||
@@ -28,3 +30,3 @@ if(idx !== lines.length - 1) line += "\n"; | ||
mapping = { value: 0, rest: mapping }; | ||
var lineAdded = false; | ||
let lineAdded = false; | ||
while(mapping.rest) | ||
@@ -36,3 +38,3 @@ lineAdded = processMapping(mapping, line, lineAdded) || lineAdded; | ||
if(mappings.length < lines.length) { | ||
var idx = mappings.length; | ||
let idx = mappings.length; | ||
while(!lines[idx].trim() && idx < lines.length-1) { | ||
@@ -57,15 +59,16 @@ addCode(lines[idx] + "\n"); | ||
base64VLQ.decode(mapping.rest, mapping); | ||
var sourceIdx = mapping.value + currentSourceIdx; | ||
const sourceIdx = mapping.value + currentSourceIdx; | ||
currentSourceIdx = sourceIdx; | ||
let linePosition; | ||
if(mapping.rest && mapping.rest[0] !== ",") { | ||
base64VLQ.decode(mapping.rest, mapping); | ||
var linePosition = mapping.value + currentLine; | ||
linePosition = mapping.value + currentLine; | ||
currentLine = linePosition; | ||
} else { | ||
var linePosition = currentLine; | ||
linePosition = currentLine; | ||
} | ||
if(mapping.rest) { | ||
var next = mapping.rest.indexOf(","); | ||
const next = mapping.rest.indexOf(","); | ||
mapping.rest = next === -1 ? "" : mapping.rest.substr(next); | ||
@@ -72,0 +75,0 @@ } |
@@ -5,5 +5,7 @@ /* | ||
*/ | ||
"use strict"; | ||
exports.getNumberOfLines = function getNumberOfLines(str) { | ||
var nr = -1; | ||
var idx = -1; | ||
let nr = -1; | ||
let idx = -1; | ||
do { | ||
@@ -17,3 +19,3 @@ nr++ | ||
exports.getUnfinishedLine = function getUnfinishedLine(str) { | ||
var idx = str.lastIndexOf("\n"); | ||
const idx = str.lastIndexOf("\n"); | ||
if(idx === -1) | ||
@@ -20,0 +22,0 @@ return str.length; |
exports.SourceListMap = require("./SourceListMap"); | ||
exports.SourceNode = require("./SourceNode"); | ||
exports.SingleLineNode = require("./SingleLineNode"); | ||
exports.CodeNode = require("./CodeNode"); | ||
exports.MappingsContext = require("./MappingsContext"); | ||
exports.fromStringWithSourceMap = require("./fromStringWithSourceMap"); |
@@ -5,22 +5,42 @@ /* | ||
*/ | ||
function MappingsContext() { | ||
this.sources = []; | ||
this.sourcesContent = []; | ||
this.hasSourceContent = false; | ||
this.currentOriginalLine = 1; | ||
this.currentSource = 0; | ||
this.unfinishedGeneratedLine = false; | ||
"use strict"; | ||
class MappingsContext { | ||
constructor() { | ||
this.sourcesIndices = new Map(); | ||
this.sourcesContent = new Map(); | ||
this.hasSourceContent = false; | ||
this.currentOriginalLine = 1; | ||
this.currentSource = 0; | ||
this.unfinishedGeneratedLine = false; | ||
} | ||
ensureSource(source, originalSource) { | ||
let idx = this.sourcesIndices.get(source); | ||
if(typeof idx === "number") { | ||
return idx; | ||
} | ||
idx = this.sourcesIndices.size; | ||
this.sourcesIndices.set(source, idx); | ||
this.sourcesContent.set(source, originalSource) | ||
if(typeof originalSource === "string") | ||
this.hasSourceContent = true; | ||
return idx; | ||
} | ||
getArrays() { | ||
const sources = []; | ||
const sourcesContent = []; | ||
for(const pair of this.sourcesContent) { | ||
sources.push(pair[0]); | ||
sourcesContent.push(pair[1]); | ||
} | ||
return { | ||
sources, | ||
sourcesContent | ||
}; | ||
} | ||
} | ||
module.exports = MappingsContext; | ||
MappingsContext.prototype.ensureSource = function(source, originalSource) { | ||
var idx = this.sources.indexOf(source); | ||
if(idx >= 0) | ||
return idx; | ||
idx = this.sources.length; | ||
this.sources.push(source); | ||
this.sourcesContent.push(originalSource); | ||
if(typeof originalSource === "string") | ||
this.hasSourceContent = true; | ||
return idx; | ||
}; |
@@ -5,84 +5,90 @@ /* | ||
*/ | ||
module.exports = SingleLineNode; // circular dependency | ||
"use strict"; | ||
var base64VLQ = require("./base64-vlq"); | ||
var getNumberOfLines = require("./helpers").getNumberOfLines; | ||
var getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
var SourceNode = require("./SourceNode"); | ||
const base64VLQ = require("./base64-vlq"); | ||
const getNumberOfLines = require("./helpers").getNumberOfLines; | ||
const getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
function SingleLineNode(generatedCode, source, originalSource, line) { | ||
this.generatedCode = generatedCode; | ||
this.originalSource = originalSource; | ||
this.source = source; | ||
this.line = line || 1; | ||
this._numberOfLines = getNumberOfLines(this.generatedCode); | ||
this._endsWithNewLine = generatedCode[generatedCode.length - 1] === "\n"; | ||
} | ||
const LINE_MAPPING = ";AAAA"; | ||
SingleLineNode.prototype.clone = function() { | ||
return new SingleLineNode(this.generatedCode, this.source, this.originalSource, this.line); | ||
} | ||
class SingleLineNode { | ||
var LINE_MAPPING = ";AAAA"; | ||
constructor(generatedCode, source, originalSource, line) { | ||
this.generatedCode = generatedCode; | ||
this.originalSource = originalSource; | ||
this.source = source; | ||
this.line = line || 1; | ||
this._numberOfLines = getNumberOfLines(this.generatedCode); | ||
this._endsWithNewLine = generatedCode[generatedCode.length - 1] === "\n"; | ||
} | ||
SingleLineNode.prototype.getGeneratedCode = function() { | ||
return this.generatedCode; | ||
}; | ||
clone() { | ||
return new SingleLineNode(this.generatedCode, this.source, this.originalSource, this.line); | ||
} | ||
SingleLineNode.prototype.getMappings = function(mappingsContext) { | ||
if(!this.generatedCode) | ||
return ""; | ||
var lines = this._numberOfLines; | ||
var sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource); | ||
var mappings = "A"; // generated column 0 | ||
if(mappingsContext.unfinishedGeneratedLine) | ||
mappings = "," + base64VLQ.encode(mappingsContext.unfinishedGeneratedLine); | ||
mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index | ||
mappings += base64VLQ.encode(this.line - mappingsContext.currentOriginalLine); // original line index | ||
mappings += "A"; // original column 0 | ||
mappingsContext.currentSource = sourceIdx; | ||
mappingsContext.currentOriginalLine = this.line; | ||
var unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode) | ||
mappings += Array(lines).join(LINE_MAPPING); | ||
if(unfinishedGeneratedLine === 0) { | ||
mappings += ";"; | ||
} else { | ||
if(lines !== 0) | ||
mappings += LINE_MAPPING; | ||
getGeneratedCode() { | ||
return this.generatedCode; | ||
} | ||
return mappings; | ||
}; | ||
SingleLineNode.prototype.getNormalizedNodes = function() { | ||
return [this]; | ||
}; | ||
getMappings(mappingsContext) { | ||
if(!this.generatedCode) | ||
return ""; | ||
const lines = this._numberOfLines; | ||
const sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource); | ||
let mappings = "A"; // generated column 0 | ||
if(mappingsContext.unfinishedGeneratedLine) | ||
mappings = "," + base64VLQ.encode(mappingsContext.unfinishedGeneratedLine); | ||
mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index | ||
mappings += base64VLQ.encode(this.line - mappingsContext.currentOriginalLine); // original line index | ||
mappings += "A"; // original column 0 | ||
mappingsContext.currentSource = sourceIdx; | ||
mappingsContext.currentOriginalLine = this.line; | ||
const unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode) | ||
mappings += Array(lines).join(LINE_MAPPING); | ||
if(unfinishedGeneratedLine === 0) { | ||
mappings += ";"; | ||
} else { | ||
if(lines !== 0) | ||
mappings += LINE_MAPPING; | ||
} | ||
return mappings; | ||
} | ||
SingleLineNode.prototype.mapGeneratedCode = function(fn) { | ||
var generatedCode = fn(this.generatedCode); | ||
return new SingleLineNode(generatedCode, this.source, this.originalSource, this.line); | ||
}; | ||
getNormalizedNodes() { | ||
return [this]; | ||
} | ||
SingleLineNode.prototype.merge = function merge(otherNode) { | ||
if(otherNode instanceof SingleLineNode) { | ||
return this.mergeSingleLineNode(otherNode); | ||
mapGeneratedCode(fn) { | ||
const generatedCode = fn(this.generatedCode); | ||
return new SingleLineNode(generatedCode, this.source, this.originalSource, this.line); | ||
} | ||
return false; | ||
}; | ||
SingleLineNode.prototype.mergeSingleLineNode = function mergeSingleLineNode(otherNode) { | ||
if(this.source === otherNode.source && | ||
this.originalSource === otherNode.originalSource) { | ||
if(this.line === otherNode.line) { | ||
this.generatedCode += otherNode.generatedCode; | ||
this._numberOfLines += otherNode._numberOfLines; | ||
this._endsWithNewLine = otherNode._endsWithNewLine; | ||
return this; | ||
} else if(this.line + 1 === otherNode.line && | ||
this._endsWithNewLine && | ||
this._numberOfLines === 1 && | ||
otherNode._numberOfLines <= 1) { | ||
return new SourceNode(this.generatedCode + otherNode.generatedCode, this.source, this.originalSource, this.line); | ||
merge(otherNode) { | ||
if(otherNode instanceof SingleLineNode) { | ||
return this.mergeSingleLineNode(otherNode); | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
mergeSingleLineNode(otherNode) { | ||
if(this.source === otherNode.source && | ||
this.originalSource === otherNode.originalSource) { | ||
if(this.line === otherNode.line) { | ||
this.generatedCode += otherNode.generatedCode; | ||
this._numberOfLines += otherNode._numberOfLines; | ||
this._endsWithNewLine = otherNode._endsWithNewLine; | ||
return this; | ||
} else if(this.line + 1 === otherNode.line && | ||
this._endsWithNewLine && | ||
this._numberOfLines === 1 && | ||
otherNode._numberOfLines <= 1) { | ||
return new SourceNode(this.generatedCode + otherNode.generatedCode, this.source, this.originalSource, this.line); | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
module.exports = SingleLineNode; | ||
const SourceNode = require("./SourceNode"); // circular dependency |
@@ -5,107 +5,114 @@ /* | ||
*/ | ||
var CodeNode = require("./CodeNode"); | ||
var SourceNode = require("./SourceNode"); | ||
var MappingsContext = require("./MappingsContext"); | ||
var getNumberOfLines = require("./helpers").getNumberOfLines; | ||
"use strict"; | ||
function SourceListMap(generatedCode, source, originalSource) { | ||
if(Array.isArray(generatedCode)) { | ||
this.children = generatedCode; | ||
} else { | ||
this.children = []; | ||
if(generatedCode || source) | ||
this.add(generatedCode, source, originalSource); | ||
} | ||
} | ||
module.exports = SourceListMap; | ||
const CodeNode = require("./CodeNode"); | ||
const SourceNode = require("./SourceNode"); | ||
const MappingsContext = require("./MappingsContext"); | ||
const getNumberOfLines = require("./helpers").getNumberOfLines; | ||
SourceListMap.prototype.add = function(generatedCode, source, originalSource) { | ||
if(typeof generatedCode === "string") { | ||
if(source) { | ||
this.children.push(new SourceNode(generatedCode, source, originalSource)); | ||
} else if(this.children.length > 0 && this.children[this.children.length - 1] instanceof CodeNode) { | ||
this.children[this.children.length - 1].addGeneratedCode(generatedCode); | ||
class SourceListMap { | ||
constructor(generatedCode, source, originalSource) { | ||
if(Array.isArray(generatedCode)) { | ||
this.children = generatedCode; | ||
} else { | ||
this.children.push(new CodeNode(generatedCode)); | ||
this.children = []; | ||
if(generatedCode || source) | ||
this.add(generatedCode, source, originalSource); | ||
} | ||
} else if(generatedCode.getMappings && generatedCode.getGeneratedCode) { | ||
this.children.push(generatedCode); | ||
} else if(generatedCode.children) { | ||
generatedCode.children.forEach(function(sln) { | ||
this.children.push(sln); | ||
}, this); | ||
} else { | ||
throw new Error("Invalid arguments to SourceListMap.prototype.add: Expected string, Node or SourceListMap"); | ||
} | ||
}; | ||
SourceListMap.prototype.preprend = function(generatedCode, source, originalSource) { | ||
if(typeof generatedCode === "string") { | ||
if(source) { | ||
this.children.unshift(new SourceNode(generatedCode, source, originalSource)); | ||
} else if(this.children.length > 0 && this.children[this.children.length - 1].preprendGeneratedCode) { | ||
this.children[this.children.length - 1].preprendGeneratedCode(generatedCode); | ||
add(generatedCode, source, originalSource) { | ||
if(typeof generatedCode === "string") { | ||
if(source) { | ||
this.children.push(new SourceNode(generatedCode, source, originalSource)); | ||
} else if(this.children.length > 0 && this.children[this.children.length - 1] instanceof CodeNode) { | ||
this.children[this.children.length - 1].addGeneratedCode(generatedCode); | ||
} else { | ||
this.children.push(new CodeNode(generatedCode)); | ||
} | ||
} else if(generatedCode.getMappings && generatedCode.getGeneratedCode) { | ||
this.children.push(generatedCode); | ||
} else if(generatedCode.children) { | ||
generatedCode.children.forEach(function(sln) { | ||
this.children.push(sln); | ||
}, this); | ||
} else { | ||
this.children.unshift(new CodeNode(generatedCode)); | ||
throw new Error("Invalid arguments to SourceListMap.protfotype.add: Expected string, Node or SourceListMap"); | ||
} | ||
} else if(generatedCode.getMappings && generatedCode.getGeneratedCode) { | ||
this.children.unshift(generatedCode); | ||
} else if(generatedCode.children) { | ||
generatedCode.children.slice().reverse().forEach(function(sln) { | ||
this.children.unshift(sln); | ||
}, this); | ||
} else { | ||
throw new Error("Invalid arguments to SourceListMap.prototype.prerend: Expected string, Node or SourceListMap"); | ||
} | ||
}; | ||
}; | ||
SourceListMap.prototype.mapGeneratedCode = function(fn) { | ||
var normalizedNodes = []; | ||
this.children.forEach(function(sln) { | ||
sln.getNormalizedNodes().forEach(function(newNode) { | ||
normalizedNodes.push(newNode); | ||
}); | ||
}); | ||
var optimizedNodes = []; | ||
normalizedNodes.forEach(function(sln) { | ||
sln = sln.mapGeneratedCode(fn); | ||
if(optimizedNodes.length === 0) { | ||
optimizedNodes.push(sln); | ||
} else { | ||
var last = optimizedNodes[optimizedNodes.length - 1]; | ||
var mergedNode = last.merge(sln); | ||
if(mergedNode) { | ||
optimizedNodes[optimizedNodes.length - 1] = mergedNode; | ||
preprend(generatedCode, source, originalSource) { | ||
if(typeof generatedCode === "string") { | ||
if(source) { | ||
this.children.unshift(new SourceNode(generatedCode, source, originalSource)); | ||
} else if(this.children.length > 0 && this.children[this.children.length - 1].preprendGeneratedCode) { | ||
this.children[this.children.length - 1].preprendGeneratedCode(generatedCode); | ||
} else { | ||
optimizedNodes.push(sln); | ||
this.children.unshift(new CodeNode(generatedCode)); | ||
} | ||
} else if(generatedCode.getMappings && generatedCode.getGeneratedCode) { | ||
this.children.unshift(generatedCode); | ||
} else if(generatedCode.children) { | ||
generatedCode.children.slice().reverse().forEach(function(sln) { | ||
this.children.unshift(sln); | ||
}, this); | ||
} else { | ||
throw new Error("Invalid arguments to SourceListMap.protfotype.prerend: Expected string, Node or SourceListMap"); | ||
} | ||
}); | ||
return new SourceListMap(optimizedNodes); | ||
}; | ||
}; | ||
SourceListMap.prototype.toString = function() { | ||
return this.children.map(function(sln) { | ||
return sln.getGeneratedCode(); | ||
}).join(""); | ||
}; | ||
mapGeneratedCode(fn) { | ||
const normalizedNodes = []; | ||
this.children.forEach(function(sln) { | ||
sln.getNormalizedNodes().forEach(function(newNode) { | ||
normalizedNodes.push(newNode); | ||
}); | ||
}); | ||
const optimizedNodes = []; | ||
normalizedNodes.forEach(function(sln) { | ||
sln = sln.mapGeneratedCode(fn); | ||
if(optimizedNodes.length === 0) { | ||
optimizedNodes.push(sln); | ||
} else { | ||
const last = optimizedNodes[optimizedNodes.length - 1]; | ||
const mergedNode = last.merge(sln); | ||
if(mergedNode) { | ||
optimizedNodes[optimizedNodes.length - 1] = mergedNode; | ||
} else { | ||
optimizedNodes.push(sln); | ||
} | ||
} | ||
}); | ||
return new SourceListMap(optimizedNodes); | ||
}; | ||
SourceListMap.prototype.toStringWithSourceMap = function(options) { | ||
var mappingsContext = new MappingsContext(); | ||
var source = this.children.map(function(sln) { | ||
return sln.getGeneratedCode(); | ||
}).join(""); | ||
var mappings = this.children.map(function(sln) { | ||
return sln.getMappings(mappingsContext); | ||
}).join(""); | ||
return { | ||
source: source, | ||
map: { | ||
version: 3, | ||
file: options && options.file, | ||
sources: mappingsContext.sources, | ||
sourcesContent: mappingsContext.hasSourceContent ? mappingsContext.sourcesContent : undefined, | ||
mappings: mappings | ||
} | ||
toString() { | ||
return this.children.map(function(sln) { | ||
return sln.getGeneratedCode(); | ||
}).join(""); | ||
}; | ||
toStringWithSourceMap(options) { | ||
const mappingsContext = new MappingsContext(); | ||
const source = this.children.map(function(sln) { | ||
return sln.getGeneratedCode(); | ||
}).join(""); | ||
const mappings = this.children.map(function(sln) { | ||
return sln.getMappings(mappingsContext); | ||
}).join(""); | ||
const arrays = mappingsContext.getArrays(); | ||
return { | ||
source: source, | ||
map: { | ||
version: 3, | ||
file: options && options.file, | ||
sources: arrays.sources, | ||
sourcesContent: mappingsContext.hasSourceContent ? arrays.sourcesContent : undefined, | ||
mappings: mappings | ||
} | ||
}; | ||
} | ||
} | ||
module.exports = SourceListMap; |
@@ -5,121 +5,126 @@ /* | ||
*/ | ||
module.exports = SourceNode; // circular dependency | ||
"use strict"; | ||
var base64VLQ = require("./base64-vlq"); | ||
var getNumberOfLines = require("./helpers").getNumberOfLines; | ||
var getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
var SingleLineNode = require("./SingleLineNode"); | ||
const base64VLQ = require("./base64-vlq"); | ||
const getNumberOfLines = require("./helpers").getNumberOfLines; | ||
const getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
function SourceNode(generatedCode, source, originalSource, startingLine) { | ||
this.generatedCode = generatedCode; | ||
this.originalSource = originalSource; | ||
this.source = source; | ||
this.startingLine = startingLine || 1; | ||
this._numberOfLines = getNumberOfLines(this.generatedCode); | ||
this._endsWithNewLine = generatedCode[generatedCode.length - 1] === "\n"; | ||
} | ||
const LINE_MAPPING = ";AACA"; | ||
SourceNode.prototype.clone = function() { | ||
return new SourceNode(this.generatedCode, this.source, this.originalSource, this.startingLine); | ||
} | ||
class SourceNode { | ||
var LINE_MAPPING = ";AACA"; | ||
constructor(generatedCode, source, originalSource, startingLine) { | ||
this.generatedCode = generatedCode; | ||
this.originalSource = originalSource; | ||
this.source = source; | ||
this.startingLine = startingLine || 1; | ||
this._numberOfLines = getNumberOfLines(this.generatedCode); | ||
this._endsWithNewLine = generatedCode[generatedCode.length - 1] === "\n"; | ||
} | ||
SourceNode.prototype.getGeneratedCode = function() { | ||
return this.generatedCode; | ||
}; | ||
clone() { | ||
return new SourceNode(this.generatedCode, this.source, this.originalSource, this.startingLine); | ||
} | ||
SourceNode.prototype.addGeneratedCode = function(code) { | ||
this.generatedCode += code; | ||
this._numberOfLines += getNumberOfLines(code); | ||
this._endsWithNewLine = code[code.length - 1] === "\n"; | ||
}; | ||
getGeneratedCode() { | ||
return this.generatedCode; | ||
} | ||
SourceNode.prototype.getMappings = function(mappingsContext) { | ||
if(!this.generatedCode) | ||
return ""; | ||
var lines = this._numberOfLines; | ||
var sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource); | ||
var mappings = "A"; // generated column 0 | ||
if(mappingsContext.unfinishedGeneratedLine) | ||
mappings = "," + base64VLQ.encode(mappingsContext.unfinishedGeneratedLine); | ||
mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index | ||
mappings += base64VLQ.encode(this.startingLine - mappingsContext.currentOriginalLine); // original line index | ||
mappings += "A"; // original column 0 | ||
mappingsContext.currentSource = sourceIdx; | ||
mappingsContext.currentOriginalLine = this.startingLine + lines - 1; | ||
var unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode) | ||
mappings += Array(lines).join(LINE_MAPPING); | ||
if(unfinishedGeneratedLine === 0) { | ||
mappings += ";"; | ||
} else { | ||
if(lines !== 0) { | ||
mappings += LINE_MAPPING; | ||
addGeneratedCode(code) { | ||
this.generatedCode += code; | ||
this._numberOfLines += getNumberOfLines(code); | ||
this._endsWithNewLine = code[code.length - 1] === "\n"; | ||
} | ||
getMappings(mappingsContext) { | ||
if(!this.generatedCode) | ||
return ""; | ||
const lines = this._numberOfLines; | ||
const sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource); | ||
let mappings = "A"; // generated column 0 | ||
if(mappingsContext.unfinishedGeneratedLine) | ||
mappings = "," + base64VLQ.encode(mappingsContext.unfinishedGeneratedLine); | ||
mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index | ||
mappings += base64VLQ.encode(this.startingLine - mappingsContext.currentOriginalLine); // original line index | ||
mappings += "A"; // original column 0 | ||
mappingsContext.currentSource = sourceIdx; | ||
mappingsContext.currentOriginalLine = this.startingLine + lines - 1; | ||
const unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode) | ||
mappings += Array(lines).join(LINE_MAPPING); | ||
if(unfinishedGeneratedLine === 0) { | ||
mappings += ";"; | ||
} else { | ||
if(lines !== 0) { | ||
mappings += LINE_MAPPING; | ||
} | ||
mappingsContext.currentOriginalLine++; | ||
} | ||
mappingsContext.currentOriginalLine++; | ||
return mappings; | ||
} | ||
return mappings; | ||
}; | ||
SourceNode.prototype.mapGeneratedCode = function(fn) { | ||
throw new Error("Cannot map generated code on a SourceMap. Normalize to SingleLineNode first."); | ||
}; | ||
mapGeneratedCode(fn) { | ||
throw new Error("Cannot map generated code on a SourceMap. Normalize to SingleLineNode first."); | ||
} | ||
SourceNode.prototype.getNormalizedNodes = function() { | ||
var results = []; | ||
var currentLine = this.startingLine; | ||
var generatedCode = this.generatedCode; | ||
var index = 0; | ||
var indexEnd = generatedCode.length; | ||
while(index < indexEnd) { | ||
// get one generated line | ||
var nextLine = generatedCode.indexOf("\n", index) + 1; | ||
if(nextLine === 0) nextLine = indexEnd; | ||
var lineGenerated = generatedCode.substr(index, nextLine - index); | ||
getNormalizedNodes() { | ||
var results = []; | ||
var currentLine = this.startingLine; | ||
var generatedCode = this.generatedCode; | ||
var index = 0; | ||
var indexEnd = generatedCode.length; | ||
while(index < indexEnd) { | ||
// get one generated line | ||
var nextLine = generatedCode.indexOf("\n", index) + 1; | ||
if(nextLine === 0) nextLine = indexEnd; | ||
var lineGenerated = generatedCode.substr(index, nextLine - index); | ||
results.push(new SingleLineNode(lineGenerated, this.source, this.originalSource, currentLine)); | ||
results.push(new SingleLineNode(lineGenerated, this.source, this.originalSource, currentLine)); | ||
// move cursors | ||
index = nextLine; | ||
currentLine++; | ||
// move cursors | ||
index = nextLine; | ||
currentLine++; | ||
} | ||
return results; | ||
} | ||
return results; | ||
}; | ||
SourceNode.prototype.merge = function merge(otherNode) { | ||
if(otherNode instanceof SourceNode) { | ||
return this.mergeSourceNode(otherNode); | ||
} else if(otherNode instanceof SingleLineNode) { | ||
return this.mergeSingleLineNode(otherNode); | ||
merge(otherNode) { | ||
if(otherNode instanceof SourceNode) { | ||
return this.mergeSourceNode(otherNode); | ||
} else if(otherNode instanceof SingleLineNode) { | ||
return this.mergeSingleLineNode(otherNode); | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
SourceNode.prototype.mergeSourceNode = function mergeSourceNode(otherNode) { | ||
if(this.source === otherNode.source && | ||
this._endsWithNewLine && | ||
this.startingLine + this._numberOfLines === otherNode.startingLine) { | ||
mergeSourceNode(otherNode) { | ||
if(this.source === otherNode.source && | ||
this._endsWithNewLine && | ||
this.startingLine + this._numberOfLines === otherNode.startingLine) { | ||
this.generatedCode += otherNode.generatedCode; | ||
this._numberOfLines += otherNode._numberOfLines; | ||
this._endsWithNewLine = otherNode._endsWithNewLine; | ||
return this; | ||
} | ||
return false; | ||
} | ||
mergeSingleLineNode(otherNode) { | ||
if(this.source === otherNode.source && | ||
this._endsWithNewLine && | ||
this.startingLine + this._numberOfLines === otherNode.line && | ||
otherNode._numberOfLines <= 1) { | ||
this.addSingleLineNode(otherNode); | ||
return this; | ||
} | ||
return false; | ||
} | ||
addSingleLineNode(otherNode) { | ||
this.generatedCode += otherNode.generatedCode; | ||
this._numberOfLines += otherNode._numberOfLines; | ||
this._numberOfLines += otherNode._numberOfLines | ||
this._endsWithNewLine = otherNode._endsWithNewLine; | ||
return this; | ||
} | ||
return false; | ||
}; | ||
} | ||
SourceNode.prototype.mergeSingleLineNode = function mergeSingleLineNode(otherNode) { | ||
if(this.source === otherNode.source && | ||
this._endsWithNewLine && | ||
this.startingLine + this._numberOfLines === otherNode.line && | ||
otherNode._numberOfLines <= 1) { | ||
this.addSingleLineNode(otherNode); | ||
return this; | ||
} | ||
return false; | ||
}; | ||
SourceNode.prototype.addSingleLineNode = function addSingleLineNode(otherNode) { | ||
this.generatedCode += otherNode.generatedCode; | ||
this._numberOfLines += otherNode._numberOfLines | ||
this._endsWithNewLine = otherNode._endsWithNewLine; | ||
}; | ||
module.exports = SourceNode; | ||
const SingleLineNode = require("./SingleLineNode"); // circular dependency |
{ | ||
"name": "source-list-map", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Fast line to line SourceMap generator.", | ||
@@ -5,0 +5,0 @@ "author": "Tobias Koppers @sokra", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
667
26223