Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure a consistent return value for sourceContentFor #391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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