Skip to content

Return null for a source without content on index maps - #142

Merged
7rulnik merged 2 commits into
mainfrom
fix/index-map-source-content-null
Nov 29, 2026
Merged

7rulnik merged 2 commits into
mainfrom
fix/index-map-source-content-null

Conversation

@7rulnik

@7rulnik 7rulnik commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

IndexedSourceMapConsumer#sourceContentFor(source) threw "<source>" is not in the SourceMap. for a source that's in the map but has no content, unless asked to return null. BasicSourceMapConsumer's returns null for such a source, and the method's comment says it "returns null if no original source content is available".

Now it returns null when a section has the source, at any depth. A source that isn't in the map still throws.

index map with a.js (content A) and b.js (no content) before after
sourceContentFor('a.js') 'A' 'A'
sourceContentFor('b.js') throws null
sourceContentFor('nope.js') throws throws
sourceContentFor('nope.js', true) null null

How: membership goes through a new BasicSourceMapConsumer#_sourceContentIndex(), recursing into nested sections. Its own sourceContentFor() now uses the same helper, so the two can't disagree. It accepts every name sourceContentFor() accepts:

  • relative to the sourceRoot;
  • a URL from sources;
  • the two fallbacks for URL sourceRoots: a file:// URL for a file: sourceRoot, and a name without the leading / for a sourceRoot without a path.

It covers sources without mappings and doesn't parse the mappings. BasicSourceMapConsumer returns null for any source when its map has no sourcesContent. An index map still throws for a source that no section has, even when a section has no sourcesContent. A source that isn't a string skips the check and throws the same Error as before.

Checked against main: I compared sourceContentFor() on 25,480 combinations of sourceRoot, sources, content, query and nullOnMissing, for a regular map and the same map in an index map. The regular consumer returns or throws the same in every case. For index maps, the only changes are "not in the SourceMap" errors that are now null (984 cases).

README: it said "If the source content for the given source is not found, then an error is thrown", which matched index maps but not regular ones. It now says what both do:

  • null for a source without content;
  • an error for a source that isn't in the map;
  • null for any source on a regular map without sourcesContent.

The hasContentsOfAllSources() section said sourceContentFor(s) "will succeed" for every source when it returns true. It now says the method returns the content, and otherwise null for the sources without content.

Why it's a patch: it only removes a throw, and makes index maps match regular maps, the JSDoc and nullOnMissing. A caller that used the throw on index maps as a "no content" signal now gets null. Such a caller already had to handle null from regular maps.

What it unblocks: #138 was closed because reading a non-string sourcesContent entry as null made this method throw. With this fix it doesn't, so that change may ship as a patch. That needs a new PR: remove the lock-in and the major() mark it added in #136, and deal with the review's other two points on #138.

Tests (they assert strictly):

  • null for no sourcesContent, a null entry, a source without mappings, a nested index map, and every name in sources (with a sourceRoot, with and without a map URL);
  • the two fallback names, nested, with a null entry and without sourcesContent;
  • content from a later section with the same source;
  • unknown sources still throw, including when a section has no sourcesContent, and a source that isn't a string throws the same Error as before;
  • the lookup doesn't parse the mappings.

The new tests other than the guards fail on main; the fallback-name and non-string tests also fail with this PR's first commit.

IndexedSourceMapConsumer#sourceContentFor(source) threw '"<source>" is not
in the SourceMap.' for a source that's in the map but has no content,
unless asked to return null. BasicSourceMapConsumer's returns null for it,
and the method's comment says it returns null when no content is
available.

It now returns null when a section, at any depth, has the source.
Membership uses each section's _findSourceIndex(), which accepts the same
names as sourceContentFor() and doesn't parse the mappings. A source that
no section has still throws, even when a section has no sourcesContent,
where BasicSourceMapConsumer returns null for any source.

The README said that an error is thrown when the content isn't found. It
now says what both consumers do: null for a source without content, an
error for a source that isn't in the map, and null for any source on a
regular map without sourcesContent.
hasSource() used _findSourceIndex(), but BasicSourceMapConsumer's
sourceContentFor() also finds a source by two more names when the
sourceRoot is a URL: a file:// URL for a file: sourceRoot, and a name
without the leading "/" for a sourceRoot without a path. For such a source
without content, an index map still threw. For example, with sourceRoot
'http://example.com/' and sources ['/a.js'], sourceContentFor('a.js').

Both lookups now go through BasicSourceMapConsumer#_sourceContentIndex(),
which sourceContentFor() uses too, so the two can't disagree. Compared with
main on 25,480 combinations of sourceRoot, sources, content, query and
nullOnMissing, BasicSourceMapConsumer#sourceContentFor() returns or throws
the same in every case. IndexedSourceMapConsumer's only changes are
"not in the SourceMap" errors that are now null.

A source that isn't a string skips the membership check, so it still
throws the same Error instead of a TypeError from util.relative().

New tests cover both names, nested, with a null entry and without
sourcesContent; content from a later section with the same source; and the
error for a source that isn't a string.
@7rulnik
7rulnik marked this pull request as ready for review November 29, 2026 22:05
@7rulnik
7rulnik merged commit d8a3be4 into main Nov 29, 2026
7 checks passed
@7rulnik
7rulnik deleted the fix/index-map-source-content-null branch November 29, 2026 22:06
7rulnik added a commit that referenced this pull request Nov 29, 2026
* Read a sourcesContent entry that isn't a string as null

ECMA-426 reads `sourcesContent` with GetOptionalListOfOptionalStrings: an
entry that isn't a string or null is an optional error, read as null when
the consumer doesn't throw
(https://tc39.es/ecma426/#sec-GetOptionalListOfOptionalStrings). The
consumer kept the value: sourceContentFor() returned it,
hasContentsOfAllSources() counted it as content, and
SourceMapGenerator.fromSourceMap() copied it to its output.

#138 made the same change and
was closed, because an index map's sourceContentFor() then threw for such a
source. #142 fixed that, so
this ships as a patch:

- The consumer copies the array only when it has such an entry, so for a
  valid map `sourcesContent` is still the caller's array. It reads the
  array by index and calls none of its methods. Undefined entries, which
  JSON can't produce, stay undefined.
- SourceMapConsumer.fromSourceMap(generator) reads a generator's content
  the same way, for content set with setSourceContent().
- The conformance todo passes, so its major() mark and the lock-in test
  that pinned the old result are gone.

* Find a source by any name, and copy sourcesContent without its constructor

- An index map with sources ['1'] and sourcesContent [1] returned 1 from
  sourceContentFor(1). With the entry read as null, the lookup goes on to
  check whether the map has the source, and that check only took strings,
  so it threw. It now checks any name, and only falls back to the
  original error when the check itself throws: for a source that isn't a
  string, with a sourceRoot, util.relative() throws a TypeError.
- Array.prototype.slice.call() used the array's constructor through
  Symbol.species, so a caller's array with `constructor = null` made the
  consumer throw, and a species that returns a plain object broke
  hasContentsOfAllSources(). The copy is now a plain array, holes kept.

The intro of conformance section 2 now says how the library reads each of
`file`, `sources`, `sourcesContent` and `names`.

* Look past index map sections whose lookup throws

A section with a sourceRoot throws a TypeError when it looks up a source
that isn't a string: util.relative() takes a string. With a sourcesContent
entry like 1 read as null, the lookup now goes on past the section that
has it, so sourceContentFor(1) could reach such a section and throw where
it returned 1 before:

- sections [{sources: ['1'], sourcesContent: [1]},
  {sourceRoot: 'src', sources: ['other.js'], sourcesContent: [null]}];
- the same sections the other way round, without sourcesContent in the
  second: the membership check stopped at the first section's error.

IndexedSourceMapConsumer#sourceContentFor() now skips a section whose
lookup throws, and hasSource() treats a throwing lookup as "not this
section". If no section has the source, it throws the first section's
error, also with nullOnMissing, as it did before. Without such an error,
it works as before, including the cheap nullOnMissing path.

Compared with main on 19,602 lookups in index maps of two or three
sections, some nested, with sourceRoots and string, null, number and
missing content, and string, number, null, undefined, object and String
queries: no new throws, the same error type and message wherever both
throw, and no change for maps without a non-string entry and a string
query.

* Say that an invalid sourceRoot can make a section's lookup throw too

A sourceRoot that util.relative() can't take, such as 0 or false, makes it
throw for any source, not only for a source that isn't a string.
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