diff --git a/lib/source-map-consumer.js b/lib/source-map-consumer.js index db0a5324..ee661146 100644 --- a/lib/source-map-consumer.js +++ b/lib/source-map-consumer.js @@ -151,7 +151,9 @@ SourceMapConsumer.prototype.eachMapping = for (var i = 0, n = mappings.length; i < n; i++) { var mapping = mappings[i]; var source = mapping.source === null ? null : sources.at(mapping.source); - source = util.computeSourceURL(sourceRoot, source, sourceMapURL); + if(source !== null) { + source = util.computeSourceURL(sourceRoot, source, sourceMapURL); + } boundCallback({ source: source, generatedLine: mapping.generatedLine, @@ -1142,7 +1144,9 @@ IndexedSourceMapConsumer.prototype._parseMappings = var mapping = sectionMappings[j]; var source = section.consumer._sources.at(mapping.source); - source = util.computeSourceURL(section.consumer.sourceRoot, source, this._sourceMapURL); + if(source !== null) { + source = util.computeSourceURL(section.consumer.sourceRoot, source, this._sourceMapURL); + } this._sources.add(source); source = this._sources.indexOf(source); diff --git a/test/test-source-map-generator.js b/test/test-source-map-generator.js index b930515c..fae7d4a1 100644 --- a/test/test-source-map-generator.js +++ b/test/test-source-map-generator.js @@ -768,3 +768,18 @@ exports['test numeric names #231'] = function (assert) { assert.equal(map.names.length, 1, "Should have one name"); assert.equal(map.names[0], "8", "Should have the right name"); }; + +exports['test that we can create a generator from a source map with empty mappings'] = function (assert) { + var generator = new SourceMapGenerator(); + generator.addMapping({ + generated: { line: 1, column: 1 }, + original: null, + source: null, + }); + const originalMap = generator.toJSON(); + const consumer = new SourceMapConsumer(originalMap); + const fromConsumer = SourceMapGenerator.fromSourceMap(consumer); + const fromSourceMap = fromConsumer.toJSON(); + assert.ok(fromSourceMap, "Creating a generator from a map with only generated position did not throw"); + assert.equal(fromSourceMap.mappings, originalMap.mappings); +} \ No newline at end of file