react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / node_modules / source-map / lib / source-map / source-map-generator.js
80766 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/6if (typeof define !== 'function') {7var define = require('amdefine')(module, require);8}9define(function (require, exports, module) {1011var base64VLQ = require('./base64-vlq');12var util = require('./util');13var ArraySet = require('./array-set').ArraySet;1415/**16* An instance of the SourceMapGenerator represents a source map which is17* being built incrementally. You may pass an object with the following18* properties:19*20* - file: The filename of the generated source.21* - sourceRoot: A root for all relative URLs in this source map.22*/23function SourceMapGenerator(aArgs) {24if (!aArgs) {25aArgs = {};26}27this._file = util.getArg(aArgs, 'file', null);28this._sourceRoot = util.getArg(aArgs, 'sourceRoot', null);29this._sources = new ArraySet();30this._names = new ArraySet();31this._mappings = [];32this._sourcesContents = null;33}3435SourceMapGenerator.prototype._version = 3;3637/**38* Creates a new SourceMapGenerator based on a SourceMapConsumer39*40* @param aSourceMapConsumer The SourceMap.41*/42SourceMapGenerator.fromSourceMap =43function SourceMapGenerator_fromSourceMap(aSourceMapConsumer) {44var sourceRoot = aSourceMapConsumer.sourceRoot;45var generator = new SourceMapGenerator({46file: aSourceMapConsumer.file,47sourceRoot: sourceRoot48});49aSourceMapConsumer.eachMapping(function (mapping) {50var newMapping = {51generated: {52line: mapping.generatedLine,53column: mapping.generatedColumn54}55};5657if (mapping.source) {58newMapping.source = mapping.source;59if (sourceRoot) {60newMapping.source = util.relative(sourceRoot, newMapping.source);61}6263newMapping.original = {64line: mapping.originalLine,65column: mapping.originalColumn66};6768if (mapping.name) {69newMapping.name = mapping.name;70}71}7273generator.addMapping(newMapping);74});75aSourceMapConsumer.sources.forEach(function (sourceFile) {76var content = aSourceMapConsumer.sourceContentFor(sourceFile);77if (content) {78generator.setSourceContent(sourceFile, content);79}80});81return generator;82};8384/**85* Add a single mapping from original source line and column to the generated86* source's line and column for this source map being created. The mapping87* object should have the following properties:88*89* - generated: An object with the generated line and column positions.90* - original: An object with the original line and column positions.91* - source: The original source file (relative to the sourceRoot).92* - name: An optional original token name for this mapping.93*/94SourceMapGenerator.prototype.addMapping =95function SourceMapGenerator_addMapping(aArgs) {96var generated = util.getArg(aArgs, 'generated');97var original = util.getArg(aArgs, 'original', null);98var source = util.getArg(aArgs, 'source', null);99var name = util.getArg(aArgs, 'name', null);100101this._validateMapping(generated, original, source, name);102103if (source && !this._sources.has(source)) {104this._sources.add(source);105}106107if (name && !this._names.has(name)) {108this._names.add(name);109}110111this._mappings.push({112generatedLine: generated.line,113generatedColumn: generated.column,114originalLine: original != null && original.line,115originalColumn: original != null && original.column,116source: source,117name: name118});119};120121/**122* Set the source content for a source file.123*/124SourceMapGenerator.prototype.setSourceContent =125function SourceMapGenerator_setSourceContent(aSourceFile, aSourceContent) {126var source = aSourceFile;127if (this._sourceRoot) {128source = util.relative(this._sourceRoot, source);129}130131if (aSourceContent !== null) {132// Add the source content to the _sourcesContents map.133// Create a new _sourcesContents map if the property is null.134if (!this._sourcesContents) {135this._sourcesContents = {};136}137this._sourcesContents[util.toSetString(source)] = aSourceContent;138} else {139// Remove the source file from the _sourcesContents map.140// If the _sourcesContents map is empty, set the property to null.141delete this._sourcesContents[util.toSetString(source)];142if (Object.keys(this._sourcesContents).length === 0) {143this._sourcesContents = null;144}145}146};147148/**149* Applies the mappings of a sub-source-map for a specific source file to the150* source map being generated. Each mapping to the supplied source file is151* rewritten using the supplied source map. Note: The resolution for the152* resulting mappings is the minimium of this map and the supplied map.153*154* @param aSourceMapConsumer The source map to be applied.155* @param aSourceFile Optional. The filename of the source file.156* If omitted, SourceMapConsumer's file property will be used.157* @param aSourceMapPath Optional. The dirname of the path to the source map158* to be applied. If relative, it is relative to the SourceMapConsumer.159* This parameter is needed when the two source maps aren't in the same160* directory, and the source map to be applied contains relative source161* paths. If so, those relative source paths need to be rewritten162* relative to the SourceMapGenerator.163*/164SourceMapGenerator.prototype.applySourceMap =165function SourceMapGenerator_applySourceMap(aSourceMapConsumer, aSourceFile, aSourceMapPath) {166// If aSourceFile is omitted, we will use the file property of the SourceMap167if (!aSourceFile) {168if (!aSourceMapConsumer.file) {169throw new Error(170'SourceMapGenerator.prototype.applySourceMap requires either an explicit source file, ' +171'or the source map\'s "file" property. Both were omitted.'172);173}174aSourceFile = aSourceMapConsumer.file;175}176var sourceRoot = this._sourceRoot;177// Make "aSourceFile" relative if an absolute Url is passed.178if (sourceRoot) {179aSourceFile = util.relative(sourceRoot, aSourceFile);180}181// Applying the SourceMap can add and remove items from the sources and182// the names array.183var newSources = new ArraySet();184var newNames = new ArraySet();185186// Find mappings for the "aSourceFile"187this._mappings.forEach(function (mapping) {188if (mapping.source === aSourceFile && mapping.originalLine) {189// Check if it can be mapped by the source map, then update the mapping.190var original = aSourceMapConsumer.originalPositionFor({191line: mapping.originalLine,192column: mapping.originalColumn193});194if (original.source !== null) {195// Copy mapping196mapping.source = original.source;197if (aSourceMapPath) {198mapping.source = util.join(aSourceMapPath, mapping.source)199}200if (sourceRoot) {201mapping.source = util.relative(sourceRoot, mapping.source);202}203mapping.originalLine = original.line;204mapping.originalColumn = original.column;205if (original.name !== null && mapping.name !== null) {206// Only use the identifier name if it's an identifier207// in both SourceMaps208mapping.name = original.name;209}210}211}212213var source = mapping.source;214if (source && !newSources.has(source)) {215newSources.add(source);216}217218var name = mapping.name;219if (name && !newNames.has(name)) {220newNames.add(name);221}222223}, this);224this._sources = newSources;225this._names = newNames;226227// Copy sourcesContents of applied map.228aSourceMapConsumer.sources.forEach(function (sourceFile) {229var content = aSourceMapConsumer.sourceContentFor(sourceFile);230if (content) {231if (aSourceMapPath) {232sourceFile = util.join(aSourceMapPath, sourceFile);233}234if (sourceRoot) {235sourceFile = util.relative(sourceRoot, sourceFile);236}237this.setSourceContent(sourceFile, content);238}239}, this);240};241242/**243* A mapping can have one of the three levels of data:244*245* 1. Just the generated position.246* 2. The Generated position, original position, and original source.247* 3. Generated and original position, original source, as well as a name248* token.249*250* To maintain consistency, we validate that any new mapping being added falls251* in to one of these categories.252*/253SourceMapGenerator.prototype._validateMapping =254function SourceMapGenerator_validateMapping(aGenerated, aOriginal, aSource,255aName) {256if (aGenerated && 'line' in aGenerated && 'column' in aGenerated257&& aGenerated.line > 0 && aGenerated.column >= 0258&& !aOriginal && !aSource && !aName) {259// Case 1.260return;261}262else if (aGenerated && 'line' in aGenerated && 'column' in aGenerated263&& aOriginal && 'line' in aOriginal && 'column' in aOriginal264&& aGenerated.line > 0 && aGenerated.column >= 0265&& aOriginal.line > 0 && aOriginal.column >= 0266&& aSource) {267// Cases 2 and 3.268return;269}270else {271throw new Error('Invalid mapping: ' + JSON.stringify({272generated: aGenerated,273source: aSource,274original: aOriginal,275name: aName276}));277}278};279280/**281* Serialize the accumulated mappings in to the stream of base 64 VLQs282* specified by the source map format.283*/284SourceMapGenerator.prototype._serializeMappings =285function SourceMapGenerator_serializeMappings() {286var previousGeneratedColumn = 0;287var previousGeneratedLine = 1;288var previousOriginalColumn = 0;289var previousOriginalLine = 0;290var previousName = 0;291var previousSource = 0;292var result = '';293var mapping;294295// The mappings must be guaranteed to be in sorted order before we start296// serializing them or else the generated line numbers (which are defined297// via the ';' separators) will be all messed up. Note: it might be more298// performant to maintain the sorting as we insert them, rather than as we299// serialize them, but the big O is the same either way.300this._mappings.sort(util.compareByGeneratedPositions);301302for (var i = 0, len = this._mappings.length; i < len; i++) {303mapping = this._mappings[i];304305if (mapping.generatedLine !== previousGeneratedLine) {306previousGeneratedColumn = 0;307while (mapping.generatedLine !== previousGeneratedLine) {308result += ';';309previousGeneratedLine++;310}311}312else {313if (i > 0) {314if (!util.compareByGeneratedPositions(mapping, this._mappings[i - 1])) {315continue;316}317result += ',';318}319}320321result += base64VLQ.encode(mapping.generatedColumn322- previousGeneratedColumn);323previousGeneratedColumn = mapping.generatedColumn;324325if (mapping.source) {326result += base64VLQ.encode(this._sources.indexOf(mapping.source)327- previousSource);328previousSource = this._sources.indexOf(mapping.source);329330// lines are stored 0-based in SourceMap spec version 3331result += base64VLQ.encode(mapping.originalLine - 1332- previousOriginalLine);333previousOriginalLine = mapping.originalLine - 1;334335result += base64VLQ.encode(mapping.originalColumn336- previousOriginalColumn);337previousOriginalColumn = mapping.originalColumn;338339if (mapping.name) {340result += base64VLQ.encode(this._names.indexOf(mapping.name)341- previousName);342previousName = this._names.indexOf(mapping.name);343}344}345}346347return result;348};349350SourceMapGenerator.prototype._generateSourcesContent =351function SourceMapGenerator_generateSourcesContent(aSources, aSourceRoot) {352return aSources.map(function (source) {353if (!this._sourcesContents) {354return null;355}356if (aSourceRoot) {357source = util.relative(aSourceRoot, source);358}359var key = util.toSetString(source);360return Object.prototype.hasOwnProperty.call(this._sourcesContents,361key)362? this._sourcesContents[key]363: null;364}, this);365};366367/**368* Externalize the source map.369*/370SourceMapGenerator.prototype.toJSON =371function SourceMapGenerator_toJSON() {372var map = {373version: this._version,374file: this._file,375sources: this._sources.toArray(),376names: this._names.toArray(),377mappings: this._serializeMappings()378};379if (this._sourceRoot) {380map.sourceRoot = this._sourceRoot;381}382if (this._sourcesContents) {383map.sourcesContent = this._generateSourcesContent(map.sources, map.sourceRoot);384}385386return map;387};388389/**390* Render the source map being generated to a string.391*/392SourceMapGenerator.prototype.toString =393function SourceMapGenerator_toString() {394return JSON.stringify(this);395};396397exports.SourceMapGenerator = SourceMapGenerator;398399});400401402