Skip to content

Read a sourcesContent entry that isn't a string as null - #138

Closed
7rulnik wants to merge 1 commit into
mainfrom
fix/non-string-sources-content
Closed

7rulnik wants to merge 1 commit into
mainfrom
fix/non-string-sources-content

Conversation

@7rulnik

@7rulnik 7rulnik commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Fixes the conformance todo "a sourcesContent entry that is not a string or null becomes null".

ECMA-426 reads sourcesContent with GetOptionalListOfOptionalStrings: an entry that isn't a string or null is an optional error, and is read as null when the consumer doesn't throw (https://tc39.es/ecma426/#sec-GetOptionalListOfOptionalStrings). BasicSourceMapConsumer kept the value as it was:

  • sourceContentFor() returned it.
  • hasContentsOfAllSources() counted it as content.
  • SourceMapGenerator.fromSourceMap() copied it to its output.

Now such an entry is null, so sourceContentFor() returns null for it, the same as for a source without content. It doesn't throw, even without returnNullOnMissing.

Why it's not breaking: only maps with such an entry change, and those are invalid. The consumer copies the array only when it has one, so for a valid map consumer.sourcesContent is still the caller's array; a test pins that. undefined entries, which JSON can't produce, stay undefined. Index maps get the fix through their sections' consumers.

Tests: the todo now passes. New tests cover sourcesContent, sourceContentFor() without returnNullOnMissing, hasContentsOfAllSources(), and keeping the caller's array.

Benchmark: the constructor now checks each sourcesContent entry once. pnpm bench --cases parse --fixtures all (the case includes the constructor), against main:

react.js.map            base        head   change           95% CI
  parse             772.9 µs    768.8 µs    +0.0%   [-1.4%, +2.6%]
preact.js.map           base        head   change           95% CI
  parse             246.8 µs    244.8 µs    +1.0%   [-0.8%, +1.9%]
amp.js.map              base        head   change           95% CI
  parse              7.83 ms     7.77 ms    +0.2%   [-0.4%, +2.2%]
issue-41.js.map         base        head   change           95% CI
  parse             59.34 ms    58.49 ms    +1.6%   [-3.4%, +3.1%]
babel.min.js.map        base        head   change           95% CI
  parse             86.35 ms    86.42 ms    +0.0%   [-2.9%, +3.8%]
vscode.map              base        head   change           95% CI
  parse            432.47 ms   436.31 ms    -0.7%   [-2.6%, +1.6%]

No change outside the noise.

This changes the conformance test right above the file one that #134 changes, and #136 changes the one below, so whichever lands later needs a small rebase.

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.

The consumer copies the array only when it has such an entry, so for a
valid map `sourcesContent` is still the caller's array. Undefined entries,
which JSON can't produce, stay undefined.
@7rulnik

7rulnik commented Nov 29, 2026

Copy link
Copy Markdown
Owner Author

Closing: this would make other calls throw for maps that work today. In an index map, consumer.sourceContentFor('a.js') (without nullOnMissing) returns the value of a non-string entry on main, but throws once the entry is read as null, because IndexedSourceMapConsumer#sourceContentFor() throws for a source whose content is null. That breaks the no-new-throws rule for a patch, so the todo is marked as needing a major release in #136 instead. The branch stays, for the major.

@7rulnik 7rulnik closed this Nov 29, 2026
7rulnik added a commit that referenced this pull request Nov 29, 2026
#134 and
#138 read them as null, as the
spec does, and were closed because other calls then throw for maps that
work today. Two lock-in tests now pin the current results, and the calls
that would throw:

- a non-string sourcesContent entry is returned by sourceContentFor(), also
  on an index map, where null would make it throw;
- a non-string file is kept, and applySourceMap() without a source file
  runs, where null would make it throw.

Each fails with its closed PR's change and says that the fix needs a major
release.
7rulnik added a commit that referenced this pull request Nov 29, 2026
* Mark the conformance todos that need a major release

Ten of the conformance todos can't be fixed without changing results for
source maps that work today, or making some of them throw. They're now
created with major(reason), which makes the test a todo whose reason starts
with "needs a major release:" and says what the fix would change. The test
runner prints it next to the test.

The other four todos are fixed without breaking anyone in their own PRs.

* Say which changes need a major release

The header said a fix needs a major release when it changes results for
source maps that work today. But the patch fixes change results for
invalid maps that work today too. It now names the three reasons: results
change for valid maps, maps that load today would throw, or leniency that
tools rely on goes away.

* Mark the file and sourcesContent todos as needing a major release

Reading a `file` or a `sourcesContent` entry that isn't a string as null
makes other calls throw for maps that work today:

- applySourceMap() without a source file throws "Both were omitted" for a
  consumer whose `file` is null, where it ran with `file: 42`.
- An index map's sourceContentFor() throws for a source whose content is
  null, unless asked to return null, where it returned the value.

So both wait for a major release too, and only the two todos for negative
positions are left for patch fixes.

* Lock in non-string file and sourcesContent entries until the major

#134 and
#138 read them as null, as the
spec does, and were closed because other calls then throw for maps that
work today. Two lock-in tests now pin the current results, and the calls
that would throw:

- a non-string sourcesContent entry is returned by sourceContentFor(), also
  on an index map, where null would make it throw;
- a non-string file is kept, and applySourceMap() without a source file
  runs, where null would make it throw.

Each fails with its closed PR's change and says that the fix needs a major
release.
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.
@7rulnik
7rulnik deleted the fix/non-string-sources-content branch November 29, 2026 22:23
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