Skip to content

Keep a null source for unmapped segments of index source maps - #129

Merged
7rulnik merged 3 commits into
mainfrom
fix/index-map-unmapped-source
Nov 29, 2026
Merged

7rulnik merged 3 commits into
mainfrom
fix/index-map-unmapped-source

Conversation

@7rulnik

@7rulnik 7rulnik commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Fixes a bug that #127 added a todo test for.

IndexedSourceMapConsumer#_parseMappings 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 ".", with null original line and column.
  • SourceMapGenerator.fromSourceMap() threw original.line and original.column are not numbers on them. That happens before the sourceRoot bug from Add todo tests for two SourceMapGenerator.fromSourceMap bugs #126, so a bundle with unmapped code fails here first.
  • The consumer's internal source set got a "." entry.

Now a mapping without a source keeps a null source and adds nothing to the set.

SourceNode with a relative path. SourceNode.fromStringWithSourceMap(code, consumer, relativePath) joins the path to every mapping's source, and util.join() throws on null: Cannot read properties of null (reading 'match'). Regular source maps with unmapped code already threw there. Index maps didn't, only because of the "." source, so this fix alone would have made them start throwing. The path is now only joined to a source that isn't null, which fixes both. #137 makes the same change, line for line.

Tests: the todo from #127 now passes, and it also covers a nested index map. A new SourceNode test covers a relative path with a regular and an index map; it asserts with deepStrictEqual, since on Node 26 the legacy assert.deepEqual() lets a null in the expected array match any value. Once this and the fromSourceMap() fix are both in, I'll add a fromSourceMap() test with unmapped segments to whichever lands second.

No benchmark: the per-mapping loop of index maps now skips the lookup for unmapped segments and is otherwise unchanged.

Base automatically changed from test/index-map-todos to main November 29, 2026 20:28
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.
@7rulnik
7rulnik force-pushed the fix/index-map-unmapped-source branch from a830143 to 6e8dff9 Compare November 29, 2026 20:29
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, and util.join() throws on a null
source. A mapping with a negative original line or column now has a null
source, so such maps, which loaded before, started throwing "Cannot read
properties of null (reading 'match')" here. The path is now only joined to
a source that isn't null. #129
makes the same change for unmapped segments.

New consumer tests check these maps without accepting a throw:
eachMapping(), originalPositionFor() and allGeneratedPositionsFor() for a
negative line and a negative column, the line and column counting for the
next segment, and SourceNode with a relative path.
…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.
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
7rulnik force-pushed the fix/index-map-unmapped-source branch from 874bfd9 to d41ed8f Compare November 29, 2026 21:00
@7rulnik
7rulnik marked this pull request as ready for review November 29, 2026 21:12
@7rulnik
7rulnik merged commit 68fb1bc into main Nov 29, 2026
7 checks passed
@7rulnik
7rulnik deleted the fix/index-map-unmapped-source branch November 29, 2026 21:12
7rulnik added a commit that referenced this pull request Nov 29, 2026
…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 added a commit that referenced this pull request Nov 29, 2026
* Fix SourceMapGenerator.fromSourceMap() on index source maps

- 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.

* Say which index maps fromSourceMap() threw on

An index map without sources, such as one with no sections, didn't throw.

* 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.

* Test fromSourceMap() on an indexed map with unmapped code, names and 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 added a commit that referenced this pull request Nov 29, 2026
… or column (#137)

* Drop the original position of a mapping with a negative original line or column

ECMA-426 makes a negative original line or column an optional error, and
keeps the mapping without its original position when the consumer doesn't
throw (https://tc39.es/ecma426/#sec-DecodeMappingsField). The consumer kept
the position: originalPositionFor() returned line 0 or a negative column,
and SourceMapGenerator.fromSourceMap() threw "Invalid mapping" on it.

As in the spec, the mapping keeps its name, and the decoded line and column
still count for the next segment.

Only invalid source maps have a negative original line or 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, and util.join() throws on a null
source. A mapping with a negative original line or column now has a null
source, so such maps, which loaded before, started throwing "Cannot read
properties of null (reading 'match')" here. The path is now only joined to
a source that isn't null. #129
makes the same change for unmapped segments.

New consumer tests check these maps without accepting a throw:
eachMapping(), originalPositionFor() and allGeneratedPositionsFor() for a
negative line and a negative column, the line and column counting for the
next segment, and SourceNode with a relative path.
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