Skip to content

Error creating a SourceMapGenerator from a SourceMapConsumer of an index map (part 2) #420

@rlidwka

Description

@rlidwka
;(async () => {
  let mozilla = require('source-map')
  let map;

  map = {
    version: 3,
    sections: [
      {
        offset: { line: 0, column: 0 },
        map: {
          version: 3,
          sources: ['123', '456'],
          sourcesContent: ['a\nb\nc\n', ''],
          mappings: 'AAAA;AACA;AACA;'
        }
      }
    ]
  }

  // this map works for some reason
  /*map = {
    version: 3,
    sources: ['123', '456'],
    sourcesContent: ['a\nb\nc\n', ''],
    mappings: 'AAAA;AACA;AACA;'
  }*/

  let consumer = await new mozilla.SourceMapConsumer(map)
  consumer.sourceRoot = null; // https://github.com/mozilla/source-map/issues/345
  let generator = mozilla.SourceMapGenerator.fromSourceMap(consumer)
  console.log(generator.toJSON())

})().catch(console.log)

This errors out:

Error: "456" is not in the SourceMap.
    at IndexedSourceMapConsumer.sourceContentFor (/tmp/node_modules/source-map/lib/source-map-consumer.js:840:11)
    at /tmp/node_modules/source-map/lib/source-map-generator.js:82:42
    at Array.forEach (<anonymous>)
    at Function.fromSourceMap (/tmp/node_modules/source-map/lib/source-map-generator.js:72:32)
    at /tmp/123/test.js:29:46

The issue is in here somewhere:

if (content) {

Content is empty - '' - but it is still in the sourcemap. I believe it should be if (content == null) { check, i.e. null or undefined, letting empty string pass along.

this may be related to #345, so tagging it just in case

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions