Skip to content

Commit

Permalink
Ensure a consistent return value for sourceContentFor
Browse files Browse the repository at this point in the history
The sourceContentFor function was previously returning null when it
should have returned an exception when the source content was not
provided.
  • Loading branch information
andrewnicols committed Jun 5, 2019
1 parent 8cb3ee5 commit 461dd8f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
12 changes: 7 additions & 5 deletions lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,15 @@ class BasicSourceMapConsumer extends SourceMapConsumer {

/**
* Returns the original source content. The only argument is the url of the
* original source file. Returns null if no original source content is
* available.
* original source file.
*/
sourceContentFor(aSource, nullOnMissing) {
if (!this.sourcesContent) {
return null;
if (nullOnMissing) {
return null;
}

throw new Error('"' + aSource + '" is not in the SourceMap.');
}

const index = this._findSourceIndex(aSource);
Expand Down Expand Up @@ -822,8 +825,7 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {

/**
* Returns the original source content. The only argument is the url of the
* original source file. Returns null if no original source content is
* available.
* original source file.
*/
sourceContentFor(aSource, nullOnMissing) {
for (let i = 0; i < this._sections.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions lib/source-map-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class SourceMapGenerator {
generator._sources.add(sourceRelative);
}

const content = aSourceMapConsumer.sourceContentFor(sourceFile);
const content = aSourceMapConsumer.sourceContentFor(sourceFile, true);
if (content != null) {
generator.setSourceContent(sourceFile, content);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ class SourceMapGenerator {

// Copy sourcesContents of applied map.
aSourceMapConsumer.sources.forEach(function(srcFile) {
const content = aSourceMapConsumer.sourceContentFor(srcFile);
const content = aSourceMapConsumer.sourceContentFor(srcFile, true);
if (content != null) {
if (aSourceMapPath != null) {
srcFile = util.join(aSourceMapPath, srcFile);
Expand Down
2 changes: 1 addition & 1 deletion lib/source-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class SourceNode {

// Copy sourcesContent into SourceNode
aSourceMapConsumer.sources.forEach(function(sourceFile) {
const content = aSourceMapConsumer.sourceContentFor(sourceFile);
const content = aSourceMapConsumer.sourceContentFor(sourceFile, true);
if (content != null) {
if (aRelativePath != null) {
sourceFile = util.join(aRelativePath, sourceFile);
Expand Down
16 changes: 16 additions & 0 deletions test/test-source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,22 @@ exports["test that we can set the context for `this` in eachMapping in indexed s
map.destroy();
};

exports["test that sourceContentFor result if no sourcesContent provided"] = async function(assert) {
const map = await new SourceMapConsumer(util.testMap);
assert.equal(map.sourceContentFor("/the/root/one.js", true), null);
assert.equal(map.sourceContentFor("/the/root/three.js", true), null);

assert.throws(function() {
map.sourceContentFor("/the/root/one.js");
}, Error);

assert.throws(function() {
map.sourceContentFor("/the/root/three.js");
}, Error);

map.destroy();
};

exports["test that the `sourcesContent` field has the original sources"] = async function(assert) {
const map = await new SourceMapConsumer(util.testMapWithSourcesContent);
const sourcesContent = map.sourcesContent;
Expand Down

0 comments on commit 461dd8f

Please sign in to comment.