From 04cf08d1a801c51261ecc50db2f66508872c531f Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 5 Jun 2019 11:12:15 +0800 Subject: [PATCH] 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. --- lib/source-map-consumer.js | 12 +++++++----- lib/source-map-generator.js | 4 ++-- lib/source-node.js | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/source-map-consumer.js b/lib/source-map-consumer.js index 9b68e393..a6eea6de 100644 --- a/lib/source-map-consumer.js +++ b/lib/source-map-consumer.js @@ -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); @@ -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++) { diff --git a/lib/source-map-generator.js b/lib/source-map-generator.js index 8111e061..6851701b 100644 --- a/lib/source-map-generator.js +++ b/lib/source-map-generator.js @@ -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); } @@ -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); diff --git a/lib/source-node.js b/lib/source-node.js index 8a7a157e..8f92c0a1 100644 --- a/lib/source-node.js +++ b/lib/source-node.js @@ -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);