Skip to content

Commit 461dd8f

Browse files
committed
Ensure a consistent return value for sourceContentFor
The sourceContentFor function was previously returning null when it should have returned an exception when the source content was not provided.
1 parent 8cb3ee5 commit 461dd8f

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

lib/source-map-consumer.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,15 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
522522

523523
/**
524524
* Returns the original source content. The only argument is the url of the
525-
* original source file. Returns null if no original source content is
526-
* available.
525+
* original source file.
527526
*/
528527
sourceContentFor(aSource, nullOnMissing) {
529528
if (!this.sourcesContent) {
530-
return null;
529+
if (nullOnMissing) {
530+
return null;
531+
}
532+
533+
throw new Error('"' + aSource + '" is not in the SourceMap.');
531534
}
532535

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

823826
/**
824827
* Returns the original source content. The only argument is the url of the
825-
* original source file. Returns null if no original source content is
826-
* available.
828+
* original source file.
827829
*/
828830
sourceContentFor(aSource, nullOnMissing) {
829831
for (let i = 0; i < this._sections.length; i++) {

lib/source-map-generator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class SourceMapGenerator {
7979
generator._sources.add(sourceRelative);
8080
}
8181

82-
const content = aSourceMapConsumer.sourceContentFor(sourceFile);
82+
const content = aSourceMapConsumer.sourceContentFor(sourceFile, true);
8383
if (content != null) {
8484
generator.setSourceContent(sourceFile, content);
8585
}
@@ -238,7 +238,7 @@ class SourceMapGenerator {
238238

239239
// Copy sourcesContents of applied map.
240240
aSourceMapConsumer.sources.forEach(function(srcFile) {
241-
const content = aSourceMapConsumer.sourceContentFor(srcFile);
241+
const content = aSourceMapConsumer.sourceContentFor(srcFile, true);
242242
if (content != null) {
243243
if (aSourceMapPath != null) {
244244
srcFile = util.join(aSourceMapPath, srcFile);

lib/source-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class SourceNode {
137137

138138
// Copy sourcesContent into SourceNode
139139
aSourceMapConsumer.sources.forEach(function(sourceFile) {
140-
const content = aSourceMapConsumer.sourceContentFor(sourceFile);
140+
const content = aSourceMapConsumer.sourceContentFor(sourceFile, true);
141141
if (content != null) {
142142
if (aRelativePath != null) {
143143
sourceFile = util.join(aRelativePath, sourceFile);

test/test-source-map-consumer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,22 @@ exports["test that we can set the context for `this` in eachMapping in indexed s
438438
map.destroy();
439439
};
440440

441+
exports["test that sourceContentFor result if no sourcesContent provided"] = async function(assert) {
442+
const map = await new SourceMapConsumer(util.testMap);
443+
assert.equal(map.sourceContentFor("/the/root/one.js", true), null);
444+
assert.equal(map.sourceContentFor("/the/root/three.js", true), null);
445+
446+
assert.throws(function() {
447+
map.sourceContentFor("/the/root/one.js");
448+
}, Error);
449+
450+
assert.throws(function() {
451+
map.sourceContentFor("/the/root/three.js");
452+
}, Error);
453+
454+
map.destroy();
455+
};
456+
441457
exports["test that the `sourcesContent` field has the original sources"] = async function(assert) {
442458
const map = await new SourceMapConsumer(util.testMapWithSourcesContent);
443459
const sourcesContent = map.sourcesContent;

0 commit comments

Comments
 (0)