react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / inline-source-map / node_modules / source-map / lib / source-map / source-map-consumer.js
80620 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 util = require('./util');1213function SourceMapConsumer(aSourceMap) {14var sourceMap = aSourceMap;15if (typeof aSourceMap === 'string') {16sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));17}1819// We do late requires because the subclasses require() this file.20if (sourceMap.sections != null) {21var indexedSourceMapConsumer = require('./indexed-source-map-consumer');22return new indexedSourceMapConsumer.IndexedSourceMapConsumer(sourceMap);23} else {24var basicSourceMapConsumer = require('./basic-source-map-consumer');25return new basicSourceMapConsumer.BasicSourceMapConsumer(sourceMap);26}27}2829SourceMapConsumer.fromSourceMap = function(aSourceMap) {30var basicSourceMapConsumer = require('./basic-source-map-consumer');31return basicSourceMapConsumer.BasicSourceMapConsumer32.fromSourceMap(aSourceMap);33}3435/**36* The version of the source mapping spec that we are consuming.37*/38SourceMapConsumer.prototype._version = 3;394041// `__generatedMappings` and `__originalMappings` are arrays that hold the42// parsed mapping coordinates from the source map's "mappings" attribute. They43// are lazily instantiated, accessed via the `_generatedMappings` and44// `_originalMappings` getters respectively, and we only parse the mappings45// and create these arrays once queried for a source location. We jump through46// these hoops because there can be many thousands of mappings, and parsing47// them is expensive, so we only want to do it if we must.48//49// Each object in the arrays is of the form:50//51// {52// generatedLine: The line number in the generated code,53// generatedColumn: The column number in the generated code,54// source: The path to the original source file that generated this55// chunk of code,56// originalLine: The line number in the original source that57// corresponds to this chunk of generated code,58// originalColumn: The column number in the original source that59// corresponds to this chunk of generated code,60// name: The name of the original symbol which generated this chunk of61// code.62// }63//64// All properties except for `generatedLine` and `generatedColumn` can be65// `null`.66//67// `_generatedMappings` is ordered by the generated positions.68//69// `_originalMappings` is ordered by the original positions.7071SourceMapConsumer.prototype.__generatedMappings = null;72Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappings', {73get: function () {74if (!this.__generatedMappings) {75this.__generatedMappings = [];76this.__originalMappings = [];77this._parseMappings(this._mappings, this.sourceRoot);78}7980return this.__generatedMappings;81}82});8384SourceMapConsumer.prototype.__originalMappings = null;85Object.defineProperty(SourceMapConsumer.prototype, '_originalMappings', {86get: function () {87if (!this.__originalMappings) {88this.__generatedMappings = [];89this.__originalMappings = [];90this._parseMappings(this._mappings, this.sourceRoot);91}9293return this.__originalMappings;94}95});9697SourceMapConsumer.prototype._nextCharIsMappingSeparator =98function SourceMapConsumer_nextCharIsMappingSeparator(aStr) {99var c = aStr.charAt(0);100return c === ";" || c === ",";101};102103/**104* Parse the mappings in a string in to a data structure which we can easily105* query (the ordered arrays in the `this.__generatedMappings` and106* `this.__originalMappings` properties).107*/108SourceMapConsumer.prototype._parseMappings =109function SourceMapConsumer_parseMappings(aStr, aSourceRoot) {110throw new Error("Subclasses must implement _parseMappings");111};112113SourceMapConsumer.GENERATED_ORDER = 1;114SourceMapConsumer.ORIGINAL_ORDER = 2;115116SourceMapConsumer.LEAST_UPPER_BOUND = 1;117SourceMapConsumer.GREATEST_LOWER_BOUND = 2;118119/**120* Iterate over each mapping between an original source/line/column and a121* generated line/column in this source map.122*123* @param Function aCallback124* The function that is called with each mapping.125* @param Object aContext126* Optional. If specified, this object will be the value of `this` every127* time that `aCallback` is called.128* @param aOrder129* Either `SourceMapConsumer.GENERATED_ORDER` or130* `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to131* iterate over the mappings sorted by the generated file's line/column132* order or the original's source/line/column order, respectively. Defaults to133* `SourceMapConsumer.GENERATED_ORDER`.134*/135SourceMapConsumer.prototype.eachMapping =136function SourceMapConsumer_eachMapping(aCallback, aContext, aOrder) {137var context = aContext || null;138var order = aOrder || SourceMapConsumer.GENERATED_ORDER;139140var mappings;141switch (order) {142case SourceMapConsumer.GENERATED_ORDER:143mappings = this._generatedMappings;144break;145case SourceMapConsumer.ORIGINAL_ORDER:146mappings = this._originalMappings;147break;148default:149throw new Error("Unknown order of iteration.");150}151152var sourceRoot = this.sourceRoot;153mappings.map(function (mapping) {154var source = mapping.source;155if (source != null && sourceRoot != null) {156source = util.join(sourceRoot, source);157}158return {159source: source,160generatedLine: mapping.generatedLine,161generatedColumn: mapping.generatedColumn,162originalLine: mapping.originalLine,163originalColumn: mapping.originalColumn,164name: mapping.name165};166}).forEach(aCallback, context);167};168169/**170* Returns all generated line and column information for the original source171* and line provided. The only argument is an object with the following172* properties:173*174* - source: The filename of the original source.175* - line: The line number in the original source.176*177* and an array of objects is returned, each with the following properties:178*179* - line: The line number in the generated source, or null.180* - column: The column number in the generated source, or null.181*/182SourceMapConsumer.prototype.allGeneratedPositionsFor =183function SourceMapConsumer_allGeneratedPositionsFor(aArgs) {184var needle = {185source: util.getArg(aArgs, 'source'),186originalLine: util.getArg(aArgs, 'line'),187originalColumn: 0188};189190if (this.sourceRoot != null) {191needle.source = util.relative(this.sourceRoot, needle.source);192}193194var mappings = [];195196var index = this._findMapping(needle,197this._originalMappings,198"originalLine",199"originalColumn",200util.compareByOriginalPositions);201if (index >= 0) {202var mapping = this._originalMappings[index];203204// Iterate until either we run out of mappings, or we run into205// a mapping for a different line. Since mappings are sorted, this is206// guaranteed to find all mappings for the line we are interested in.207while (mapping && mapping.originalLine === needle.originalLine) {208mappings.push({209line: util.getArg(mapping, 'generatedLine', null),210column: util.getArg(mapping, 'generatedColumn', null),211lastColumn: util.getArg(mapping, 'lastGeneratedColumn', null)212});213214mapping = this._originalMappings[++index];215}216}217218return mappings;219};220221exports.SourceMapConsumer = SourceMapConsumer;222223});224225226