source-map
Advanced tools
Comparing version 0.1.14 to 0.1.15
@@ -145,2 +145,76 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
/** | ||
* Applies a SourceMap for a source file to the SourceMap. | ||
* Each mapping to the supplied source file is rewritten using the | ||
* supplied SourceMap. Note: The resolution for the resulting mappings | ||
* is the minimium of this map and the supplied map. | ||
* | ||
* @param aSourceMapConsumer The SourceMap to be applied. | ||
* @param aSourceFile Optional. The filename of the source file. | ||
* If omitted, sourceMapConsumer.file will be used. | ||
*/ | ||
SourceMapGenerator.prototype.applySourceMap = | ||
function SourceMapGenerator_applySourceMap(aSourceMapConsumer, aSourceFile) { | ||
// If aSourceFile is omitted, we will use the file property of the SourceMap | ||
if (!aSourceFile) { | ||
aSourceFile = aSourceMapConsumer.file; | ||
} | ||
var sourceRoot = this._sourceRoot; | ||
// Make "aSourceFile" relative if an absolute Url is passed. | ||
if (sourceRoot) { | ||
aSourceFile = util.relative(sourceRoot, aSourceFile); | ||
} | ||
// Applying the SourceMap can add and remove items from the sources and | ||
// the names array. | ||
var newSources = new ArraySet(); | ||
var newNames = new ArraySet(); | ||
// Find mappings for the "aSourceFile" | ||
this._mappings.forEach(function (mapping) { | ||
if (mapping.source === aSourceFile && mapping.original) { | ||
// Check if it can be mapped by the SourceMap. | ||
// Than update the mapping. | ||
var original = aSourceMapConsumer.originalPositionFor({ | ||
line: mapping.original.line, | ||
column: mapping.original.column | ||
}); | ||
if (original && original.source !== null) { | ||
// Copy mapping | ||
if (sourceRoot) { | ||
mapping.source = util.relative(sourceRoot, original.source); | ||
} else { | ||
mapping.source = original.source; | ||
} | ||
mapping.original.line = original.line; | ||
mapping.original.column = original.column; | ||
mapping.name = mapping.name && original.name || mapping.name; | ||
} | ||
} | ||
var source = mapping.source; | ||
if (source && !newSources.has(source)) { | ||
newSources.add(source); | ||
} | ||
var name = mapping.name; | ||
if (name && !newNames.has(name)) { | ||
newNames.add(name); | ||
} | ||
}, this); | ||
this._sources = newSources; | ||
this._names = newNames; | ||
// Copy sourcesContents of applied map. | ||
aSourceMapConsumer.sources.forEach(function (sourceFile) { | ||
var content = aSourceMapConsumer.sourceContentFor(sourceFile); | ||
if (content) { | ||
if (sourceRoot) { | ||
sourceFile = util.relative(sourceRoot, sourceFile); | ||
} | ||
this.setSourceContent(sourceFile, content); | ||
} | ||
}, this); | ||
}; | ||
/** | ||
* A mapping can have one of the three levels of data: | ||
@@ -147,0 +221,0 @@ * |
@@ -204,3 +204,2 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
generated.column = 0; | ||
sourceMappingActive = false; | ||
} else { | ||
@@ -207,0 +206,0 @@ generated.column++; |
@@ -110,3 +110,4 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
copy.filter.moduleDefines, | ||
removeAmdefine | ||
removeAmdefine, | ||
makeNonRelative | ||
], | ||
@@ -113,0 +114,0 @@ dest: 'dist/test/Utils.jsm' |
{ | ||
"name": "source-map", | ||
"description": "Generates and consumes source maps", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"homepage": "https://github.com/mozilla/source-map", | ||
@@ -6,0 +6,0 @@ "author": "Nick Fitzgerald <nfitzgerald@mozilla.com>", |
@@ -144,4 +144,3 @@ # Source Map | ||
* `callback`: The function that is called with each mapping. This function | ||
should not mutate the mapping. | ||
* `callback`: The function that is called with each mapping. | ||
@@ -171,2 +170,8 @@ * `context`: Optional. If specified, this object will be the value of `this` | ||
#### SourceMapGenerator.fromSourceMap(sourceMapConsumer) | ||
Creates a new SourceMapGenerator based on a SourceMapConsumer | ||
* `sourceMapConsumer` The SourceMap. | ||
#### SourceMapGenerator.prototype.addMapping(mapping) | ||
@@ -194,2 +199,14 @@ | ||
#### SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile]) | ||
Applies a SourceMap for a source file to the SourceMap. | ||
Each mapping to the supplied source file is rewritten using the | ||
supplied SourceMap. Note: The resolution for the resulting mappings | ||
is the minimium of this map and the supplied map. | ||
* `sourceMapConsumer`: The SourceMap to be applied. | ||
* `sourceFile`: Optional. The filename of the source file. | ||
If omitted, sourceMapConsumer.file will be used. | ||
#### SourceMapGenerator.prototype.toString() | ||
@@ -280,3 +297,3 @@ | ||
exports["test doing the foo bar"] = function () { | ||
exports["test doing the foo bar"] = function (assert, util) { | ||
... | ||
@@ -287,4 +304,23 @@ }; | ||
The `util` argument is the test utility module located at `test/source-map/util`. | ||
The `assert` argument is a cut down version of node's assert module. You have | ||
access to the following assertion functions: | ||
* `doesNotThrow` | ||
* `equal` | ||
* `ok` | ||
* `strictEqual` | ||
* `throws` | ||
(The reason for the restricted set of test functions is because we need the | ||
tests to run inside Firefox's test suite as well and so the assert module is | ||
shimmed in that environment. See `build/assert-shim.js`.) | ||
[format]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit | ||
[feature]: https://wiki.mozilla.org/DevTools/Features/SourceMap | ||
[Dryice]: https://github.com/mozilla/dryice |
@@ -14,2 +14,3 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer; | ||
var SourceNode = require('../../lib/source-map/source-node').SourceNode; | ||
var util = require('./util'); | ||
@@ -177,13 +178,3 @@ | ||
assert.equal(map.version, 3); | ||
assert.equal(map.file, 'min.js'); | ||
assert.equal(map.names.length, 3); | ||
assert.equal(map.names[0], 'bar'); | ||
assert.equal(map.names[1], 'baz'); | ||
assert.equal(map.names[2], 'n'); | ||
assert.equal(map.sources.length, 2); | ||
assert.equal(map.sources[0], 'one.js'); | ||
assert.equal(map.sources[1], 'two.js'); | ||
assert.equal(map.sourceRoot, '/the/root'); | ||
assert.equal(map.mappings, 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'); | ||
util.assertEqualMaps(assert, map, util.testMap); | ||
}; | ||
@@ -209,4 +200,6 @@ | ||
map = JSON.parse(map.toString()); | ||
assert.deepEqual(map.sources, ['one.js', 'two.js']); | ||
assert.deepEqual(map.sourcesContent, ['one file content', null]); | ||
assert.equal(map.sources[0], 'one.js'); | ||
assert.equal(map.sources[1], 'two.js'); | ||
assert.equal(map.sourcesContent[0], 'one file content'); | ||
assert.equal(map.sourcesContent[1], null); | ||
}; | ||
@@ -216,3 +209,3 @@ | ||
var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap)); | ||
assert.deepEqual(map.toJSON(), util.testMap); | ||
util.assertEqualMaps(assert, map.toJSON(), util.testMap); | ||
}; | ||
@@ -223,6 +216,61 @@ | ||
new SourceMapConsumer(util.testMapWithSourcesContent)); | ||
util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent); | ||
}; | ||
assert.deepEqual(map.toJSON().sourcesContent, util.testMapWithSourcesContent.sourcesContent); | ||
assert.deepEqual(map.toJSON(), util.testMapWithSourcesContent); | ||
exports['test applySourceMap'] = function (assert, util) { | ||
var node = new SourceNode(null, null, null, [ | ||
new SourceNode(2, 0, 'fileX', 'lineX2\n'), | ||
'genA1\n', | ||
new SourceNode(2, 0, 'fileY', 'lineY2\n'), | ||
'genA2\n', | ||
new SourceNode(1, 0, 'fileX', 'lineX1\n'), | ||
'genA3\n', | ||
new SourceNode(1, 0, 'fileY', 'lineY1\n') | ||
]); | ||
var mapStep1 = node.toStringWithSourceMap({ | ||
file: 'fileA' | ||
}).map; | ||
mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n'); | ||
mapStep1 = mapStep1.toJSON(); | ||
node = new SourceNode(null, null, null, [ | ||
'gen1\n', | ||
new SourceNode(1, 0, 'fileA', 'lineA1\n'), | ||
new SourceNode(2, 0, 'fileA', 'lineA2\n'), | ||
new SourceNode(3, 0, 'fileA', 'lineA3\n'), | ||
new SourceNode(4, 0, 'fileA', 'lineA4\n'), | ||
new SourceNode(1, 0, 'fileB', 'lineB1\n'), | ||
new SourceNode(2, 0, 'fileB', 'lineB2\n'), | ||
'gen2\n' | ||
]); | ||
var mapStep2 = node.toStringWithSourceMap({ | ||
file: 'fileGen' | ||
}).map; | ||
mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n'); | ||
mapStep2 = mapStep2.toJSON(); | ||
node = new SourceNode(null, null, null, [ | ||
'gen1\n', | ||
new SourceNode(2, 0, 'fileX', 'lineA1\n'), | ||
new SourceNode(2, 0, 'fileA', 'lineA2\n'), | ||
new SourceNode(2, 0, 'fileY', 'lineA3\n'), | ||
new SourceNode(4, 0, 'fileA', 'lineA4\n'), | ||
new SourceNode(1, 0, 'fileB', 'lineB1\n'), | ||
new SourceNode(2, 0, 'fileB', 'lineB2\n'), | ||
'gen2\n' | ||
]); | ||
var expectedMap = node.toStringWithSourceMap({ | ||
file: 'fileGen' | ||
}).map; | ||
expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n'); | ||
expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n'); | ||
expectedMap = expectedMap.toJSON(); | ||
// apply source map "mapStep1" to "mapStep2" | ||
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2)); | ||
generator.applySourceMap(new SourceMapConsumer(mapStep1)); | ||
var actualMap = generator.toJSON(); | ||
util.assertEqualMaps(assert, actualMap, expectedMap); | ||
}; | ||
}); |
@@ -97,2 +97,43 @@ /* -*- Mode: js; js-indent-level: 2; -*- */ | ||
function assertEqualMaps(assert, actualMap, expectedMap) { | ||
assert.equal(actualMap.version, expectedMap.version, "version mismatch"); | ||
assert.equal(actualMap.file, expectedMap.file, "file mismatch"); | ||
assert.equal(actualMap.names.length, | ||
expectedMap.names.length, | ||
"names length mismatch: " + | ||
actualMap.names.join(", ") + " != " + expectedMap.names.join(", ")); | ||
for (var i = 0; i < actualMap.names.length; i++) { | ||
assert.equal(actualMap.names[i], | ||
expectedMap.names[i], | ||
"names[" + i + "] mismatch: " + | ||
actualMap.names.join(", ") + " != " + expectedMap.names.join(", ")); | ||
} | ||
assert.equal(actualMap.sources.length, | ||
expectedMap.sources.length, | ||
"sources length mismatch: " + | ||
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", ")); | ||
for (var i = 0; i < actualMap.sources.length; i++) { | ||
assert.equal(actualMap.sources[i], | ||
expectedMap.sources[i], | ||
"sources[" + i + "] length mismatch: " + | ||
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", ")); | ||
} | ||
assert.equal(actualMap.sourceRoot, | ||
expectedMap.sourceRoot, | ||
"sourceRoot mismatch: " + | ||
actualMap.sourceRoot + " != " + expectedMap.sourceRoot); | ||
assert.equal(actualMap.mappings, expectedMap.mappings, "mappings mismatch"); | ||
if (actualMap.sourcesContent) { | ||
assert.equal(actualMap.sourcesContent.length, | ||
expectedMap.sourcesContent.length, | ||
"sourcesContent length mismatch"); | ||
for (var i = 0; i < actualMap.sourcesContent.length; i++) { | ||
assert.equal(actualMap.sourcesContent[i], | ||
expectedMap.sourcesContent[i], | ||
"sourcesContent[" + i + "] mismatch"); | ||
} | ||
} | ||
} | ||
exports.assertEqualMaps = assertEqualMaps; | ||
}); |
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
112531
2688
322