Repository navigation
Return null for a source without content on index maps - #142
Merged
Merged
Conversation
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.
11 tasks
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
marked this pull request as ready for review
November 29, 2026 22:05
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.
This was referenced Dec 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 returnnull.BasicSourceMapConsumer's returnsnullfor such a source, and the method's comment says it "returns null if no original source content is available".Now it returns
nullwhen a section has the source, at any depth. A source that isn't in the map still throws.a.js(contentA) andb.js(no content)sourceContentFor('a.js')'A''A'sourceContentFor('b.js')nullsourceContentFor('nope.js')sourceContentFor('nope.js', true)nullnullHow: membership goes through a new
BasicSourceMapConsumer#_sourceContentIndex(), recursing into nested sections. Its ownsourceContentFor()now uses the same helper, so the two can't disagree. It accepts every namesourceContentFor()accepts:sources;file://URL for afile:sourceRoot, and a name without the leading/for a sourceRoot without a path.It covers sources without mappings and doesn't parse the mappings.
BasicSourceMapConsumerreturnsnullfor any source when its map has nosourcesContent. An index map still throws for a source that no section has, even when a section has nosourcesContent. A source that isn't a string skips the check and throws the sameErroras before.Checked against
main: I comparedsourceContentFor()on 25,480 combinations of sourceRoot, sources, content, query andnullOnMissing, 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 nownull(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:
nullfor a source without content;nullfor any source on a regular map withoutsourcesContent.The
hasContentsOfAllSources()section saidsourceContentFor(s)"will succeed" for every source when it returnstrue. It now says the method returns the content, and otherwisenullfor 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 getsnull. Such a caller already had to handlenullfrom regular maps.What it unblocks: #138 was closed because reading a non-string
sourcesContententry asnullmade 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 themajor()mark it added in #136, and deal with the review's other two points on #138.Tests (they assert strictly):
nullfor nosourcesContent, anullentry, a source without mappings, a nested index map, and every name insources(with a sourceRoot, with and without a map URL);nullentry and withoutsourcesContent;sourcesContent, and a source that isn't a string throws the sameErroras before;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.