7rulnik / source-map-js

Consume and generate source maps.
Other
92 stars 13 forks source link

mapping.source is not null even if the original is. #25

Closed dragomirtitian closed 1 week ago

dragomirtitian commented 3 weeks ago

When trying to create a source map generator form a consumer that has mappings with just the generated position we get an error.

Code to reproduce:

import { SourceMapGenerator, SourceMapConsumer } from 'source-map-js';

const original = new SourceMapGenerator({
    file: "origina.min.js",
})
original.addMapping({
    generated: { line: 1, column: 1},
    original: null!,
    source: null!,
});

const consumer = new SourceMapConsumer(original.toJSON());

const fromConsumer = SourceMapGenerator.fromSourceMap(consumer);

Actual result

Error: original.line and original.column are not numbers -- you probably meant to omit the original mapping entirely and only map the generated position. If so, pass null for the original mapping instead of an object with empty or null values.

Expected result

We should be able to create a source map generator from a consumer that has mappings with only generated position.

Cause

When mappings are produced they get a source even if they did not originally have one. Code


      var mapping = mappings[i];
      var source = mapping.source === null ? null : sources.at(mapping.source);
      source = util.computeSourceURL(sourceRoot, source, sourceMapURL); 

util.computeSourceURL will return . if all components are null.