Skip to content

Fix SourceMapGenerator.fromSourceMap() on index source maps - #132

Merged
7rulnik merged 5 commits into
mainfrom
fix/from-source-map-indexed
Nov 29, 2026
Merged

7rulnik merged 5 commits into
mainfrom
fix/from-source-map-indexed

Conversation

@7rulnik

@7rulnik 7rulnik commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Fixes the two bugs that #126 added todo tests for:

  • fromSourceMap() checked sourceRoot !== null before making each source relative to the sourceRoot. IndexedSourceMapConsumer has no sourceRoot, so the check passed for undefined, and util.relative() threw Cannot read properties of undefined (reading 'replace'). That happened for every index map with sources. The rest of the generator checks != null, and so does this now.
  • It copied each source's content with sourceContentFor(source), which on an IndexedSourceMapConsumer throws for a source without content unless asked to return null. It only asks about the consumer's own sources, so it now passes true and gets null for those. For a BasicSourceMapConsumer nothing changes: it already returned null for them.

Tests: both todos from #126 now pass, and new tests cover a nested index map and the full index map below.

With #128 (names at index 0), #129 (unmapped segments) and #130 (file) merged, fromSourceMap() now copies an index map exactly. A new test, added after merging main into this branch, covers a map with unmapped segments, names at index 0, a file and a source without content. It asserts with deepStrictEqual, and so do the null checks in the other tests here: on Node 26 the legacy assert.deepEqual() lets a null in the expected array match any value.

applySourceMap() and SourceNode.fromStringWithSourceMap() have the same missing-content bug, fixed in #131.

- It checked `sourceRoot !== null` before making each source relative to
  the sourceRoot. IndexedSourceMapConsumer has no sourceRoot, so the check
  passed for undefined and util.relative() threw "Cannot read properties of
  undefined (reading 'replace')". The rest of the generator checks
  `!= null`, and so does this now.
- It copied each source's content with sourceContentFor(source), which on
  an IndexedSourceMapConsumer throws for a source without content unless
  asked to return null. It only asks about the consumer's own sources, so it
  now passes `true` and gets null for those.

Both are from #126.
7rulnik added a commit that referenced this pull request Nov 29, 2026
…e path

SourceNode.fromStringWithSourceMap() joins its third argument, a relative
path, to the source of every mapping. A mapping without an original
position has a null source, and util.join() threw "Cannot read properties
of null (reading 'match')" on it. Regular source maps with such mappings
already threw. Index maps didn't, because their unmapped segments had the
source "."; with that fixed in the previous commit, they started throwing
too. The path is now only joined to a source that isn't null.

The changeset no longer says that SourceMapGenerator.fromSourceMap() works
on these maps: on index maps it still throws for another reason until
#132 lands.

Found by a Codex review.
An index map without sources, such as one with no sections, didn't throw.
These test files use the legacy node:assert. On Node 26 its deepEqual()
lets a null in the expected array match any value, so expected entries
like ['A', null] didn't check the null.
7rulnik added a commit that referenced this pull request Nov 29, 2026
…e path

SourceNode.fromStringWithSourceMap() joins its third argument, a relative
path, to the source of every mapping. A mapping without an original
position has a null source, and util.join() threw "Cannot read properties
of null (reading 'match')" on it. Regular source maps with such mappings
already threw. Index maps didn't, because their unmapped segments had the
source "."; with that fixed in the previous commit, they started throwing
too. The path is now only joined to a source that isn't null.

The changeset no longer says that SourceMapGenerator.fromSourceMap() works
on these maps: on index maps it still throws for another reason until
#132 lands.
7rulnik added a commit that referenced this pull request Nov 29, 2026
* Keep a null source for unmapped segments of index source maps

IndexedSourceMapConsumer looked up every mapping's source in its section
with ArraySet#at(mapping.source), even for a segment with only a generated
column, whose source is null. at(null) returns undefined, because
null >= 0, and util.computeSourceURL() turned that into ".". So
eachMapping() gave those mappings the source ".", and
SourceMapGenerator.fromSourceMap() threw on them: a source without an
original line and column.

* Accept code without an original position in SourceNode with a relative path

SourceNode.fromStringWithSourceMap() joins its third argument, a relative
path, to the source of every mapping. A mapping without an original
position has a null source, and util.join() threw "Cannot read properties
of null (reading 'match')" on it. Regular source maps with such mappings
already threw. Index maps didn't, because their unmapped segments had the
source "."; with that fixed in the previous commit, they started throwing
too. The path is now only joined to a source that isn't null.

The changeset no longer says that SourceMapGenerator.fromSourceMap() works
on these maps: on index maps it still throws for another reason until
#132 lands.

* Check null entries in the new tests with deepStrictEqual

These test files use the legacy node:assert. On Node 26 its deepEqual()
lets a null in the expected array match any value, so expected entries
like ['A', null] didn't check the null.
…a file

With #128,
#129 and
#130 merged,
fromSourceMap() copies such a map exactly: the unmapped segments stay
unmapped, the names at index 0 stay, the source without content gets null,
and the file is kept.
@7rulnik
7rulnik marked this pull request as ready for review November 29, 2026 21:19
@7rulnik
7rulnik merged commit 33e2186 into main Nov 29, 2026
7 checks passed
@7rulnik
7rulnik deleted the fix/from-source-map-indexed branch November 29, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant