Skip to content

Commit

Permalink
Merge pull request #289 from tromey/m-c-fixes
Browse files Browse the repository at this point in the history
M-C fixes
  • Loading branch information
tromey authored Sep 29, 2017
2 parents 83d389f + 2c6fb7e commit 1d77c0b
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 252 deletions.
78 changes: 47 additions & 31 deletions dist/source-map.debug.js

Large diffs are not rendered by default.

76 changes: 46 additions & 30 deletions dist/source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1448,8 +1448,8 @@ return /******/ (function(modules) { // webpackBootstrap
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
}

SourceMapConsumer.fromSourceMap = function(aSourceMap) {
return BasicSourceMapConsumer.fromSourceMap(aSourceMap);
SourceMapConsumer.fromSourceMap = function(aSourceMap, aSourceMapURL) {
return BasicSourceMapConsumer.fromSourceMap(aSourceMap, aSourceMapURL);
}

/**
Expand Down Expand Up @@ -1619,13 +1619,10 @@ return /******/ (function(modules) { // webpackBootstrap
originalColumn: util.getArg(aArgs, 'column', 0)
};

if (this.sourceRoot != null) {
needle.source = util.relative(this.sourceRoot, needle.source);
}
if (!this._sources.has(needle.source)) {
needle.source = this._findSourceIndex(needle.source);
if (needle.source < 0) {
return [];
}
needle.source = this._sources.indexOf(needle.source);

var mappings = [];

Expand Down Expand Up @@ -1763,6 +1760,10 @@ return /******/ (function(modules) { // webpackBootstrap
this._names = ArraySet.fromArray(names.map(String), true);
this._sources = ArraySet.fromArray(sources, true);

this._absoluteSources = this._sources.toArray().map(function (s) {
return util.computeSourceURL(sourceRoot, s, aSourceMapURL);
});

this.sourceRoot = sourceRoot;
this.sourcesContent = sourcesContent;
this._mappings = mappings;
Expand All @@ -1773,6 +1774,32 @@ return /******/ (function(modules) { // webpackBootstrap
BasicSourceMapConsumer.prototype = Object.create(SourceMapConsumer.prototype);
BasicSourceMapConsumer.prototype.consumer = SourceMapConsumer;

/**
* Utility function to find the index of a source. Returns -1 if not
* found.
*/
BasicSourceMapConsumer.prototype._findSourceIndex = function(aSource) {
var relativeSource = aSource;
if (this.sourceRoot != null) {
relativeSource = util.relative(this.sourceRoot, relativeSource);
}

if (this._sources.has(relativeSource)) {
return this._sources.indexOf(relativeSource);
}

// Maybe aSource is an absolute URL as returned by |sources|. In
// this case we can't simply undo the transform.
var i;
for (i = 0; i < this._absoluteSources.length; ++i) {
if (this._absoluteSources[i] == aSource) {
return i;
}
}

return -1;
};

/**
* Create a BasicSourceMapConsumer from a SourceMapGenerator.
*
Expand All @@ -1793,6 +1820,9 @@ return /******/ (function(modules) { // webpackBootstrap
smc.sourceRoot);
smc.file = aSourceMap._file;
smc._sourceMapURL = aSourceMapURL;
smc._absoluteSources = smc._sources.toArray().map(function (s) {
return util.computeSourceURL(smc.sourceRoot, s, aSourceMapURL);
});

// Because we are modifying the entries (by converting string sources and
// names to indices into the sources and names ArraySets), we have to make
Expand Down Expand Up @@ -1839,9 +1869,7 @@ return /******/ (function(modules) { // webpackBootstrap
*/
Object.defineProperty(BasicSourceMapConsumer.prototype, 'sources', {
get: function () {
return this._sources.toArray().map(function (s) {
return util.computeSourceURL(this.sourceRoot, s, this._sourceMapURL);
}, this);
return this._absoluteSources.slice();
}
});

Expand Down Expand Up @@ -2111,25 +2139,16 @@ return /******/ (function(modules) { // webpackBootstrap
return null;
}

var index = this._findSourceIndex(aSource);
if (index >= 0) {
return this.sourcesContent[index];
}

var relativeSource = aSource;
if (this.sourceRoot != null) {
relativeSource = util.relative(this.sourceRoot, relativeSource);
}

if (this._sources.has(relativeSource)) {
return this.sourcesContent[this._sources.indexOf(relativeSource)];
}

// Maybe aSource is an absolute URL as returned by |sources|. In
// this case we can't simply undo the transform.
var sourceArray = this.sources;
var i;
for (i = 0; i < sourceArray.length; ++i) {
if (sourceArray[i] == aSource) {
return this.sourcesContent[i];
}
}

var url;
if (this.sourceRoot != null
&& (url = util.urlParse(this.sourceRoot))) {
Expand Down Expand Up @@ -2187,17 +2206,14 @@ return /******/ (function(modules) { // webpackBootstrap
BasicSourceMapConsumer.prototype.generatedPositionFor =
function SourceMapConsumer_generatedPositionFor(aArgs) {
var source = util.getArg(aArgs, 'source');
if (this.sourceRoot != null) {
source = util.relative(this.sourceRoot, source);
}
if (!this._sources.has(source)) {
source = this._findSourceIndex(source);
if (source < 0) {
return {
line: null,
column: null,
lastColumn: null
};
}
source = this._sources.indexOf(source);

var needle = {
source: source,
Expand Down Expand Up @@ -2474,7 +2490,7 @@ return /******/ (function(modules) { // webpackBootstrap

// Only consider this section if the requested source is in the list of
// sources of the consumer.
if (section.consumer.sources.indexOf(util.getArg(aArgs, 'source')) === -1) {
if (section.consumer._findSourceIndex(util.getArg(aArgs, 'source')) === -1) {
continue;
}
var generatedPosition = section.consumer.generatedPositionFor(aArgs);
Expand Down
2 changes: 1 addition & 1 deletion dist/source-map.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/source-map.min.js.map

Large diffs are not rendered by default.

78 changes: 47 additions & 31 deletions dist/test/test_api.js

Large diffs are not rendered by default.

78 changes: 47 additions & 31 deletions dist/test/test_dog_fooding.js

Large diffs are not rendered by default.

103 changes: 70 additions & 33 deletions dist/test/test_source_map_consumer.js

Large diffs are not rendered by default.

78 changes: 47 additions & 31 deletions dist/test/test_source_map_generator.js

Large diffs are not rendered by default.

78 changes: 47 additions & 31 deletions dist/test/test_source_node.js

Large diffs are not rendered by default.

76 changes: 46 additions & 30 deletions lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function SourceMapConsumer(aSourceMap, aSourceMapURL) {
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
}

SourceMapConsumer.fromSourceMap = function(aSourceMap) {
return BasicSourceMapConsumer.fromSourceMap(aSourceMap);
SourceMapConsumer.fromSourceMap = function(aSourceMap, aSourceMapURL) {
return BasicSourceMapConsumer.fromSourceMap(aSourceMap, aSourceMapURL);
}

/**
Expand Down Expand Up @@ -193,13 +193,10 @@ SourceMapConsumer.prototype.allGeneratedPositionsFor =
originalColumn: util.getArg(aArgs, 'column', 0)
};

if (this.sourceRoot != null) {
needle.source = util.relative(this.sourceRoot, needle.source);
}
if (!this._sources.has(needle.source)) {
needle.source = this._findSourceIndex(needle.source);
if (needle.source < 0) {
return [];
}
needle.source = this._sources.indexOf(needle.source);

var mappings = [];

Expand Down Expand Up @@ -337,6 +334,10 @@ function BasicSourceMapConsumer(aSourceMap, aSourceMapURL) {
this._names = ArraySet.fromArray(names.map(String), true);
this._sources = ArraySet.fromArray(sources, true);

this._absoluteSources = this._sources.toArray().map(function (s) {
return util.computeSourceURL(sourceRoot, s, aSourceMapURL);
});

this.sourceRoot = sourceRoot;
this.sourcesContent = sourcesContent;
this._mappings = mappings;
Expand All @@ -347,6 +348,32 @@ function BasicSourceMapConsumer(aSourceMap, aSourceMapURL) {
BasicSourceMapConsumer.prototype = Object.create(SourceMapConsumer.prototype);
BasicSourceMapConsumer.prototype.consumer = SourceMapConsumer;

/**
* Utility function to find the index of a source. Returns -1 if not
* found.
*/
BasicSourceMapConsumer.prototype._findSourceIndex = function(aSource) {
var relativeSource = aSource;
if (this.sourceRoot != null) {
relativeSource = util.relative(this.sourceRoot, relativeSource);
}

if (this._sources.has(relativeSource)) {
return this._sources.indexOf(relativeSource);
}

// Maybe aSource is an absolute URL as returned by |sources|. In
// this case we can't simply undo the transform.
var i;
for (i = 0; i < this._absoluteSources.length; ++i) {
if (this._absoluteSources[i] == aSource) {
return i;
}
}

return -1;
};

/**
* Create a BasicSourceMapConsumer from a SourceMapGenerator.
*
Expand All @@ -367,6 +394,9 @@ BasicSourceMapConsumer.fromSourceMap =
smc.sourceRoot);
smc.file = aSourceMap._file;
smc._sourceMapURL = aSourceMapURL;
smc._absoluteSources = smc._sources.toArray().map(function (s) {
return util.computeSourceURL(smc.sourceRoot, s, aSourceMapURL);
});

// Because we are modifying the entries (by converting string sources and
// names to indices into the sources and names ArraySets), we have to make
Expand Down Expand Up @@ -413,9 +443,7 @@ BasicSourceMapConsumer.prototype._version = 3;
*/
Object.defineProperty(BasicSourceMapConsumer.prototype, 'sources', {
get: function () {
return this._sources.toArray().map(function (s) {
return util.computeSourceURL(this.sourceRoot, s, this._sourceMapURL);
}, this);
return this._absoluteSources.slice();
}
});

Expand Down Expand Up @@ -685,25 +713,16 @@ BasicSourceMapConsumer.prototype.sourceContentFor =
return null;
}

var index = this._findSourceIndex(aSource);
if (index >= 0) {
return this.sourcesContent[index];
}

var relativeSource = aSource;
if (this.sourceRoot != null) {
relativeSource = util.relative(this.sourceRoot, relativeSource);
}

if (this._sources.has(relativeSource)) {
return this.sourcesContent[this._sources.indexOf(relativeSource)];
}

// Maybe aSource is an absolute URL as returned by |sources|. In
// this case we can't simply undo the transform.
var sourceArray = this.sources;
var i;
for (i = 0; i < sourceArray.length; ++i) {
if (sourceArray[i] == aSource) {
return this.sourcesContent[i];
}
}

var url;
if (this.sourceRoot != null
&& (url = util.urlParse(this.sourceRoot))) {
Expand Down Expand Up @@ -761,17 +780,14 @@ BasicSourceMapConsumer.prototype.sourceContentFor =
BasicSourceMapConsumer.prototype.generatedPositionFor =
function SourceMapConsumer_generatedPositionFor(aArgs) {
var source = util.getArg(aArgs, 'source');
if (this.sourceRoot != null) {
source = util.relative(this.sourceRoot, source);
}
if (!this._sources.has(source)) {
source = this._findSourceIndex(source);
if (source < 0) {
return {
line: null,
column: null,
lastColumn: null
};
}
source = this._sources.indexOf(source);

var needle = {
source: source,
Expand Down Expand Up @@ -1048,7 +1064,7 @@ IndexedSourceMapConsumer.prototype.generatedPositionFor =

// Only consider this section if the requested source is in the list of
// sources of the consumer.
if (section.consumer.sources.indexOf(util.getArg(aArgs, 'source')) === -1) {
if (section.consumer._findSourceIndex(util.getArg(aArgs, 'source')) === -1) {
continue;
}
var generatedPosition = section.consumer.generatedPositionFor(aArgs);
Expand Down
25 changes: 23 additions & 2 deletions test/test-source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ exports['test sourceRoot + generatedPositionFor'] = function (assert) {
generated: { line: 6, column: 6 },
source: 'bang.coffee'
});
map = new SourceMapConsumer(map.toString());
map = new SourceMapConsumer(map.toString(), 'http://example.com/');

// Should handle without sourceRoot.
var pos = map.generatedPositionFor({
Expand All @@ -519,6 +519,16 @@ exports['test sourceRoot + generatedPositionFor'] = function (assert) {

assert.equal(pos.line, 2);
assert.equal(pos.column, 2);

// Should handle absolute case.
var pos = map.generatedPositionFor({
line: 1,
column: 1,
source: 'http://example.com/foo/bar/bang.coffee'
});

assert.equal(pos.line, 2);
assert.equal(pos.column, 2);
};

exports['test sourceRoot + generatedPositionFor for path above the root'] = function (assert) {
Expand Down Expand Up @@ -573,7 +583,7 @@ exports['test allGeneratedPositionsFor for line'] = function (assert) {
generated: { line: 4, column: 2 },
source: 'bar.coffee'
});
map = new SourceMapConsumer(map.toString());
map = new SourceMapConsumer(map.toString(), 'http://example.com/');

var mappings = map.allGeneratedPositionsFor({
line: 2,
Expand All @@ -585,6 +595,17 @@ exports['test allGeneratedPositionsFor for line'] = function (assert) {
assert.equal(mappings[0].column, 2);
assert.equal(mappings[1].line, 3);
assert.equal(mappings[1].column, 3);

mappings = map.allGeneratedPositionsFor({
line: 2,
source: 'http://example.com/bar.coffee'
});

assert.equal(mappings.length, 2);
assert.equal(mappings[0].line, 3);
assert.equal(mappings[0].column, 2);
assert.equal(mappings[1].line, 3);
assert.equal(mappings[1].column, 3);
};

exports['test allGeneratedPositionsFor for line fuzzy'] = function (assert) {
Expand Down

0 comments on commit 1d77c0b

Please sign in to comment.