source-list-map
Advanced tools
Comparing version 0.1.8 to 1.0.0
@@ -6,2 +6,3 @@ /* | ||
var getNumberOfLines = require("./helpers").getNumberOfLines; | ||
var getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
@@ -23,3 +24,7 @@ function CodeNode(generatedCode) { | ||
var lines = getNumberOfLines(this.generatedCode); | ||
return Array(lines+1).join(";"); | ||
if((mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode)) > 0) { | ||
return Array(lines).join(";"); | ||
} else { | ||
return Array(lines+1).join(";"); | ||
} | ||
}; | ||
@@ -32,3 +37,16 @@ | ||
CodeNode.prototype.mapGeneratedCode = function(fn) { | ||
this.generatedCode = fn(this.generatedCode); | ||
var generatedCode = fn(this.generatedCode); | ||
return new CodeNode(generatedCode); | ||
}; | ||
CodeNode.prototype.getNormalizedNodes = function() { | ||
return [this]; | ||
}; | ||
CodeNode.prototype.merge = function merge(otherNode) { | ||
if(otherNode instanceof CodeNode) { | ||
this.generatedCode += otherNode.generatedCode; | ||
return this; | ||
} | ||
return false; | ||
}; |
@@ -79,3 +79,3 @@ /* | ||
} else if(currentNode && currentNode instanceof SourceNode && !generatedCode.trim()) { | ||
currentNode.generatedCode += generatedCode; | ||
currentNode.addGeneratedCode(generatedCode); | ||
currentSourceNodeLine++; | ||
@@ -92,3 +92,3 @@ } else { | ||
) { | ||
currentNode.generatedCode += generatedCode; | ||
currentNode.addGeneratedCode(generatedCode); | ||
currentSourceNodeLine++; | ||
@@ -95,0 +95,0 @@ } else { |
@@ -14,1 +14,9 @@ /* | ||
}; | ||
exports.getUnfinishedLine = function getUnfinishedLine(str) { | ||
var idx = str.lastIndexOf("\n"); | ||
if(idx === -1) | ||
return str.length; | ||
else | ||
return str.length - idx - 1; | ||
}; |
@@ -11,2 +11,3 @@ /* | ||
this.currentSource = 0; | ||
this.unfinishedGeneratedLine = false; | ||
} | ||
@@ -13,0 +14,0 @@ module.exports = MappingsContext; |
@@ -8,2 +8,3 @@ /* | ||
var MappingsContext = require("./MappingsContext"); | ||
var getNumberOfLines = require("./helpers").getNumberOfLines; | ||
@@ -62,5 +63,24 @@ function SourceListMap(generatedCode, source, originalSource) { | ||
SourceListMap.prototype.mapGeneratedCode = function(fn) { | ||
var normalizedNodes = []; | ||
this.children.forEach(function(sln) { | ||
sln.mapGeneratedCode(fn); | ||
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; | ||
} else { | ||
optimizedNodes.push(sln); | ||
} | ||
} | ||
}); | ||
return new SourceListMap(optimizedNodes); | ||
}; | ||
@@ -77,3 +97,3 @@ | ||
var source = this.children.map(function(sln) { | ||
return sln.generatedCode; | ||
return sln.getGeneratedCode(); | ||
}).join(""); | ||
@@ -80,0 +100,0 @@ var mappings = this.children.map(function(sln) { |
@@ -5,4 +5,8 @@ /* | ||
*/ | ||
module.exports = SourceNode; // circular dependency | ||
var base64VLQ = require("./base64-vlq"); | ||
var getNumberOfLines = require("./helpers").getNumberOfLines; | ||
var getUnfinishedLine = require("./helpers").getUnfinishedLine; | ||
var SingleLineNode = require("./SingleLineNode"); | ||
@@ -14,4 +18,4 @@ function SourceNode(generatedCode, source, originalSource, startingLine) { | ||
this.startingLine = startingLine || 1; | ||
this._numberOfLines = getNumberOfLines(this.generatedCode); | ||
} | ||
module.exports = SourceNode; | ||
@@ -22,4 +26,3 @@ SourceNode.prototype.clone = function() { | ||
var LINE_MAPPING = "AACA;"; | ||
var LAST_LINE_MAPPING = "AACA"; | ||
var LINE_MAPPING = ";AACA"; | ||
@@ -30,17 +33,29 @@ SourceNode.prototype.getGeneratedCode = function() { | ||
SourceNode.prototype.addGeneratedCode = function(code) { | ||
this.generatedCode += code; | ||
this._numberOfLines += getNumberOfLines(code); | ||
}; | ||
SourceNode.prototype.getMappings = function(mappingsContext) { | ||
var lines = getNumberOfLines(this.generatedCode); | ||
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 | ||
if(lines !== 0) | ||
mappings += ";" | ||
mappingsContext.currentSource = sourceIdx; | ||
mappingsContext.currentOriginalLine = (lines || 1) + this.startingLine - 1; | ||
var unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode) | ||
mappings += Array(lines).join(LINE_MAPPING); | ||
if(lines !== 0 && this.generatedCode[this.generatedCode.length - 1] !== "\n") { | ||
mappings += LAST_LINE_MAPPING; | ||
mappingsContext.currentOriginalLine++; | ||
if(unfinishedGeneratedLine === 0) { | ||
mappings += ";"; | ||
} else { | ||
if(lines !== 0) { | ||
mappings += LINE_MAPPING; | ||
mappingsContext.currentOriginalLine++; | ||
} | ||
} | ||
@@ -51,3 +66,45 @@ return mappings; | ||
SourceNode.prototype.mapGeneratedCode = function(fn) { | ||
this.generatedCode = fn(this.generatedCode); | ||
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); | ||
results.push(new SingleLineNode(lineGenerated, this.source, this.originalSource, currentLine)); | ||
// move cursors | ||
index = nextLine; | ||
currentLine++; | ||
} | ||
return results; | ||
}; | ||
SourceNode.prototype.merge = function merge(otherNode) { | ||
if(otherNode instanceof SourceNode && | ||
this.source === otherNode.source && | ||
this.generatedCode[this.generatedCode.length - 1] === "\n" && | ||
this.startingLine + this._numberOfLines === otherNode.startingLine) { | ||
this.generatedCode += otherNode.generatedCode; | ||
this._numberOfLines += otherNode._numberOfLines; | ||
return this; | ||
} else if(otherNode instanceof SingleLineNode && | ||
this.source === otherNode.source) { | ||
if(this.generatedCode[this.generatedCode.length - 1] === "\n" && | ||
this.startingLine + this._numberOfLines === otherNode.line && | ||
otherNode._numberOfLines <= 1) { | ||
this.generatedCode += otherNode.generatedCode; | ||
this._numberOfLines += otherNode._numberOfLines | ||
return this; | ||
} | ||
} | ||
return false; | ||
}; |
{ | ||
"name": "source-list-map", | ||
"version": "0.1.8", | ||
"version": "1.0.0", | ||
"description": "Fast line to line SourceMap generator.", | ||
@@ -5,0 +5,0 @@ "author": "Tobias Koppers @sokra", |
@@ -76,7 +76,9 @@ # source-list-map | ||
``` js | ||
SourceListMap.prototype.mapGeneratedCode(fn: function) | ||
SourceListMap.prototype.mapGeneratedCode(fn: function) : SourceListMap | ||
``` | ||
Applies `fn` to each generated code block. The returned value is set as new generated code. The number of lines must not change. | ||
Applies `fn` to each generated code block (per line). The returned value is set as new generated code. Returns a new SourceListMap. | ||
Removing and adding lines is supported. The SourceMap complexity will increase when doing this. | ||
## Test | ||
@@ -88,4 +90,4 @@ | ||
Copyright (c) 2015 Tobias Koppers | ||
Copyright (c) 2017 JS Foundation | ||
MIT (http://www.opensource.org/licenses/mit-license.php) |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
25054
11
599
0
91