source-map
Advanced tools
Comparing version 0.1.33 to 0.1.34
# Change Log | ||
## 0.1.34 | ||
* Make `SourceNode` work with windows style ("\r\n") newlines. Issue #103. | ||
* Fix bug involving source contents and the | ||
`SourceMapGenerator.prototype.applySourceMap`. Issue #100. | ||
## 0.1.33 | ||
@@ -47,3 +54,3 @@ | ||
* Allow duplicate entries in the `names` and `sources` arrays of source maps | ||
(usually from TypeScript) we are parsing. Fixes github isse 72. | ||
(usually from TypeScript) we are parsing. Fixes github issue 72. | ||
@@ -50,0 +57,0 @@ ## 0.1.28 |
@@ -232,2 +232,5 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
if (content) { | ||
if (aSourceMapPath) { | ||
sourceFile = util.join(aSourceMapPath, sourceFile); | ||
} | ||
if (sourceRoot) { | ||
@@ -234,0 +237,0 @@ sourceFile = util.relative(sourceRoot, sourceFile); |
@@ -15,2 +15,9 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
// Matches a Windows-style `\r\n` newline or a `\n` newline used by all other | ||
// operating systems these days (capturing the result). | ||
var REGEX_NEWLINE = /(\r?\n)/g; | ||
// Matches a Windows-style newline, or any character. | ||
var REGEX_CHARACTER = /\r\n|[\s\S]/g; | ||
/** | ||
@@ -50,5 +57,13 @@ * SourceNodes provide a way to abstract over interpolating/concatenating | ||
// The generated code | ||
// Processed fragments are removed from this array. | ||
var remainingLines = aGeneratedCode.split('\n'); | ||
// All even indices of this array are one line of the generated code, | ||
// while all odd indices are the newlines between two adjacent lines | ||
// (since `REGEX_NEWLINE` captures its match). | ||
// Processed fragments are removed from this array, by calling `shiftNextLine`. | ||
var remainingLines = aGeneratedCode.split(REGEX_NEWLINE); | ||
var shiftNextLine = function() { | ||
var lineContents = remainingLines.shift(); | ||
// The last line of a file might not have a newline. | ||
var newLine = remainingLines.shift() || ""; | ||
return lineContents + newLine; | ||
}; | ||
@@ -70,3 +85,3 @@ // We need to remember the position of "remainingLines" | ||
// Associate first line with "lastMapping" | ||
addMappingWithCode(lastMapping, remainingLines.shift() + "\n"); | ||
addMappingWithCode(lastMapping, shiftNextLine()); | ||
lastGeneratedLine++; | ||
@@ -95,3 +110,3 @@ lastGeneratedColumn = 0; | ||
while (lastGeneratedLine < mapping.generatedLine) { | ||
node.add(remainingLines.shift() + "\n"); | ||
node.add(shiftNextLine()); | ||
lastGeneratedLine++; | ||
@@ -111,8 +126,6 @@ } | ||
// Associate the remaining code in the current line with "lastMapping" | ||
var lastLine = remainingLines.shift(); | ||
if (remainingLines.length > 0) lastLine += "\n"; | ||
addMappingWithCode(lastMapping, lastLine); | ||
addMappingWithCode(lastMapping, shiftNextLine()); | ||
} | ||
// and add the remaining lines without any mapping | ||
node.add(remainingLines.join("\n")); | ||
node.add(remainingLines.join("")); | ||
} | ||
@@ -356,4 +369,4 @@ | ||
} | ||
chunk.split('').forEach(function (ch, idx, array) { | ||
if (ch === '\n') { | ||
chunk.match(REGEX_CHARACTER).forEach(function (ch, idx, array) { | ||
if (REGEX_NEWLINE.test(ch)) { | ||
generated.line++; | ||
@@ -380,3 +393,3 @@ generated.column = 0; | ||
} else { | ||
generated.column++; | ||
generated.column += ch.length; | ||
} | ||
@@ -383,0 +396,0 @@ }); |
{ | ||
"name": "source-map", | ||
"description": "Generates and consumes source maps", | ||
"version": "0.1.33", | ||
"version": "0.1.34", | ||
"homepage": "https://github.com/mozilla/source-map", | ||
@@ -27,3 +27,4 @@ "author": "Nick Fitzgerald <nfitzgerald@mozilla.com>", | ||
"Simon Lydell <simon.lydell@gmail.com>", | ||
"Jmeas Smith <jellyes2@gmail.com>" | ||
"Jmeas Smith <jellyes2@gmail.com>", | ||
"Michael Z Goddard <mzgoddard@gmail.com>" | ||
], | ||
@@ -30,0 +31,0 @@ "repository": { |
@@ -312,2 +312,3 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
}); | ||
bundleMap.setSourceContent('../coffee/foo.coffee', 'foo coffee'); | ||
bundleMap.addMapping({ | ||
@@ -318,2 +319,3 @@ generated: { line: 13, column: 13 }, | ||
}); | ||
bundleMap.setSourceContent('/bar.coffee', 'bar coffee'); | ||
bundleMap.addMapping({ | ||
@@ -324,2 +326,6 @@ generated: { line: 23, column: 23 }, | ||
}); | ||
bundleMap.setSourceContent( | ||
'http://www.example.com/baz.coffee', | ||
'baz coffee' | ||
); | ||
bundleMap = new SourceMapConsumer(bundleMap.toJSON()); | ||
@@ -358,2 +364,3 @@ | ||
}); | ||
map.setSourceContent(sources[0], 'foo coffee'); | ||
map.addMapping({ | ||
@@ -364,2 +371,3 @@ generated: { line: 11, column: 11 }, | ||
}); | ||
map.setSourceContent(sources[1], 'bar coffee'); | ||
map.addMapping({ | ||
@@ -370,2 +378,3 @@ generated: { line: 21, column: 21 }, | ||
}); | ||
map.setSourceContent(sources[2], 'baz coffee'); | ||
return map.toJSON(); | ||
@@ -372,0 +381,0 @@ } |
@@ -16,2 +16,8 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
function forEachNewline(fn) { | ||
return function (assert, util) { | ||
['\n', '\r\n'].forEach(fn.bind(null, assert, util)); | ||
} | ||
} | ||
exports['test .add()'] = function (assert, util) { | ||
@@ -132,14 +138,23 @@ var node = new SourceNode(null, null, null); | ||
exports['test .toStringWithSourceMap()'] = function (assert, util) { | ||
exports['test .toStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) { | ||
var node = new SourceNode(null, null, null, | ||
['(function () {\n', | ||
['(function () {' + nl, | ||
' ', | ||
new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'), | ||
new SourceNode(1, 8, 'a.js', '()'), | ||
';\n', | ||
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n', | ||
';' + nl, | ||
' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';' + nl, | ||
'}());']); | ||
var map = node.toStringWithSourceMap({ | ||
var result = node.toStringWithSourceMap({ | ||
file: 'foo.js' | ||
}).map; | ||
}); | ||
assert.equal(result.code, [ | ||
'(function () {', | ||
' someCall();', | ||
' if (foo) bar();', | ||
'}());' | ||
].join(nl)); | ||
var map = result.map; | ||
var mapWithoutOptions = node.toStringWithSourceMap().map; | ||
@@ -196,7 +211,8 @@ | ||
assert.equal(actual.column, null); | ||
}; | ||
}); | ||
exports['test .fromStringWithSourceMap()'] = function (assert, util) { | ||
exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) { | ||
var testCode = util.testGeneratedCode.replace(/\n/g, nl); | ||
var node = SourceNode.fromStringWithSourceMap( | ||
util.testGeneratedCode, | ||
testCode, | ||
new SourceMapConsumer(util.testMap)); | ||
@@ -210,3 +226,3 @@ | ||
assert.equal(code, util.testGeneratedCode); | ||
assert.equal(code, testCode); | ||
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); | ||
@@ -217,7 +233,7 @@ map = map.toJSON(); | ||
assert.equal(map.mappings, util.testMap.mappings); | ||
}; | ||
}); | ||
exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) { | ||
exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) { | ||
var node = SourceNode.fromStringWithSourceMap( | ||
util.testGeneratedCode, | ||
util.testGeneratedCode.replace(/\n/g, nl), | ||
new SourceMapConsumer(util.emptyMap)); | ||
@@ -230,3 +246,3 @@ var result = node.toStringWithSourceMap({ | ||
assert.equal(code, util.testGeneratedCode); | ||
assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl)); | ||
assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); | ||
@@ -238,11 +254,11 @@ map = map.toJSON(); | ||
assert.equal(map.mappings, util.emptyMap.mappings); | ||
}; | ||
}); | ||
exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) { | ||
exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) { | ||
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", | ||
"(function() {" + nl, | ||
" var Test = {};" + nl, | ||
" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl), | ||
" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl, | ||
"}());" + nl, | ||
"/* Generated Source */"]); | ||
@@ -268,11 +284,11 @@ input = input.toStringWithSourceMap({ | ||
util.assertEqualMaps(assert, map, inputMap); | ||
}; | ||
}); | ||
exports['test .toStringWithSourceMap() merging duplicate mappings'] = function (assert, util) { | ||
exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) { | ||
var input = new SourceNode(null, null, null, [ | ||
new SourceNode(1, 0, "a.js", "(function"), | ||
new SourceNode(1, 0, "a.js", "() {\n"), | ||
new SourceNode(1, 0, "a.js", "() {" + nl), | ||
" ", | ||
new SourceNode(1, 0, "a.js", "var Test = "), | ||
new SourceNode(1, 0, "b.js", "{};\n"), | ||
new SourceNode(1, 0, "b.js", "{};" + nl), | ||
new SourceNode(2, 0, "b.js", "Test"), | ||
@@ -282,4 +298,4 @@ new SourceNode(2, 0, "b.js", ".A", "A"), | ||
"1234", | ||
new SourceNode(2, 40, "b.js", " };\n", "A"), | ||
"}());\n", | ||
new SourceNode(2, 40, "b.js", " };" + nl, "A"), | ||
"}());" + nl, | ||
"/* Generated Source */" | ||
@@ -291,2 +307,10 @@ ]); | ||
assert.equal(input.code, [ | ||
"(function() {", | ||
" var Test = {};", | ||
"Test.A = { value: 1234 };", | ||
"}());", | ||
"/* Generated Source */" | ||
].join(nl)) | ||
var correctMap = new SourceMapGenerator({ | ||
@@ -346,13 +370,13 @@ file: 'foo.js' | ||
util.assertEqualMaps(assert, inputMap, correctMap); | ||
}; | ||
}); | ||
exports['test .toStringWithSourceMap() multi-line SourceNodes'] = function (assert, util) { | ||
exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) { | ||
var input = new SourceNode(null, null, null, [ | ||
new SourceNode(1, 0, "a.js", "(function() {\nvar nextLine = 1;\nanotherLine();\n"), | ||
new SourceNode(2, 2, "b.js", "Test.call(this, 123);\n"), | ||
new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';\n"), | ||
new SourceNode(2, 2, "b.js", "anotherLine();\n"), | ||
"/*\nGenerated\nSource\n*/\n", | ||
new SourceNode(3, 4, "c.js", "anotherLine();\n"), | ||
"/*\nGenerated\nSource\n*/" | ||
new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl), | ||
new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl), | ||
new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl), | ||
new SourceNode(2, 2, "b.js", "anotherLine();" + nl), | ||
"/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl, | ||
new SourceNode(3, 4, "c.js", "anotherLine();" + nl), | ||
"/*" + nl + "Generated" + nl + "Source" + nl + "*/" | ||
]); | ||
@@ -363,2 +387,20 @@ input = input.toStringWithSourceMap({ | ||
assert.equal(input.code, [ | ||
"(function() {", | ||
"var nextLine = 1;", | ||
"anotherLine();", | ||
"Test.call(this, 123);", | ||
"this['stuff'] = 'v';", | ||
"anotherLine();", | ||
"/*", | ||
"Generated", | ||
"Source", | ||
"*/", | ||
"anotherLine();", | ||
"/*", | ||
"Generated", | ||
"Source", | ||
"*/" | ||
].join(nl)); | ||
var correctMap = new SourceMapGenerator({ | ||
@@ -406,2 +448,8 @@ file: 'foo.js' | ||
util.assertEqualMaps(assert, inputMap, correctMap); | ||
}); | ||
exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) { | ||
var node = new SourceNode(1, 0, 'empty.js', ''); | ||
var result = node.toStringWithSourceMap(); | ||
assert.equal(result.code, ''); | ||
}; | ||
@@ -408,0 +456,0 @@ |
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
170689
38
4026