From 461dd8fd06cf01c3072206a564f777944b4548f5 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 +- test/test-source-map-consumer.js | 16 ++++++++++++++++ 4 files changed, 26 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); diff --git a/test/test-source-map-consumer.js b/test/test-source-map-consumer.js index 3d86b39e..3c964ae8 100644 --- a/test/test-source-map-consumer.js +++ b/test/test-source-map-consumer.js @@ -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;