Skip to content

Commit

Permalink
Merge branch 'source-map-gen-apply-map'
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Mar 20, 2013
2 parents 347944f + 90fd68f commit aec9c40
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions lib/source-map/source-map-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,77 @@ define(function (require, exports, module) {
}
};

/**
* 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.
*/
SourceMapGenerator.prototype.applySourceMap =
function SourceMapGenerator_applySourceMap(aSourceFile, aSourceMapConsumer) {
// aSourceFile is an optional argument
if(!aSourceMapConsumer) {
aSourceMapConsumer = aSourceFile;
aSourceFile = aSourceMapConsumer.file;
}
var sourceRoot = this._sourceRoot;
// 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
mapping.source = original.source;
mapping.original.line = original.line;
mapping.original.column = original.column;
mapping.name = mapping.name && original.name || mapping.name;
// Try to make source file relative to our sourceRoot
if(sourceRoot) {
var relativeUrl = util.relative(sourceRoot, mapping.source);
if(relativeUrl) {
mapping.source = relativeUrl;
}
}
}
}

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;
aSourceMapConsumer.sources.forEach(function (sourceFile) {
var content = aSourceMapConsumer.sourceContentFor(sourceFile);
if(content) {
if(sourceRoot) {
var relativeUrl = util.relative(sourceRoot, sourceFile);
if(relativeUrl) {
sourceFile = relativeUrl;
}
}
generator.setSourceContent(sourceFile, content);
}
});
};

/**
* A mapping can have one of the three levels of data:
*
Expand Down

0 comments on commit aec9c40

Please sign in to comment.