Skip to content

Commit 0f58a3f

Browse files
Ensure null source is respected. (#26)
1 parent 9403576 commit 0f58a3f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: lib/source-map-consumer.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ SourceMapConsumer.prototype.eachMapping =
151151
for (var i = 0, n = mappings.length; i < n; i++) {
152152
var mapping = mappings[i];
153153
var source = mapping.source === null ? null : sources.at(mapping.source);
154-
source = util.computeSourceURL(sourceRoot, source, sourceMapURL);
154+
if(source !== null) {
155+
source = util.computeSourceURL(sourceRoot, source, sourceMapURL);
156+
}
155157
boundCallback({
156158
source: source,
157159
generatedLine: mapping.generatedLine,
@@ -1142,7 +1144,9 @@ IndexedSourceMapConsumer.prototype._parseMappings =
11421144
var mapping = sectionMappings[j];
11431145

11441146
var source = section.consumer._sources.at(mapping.source);
1145-
source = util.computeSourceURL(section.consumer.sourceRoot, source, this._sourceMapURL);
1147+
if(source !== null) {
1148+
source = util.computeSourceURL(section.consumer.sourceRoot, source, this._sourceMapURL);
1149+
}
11461150
this._sources.add(source);
11471151
source = this._sources.indexOf(source);
11481152

Diff for: test/test-source-map-generator.js

+15
Original file line numberDiff line numberDiff line change
@@ -768,3 +768,18 @@ exports['test numeric names #231'] = function (assert) {
768768
assert.equal(map.names.length, 1, "Should have one name");
769769
assert.equal(map.names[0], "8", "Should have the right name");
770770
};
771+
772+
exports['test that we can create a generator from a source map with empty mappings'] = function (assert) {
773+
var generator = new SourceMapGenerator();
774+
generator.addMapping({
775+
generated: { line: 1, column: 1 },
776+
original: null,
777+
source: null,
778+
});
779+
const originalMap = generator.toJSON();
780+
const consumer = new SourceMapConsumer(originalMap);
781+
const fromConsumer = SourceMapGenerator.fromSourceMap(consumer);
782+
const fromSourceMap = fromConsumer.toJSON();
783+
assert.ok(fromSourceMap, "Creating a generator from a map with only generated position did not throw");
784+
assert.equal(fromSourceMap.mappings, originalMap.mappings);
785+
}

0 commit comments

Comments
 (0)