source-map
Advanced tools
Comparing version 0.1.16 to 0.1.17
@@ -277,3 +277,3 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
"generatedColumn", | ||
this._compareGeneratedPositions) | ||
this._compareGeneratedPositions); | ||
@@ -357,3 +357,3 @@ if (mapping) { | ||
"originalColumn", | ||
this._compareOriginalPositions) | ||
this._compareOriginalPositions); | ||
@@ -360,0 +360,0 @@ if (mapping) { |
@@ -36,2 +36,97 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
/** | ||
* Creates a SourceNode from generated code and a SourceMapConsumer. | ||
* | ||
* @param aGeneratedCode The generated code | ||
* @param aSourceMapConsumer The SourceMap for the generated code | ||
*/ | ||
SourceNode.fromStringWithSourceMap = | ||
function SourceNode_fromStringWithSourceMap(aGeneratedCode, aSourceMapConsumer) { | ||
// The SourceNode we want to fill with the generated code | ||
// and the SourceMap | ||
var node = new SourceNode(); | ||
// The generated code | ||
// Processed fragments are removed from this array. | ||
var remainingLines = aGeneratedCode.split('\n'); | ||
// We need to remember the position of "remainingLines" | ||
var lastGeneratedLine = 1, lastGeneratedColumn = 0; | ||
// The generate SourceNodes we need a code range. | ||
// To extract it current and last mapping is used. | ||
// Here we store the last mapping. | ||
var lastMapping = null; | ||
aSourceMapConsumer.eachMapping(function (mapping) { | ||
if (lastMapping === null) { | ||
// We add the generated code until the first mapping | ||
// to the SourceNode without any mapping. | ||
// Each line is added as separate string. | ||
while (lastGeneratedLine < mapping.generatedLine) { | ||
node.add(remainingLines.shift() + "\n"); | ||
lastGeneratedLine++; | ||
} | ||
if (lastGeneratedColumn < mapping.generatedColumn) { | ||
var nextLine = remainingLines[0]; | ||
node.add(nextLine.substr(0, mapping.generatedColumn)); | ||
remainingLines[0] = nextLine.substr(mapping.generatedColumn); | ||
lastGeneratedColumn = mapping.generatedColumn; | ||
} | ||
} else { | ||
// We add the code from "lastMapping" to "mapping": | ||
// First check if there is a new line in between. | ||
if (lastGeneratedLine < mapping.generatedLine) { | ||
var code = ""; | ||
// Associate full lines with "lastMapping" | ||
do { | ||
code += remainingLines.shift() + "\n"; | ||
lastGeneratedLine++; | ||
lastGeneratedColumn = 0; | ||
} while (lastGeneratedLine < mapping.generatedLine); | ||
// When we reached the correct line, we add code until we | ||
// reach the correct column too. | ||
if (lastGeneratedColumn < mapping.generatedColumn) { | ||
var nextLine = remainingLines[0]; | ||
code += nextLine.substr(0, mapping.generatedColumn); | ||
remainingLines[0] = nextLine.substr(mapping.generatedColumn); | ||
lastGeneratedColumn = mapping.generatedColumn; | ||
} | ||
// Create the SourceNode. | ||
addMappingWithCode(lastMapping, code); | ||
} else { | ||
// There is no new line in between. | ||
// Associate the code between "lastGeneratedColumn" and | ||
// "mapping.generatedColumn" with "lastMapping" | ||
var nextLine = remainingLines[0]; | ||
var code = nextLine.substr(0, mapping.generatedColumn - | ||
lastGeneratedColumn); | ||
remainingLines[0] = nextLine.substr(mapping.generatedColumn - | ||
lastGeneratedColumn); | ||
lastGeneratedColumn = mapping.generatedColumn; | ||
addMappingWithCode(lastMapping, code); | ||
} | ||
} | ||
lastMapping = mapping; | ||
}, this); | ||
// We have processed all mappings. | ||
// Associate the remaining code in the current line with "lastMapping" | ||
// and add the remaining lines without any mapping | ||
addMappingWithCode(lastMapping, remainingLines.join("\n")); | ||
return node; | ||
function addMappingWithCode(mapping, code) { | ||
if (mapping.source === undefined) { | ||
node.add(code); | ||
} else { | ||
node.add(new SourceNode(mapping.originalLine, | ||
mapping.originalColumn, | ||
mapping.source, | ||
code, | ||
mapping.name)); | ||
} | ||
} | ||
}; | ||
/** | ||
* Add a chunk of generated JS to this source node. | ||
@@ -116,3 +211,3 @@ * | ||
var i; | ||
var len = this.children.length | ||
var len = this.children.length; | ||
if (len > 0) { | ||
@@ -119,0 +214,0 @@ newChildren = []; |
@@ -50,3 +50,3 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
function toSetString(aStr) { | ||
return '$' + aStr | ||
return '$' + aStr; | ||
} | ||
@@ -53,0 +53,0 @@ exports.toSetString = toSetString; |
{ | ||
"name": "source-map", | ||
"description": "Generates and consumes source maps", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"homepage": "https://github.com/mozilla/source-map", | ||
@@ -6,0 +6,0 @@ "author": "Nick Fitzgerald <nfitzgerald@mozilla.com>", |
@@ -236,2 +236,10 @@ # Source Map | ||
#### SourceNode.fromStringWithSourceMap(code, sourceMapConsumer) | ||
Creates a SourceNode from generated code and a SourceMapConsumer. | ||
* `code`: The generated code | ||
* `sourceMapConsumer` The SourceMap for the generated code | ||
#### SourceNode.prototype.add(chunk) | ||
@@ -238,0 +246,0 @@ |
@@ -191,2 +191,55 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
exports['test .fromStringWithSourceMap()'] = function (assert, util) { | ||
var node = SourceNode.fromStringWithSourceMap( | ||
util.testGeneratedCode, | ||
new SourceMapConsumer(util.testMap)); | ||
var result = node.toStringWithSourceMap({ | ||
file: 'min.js' | ||
}); | ||
var map = result.map; | ||
var code = result.code; | ||
assert.equal(code, util.testGeneratedCode); | ||
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); | ||
map = map.toJSON(); | ||
assert.equal(map.version, util.testMap.version); | ||
assert.equal(map.file, util.testMap.file); | ||
assert.equal(map.mappings, util.testMap.mappings); | ||
}; | ||
exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) { | ||
var input = new SourceNode(null, null, null, [ | ||
"(function() {\n", | ||
" var Test = {};\n", | ||
" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"), | ||
" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n", | ||
"}());\n", | ||
"/* Generated Source */"]); | ||
input = input.toStringWithSourceMap({ | ||
file: 'foo.js' | ||
}); | ||
var node = SourceNode.fromStringWithSourceMap( | ||
input.code, | ||
new SourceMapConsumer(input.map.toString())); | ||
var result = node.toStringWithSourceMap({ | ||
file: 'foo.js' | ||
}); | ||
var map = result.map; | ||
var code = result.code; | ||
assert.equal(code, input.code); | ||
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); | ||
map = map.toJSON(); | ||
var inputMap = input.map.toJSON(); | ||
assert.equal(map.version, inputMap.version); | ||
assert.equal(map.file, inputMap.file); | ||
assert.equal(map.mappings, inputMap.mappings); | ||
assert.deepEqual(map.sources, inputMap.sources); | ||
assert.equal(map.sourceRoot, inputMap.sourceRoot); | ||
assert.deepEqual(map.names, inputMap.names); | ||
}; | ||
}); |
@@ -33,2 +33,4 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
// TWO.inc=function(a){return a+1;}; | ||
exports.testGeneratedCode = " ONE.foo=function(a){return baz(a);};\n"+ | ||
" TWO.inc=function(a){return a+1;};"; | ||
exports.testMap = { | ||
@@ -35,0 +37,0 @@ version: 3, |
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
118972
2826
330