Skip to content

Support allGeneratedPositionsFor on indexed source maps - #105

Merged
7rulnik merged 2 commits into
mainfrom
fix/issue-103
Nov 19, 2026
Merged

7rulnik merged 2 commits into
mainfrom
fix/issue-103

Conversation

@7rulnik

@7rulnik 7rulnik commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Fixes #103.

Stacked on #104, which fixes the section column offset in the decoded mappings this PR reads from. Merge #104 first; after it's merged this PR needs a rebase onto main.

The bug

allGeneratedPositionsFor threw TypeError: this._findSourceIndex is not a function on every indexed source map. It's defined on SourceMapConsumer.prototype and calls this._findSourceIndex and this._findMapping, which only BasicSourceMapConsumer had.

Fix

IndexedSourceMapConsumer already keeps every section's mappings in _originalMappings, shifted by the section offset and sorted by original position. So instead of a separate implementation, it gets the two methods the shared one needs:

  • _findSourceIndex maps a source name to its index in this._sources. It accepts any form a section accepts: relative to the section's sourceRoot, or a URL from sources.
  • _findMapping is the generic binary search from BasicSourceMapConsumer.

That gives the same behaviour as a map without sections:

  • Every section counts. If a source appears in several sections, positions from all of them are returned. Upstream mozilla/source-map's version only looks at the first section that has the source.
  • Fuzzy matching is global. Without a column, the closest line at or after the requested one is chosen across all sections, not per section.
  • util.testMap and util.indexedTestMap (the same mappings, with and without sections) now give identical results for every source, line and column the new test tries.

With the map from the issue, allGeneratedPositionsFor({ source: "b.js", line: 1 }) returns [{ line: 2, column: 4, lastColumn: null }], as @jridgewell/trace-mapping does. That column 4 depends on #104.

Side effect: generatedPositionFor on a nested indexed map threw the same TypeError, because it calls _findSourceIndex on each section's consumer. It works now.

Not fixed here

The indexed generatedPositionFor returns a made-up position instead of nulls when the first section that has the source finds no match. It adds the section offset to line: null. With b.js mapped only at original line 5, in a section at offset: { line: 5, column: 3 }, generatedPositionFor({ source: "b.js", line: 1, column: 0 }) returns { line: 5, column: 0 }. Filed as #106. Fixed in #115, stacked on this PR.

Testing

Three new tests:

  • the issue's map;
  • the equivalence check with testMap;
  • a source in two sections, fuzzy matching across sections, a source listed without mappings, and a nested indexed map.

All three fail without the change. pnpm test (233 tests) passes on Node 22, 24 and 26.13.1, and pnpm test:coverage and pnpm format:check pass on 26.13.1.

When decoding an index map and in generatedPositionFor, the section's
column offset was added to mappings whose section-relative line equals
the section's absolute start line (offset.line + 1). That is the
section's first line only when offset.line is 0; otherwise the offset
landed on a later line of the section, or on none. ECMA-426
(DecodeIndexSourceMap) adds it to mappings on the section's first line,
which is also what originalPositionFor already assumed.

This fixes eachMapping, generatedPositionFor and everything built on
the decoded mappings (SourceMapGenerator.fromSourceMap,
SourceNode.fromStringWithSourceMap) for sections with offset.line > 0
and a column offset. The ECMA-426 conformance test for it is no longer
todo.
allGeneratedPositionsFor is defined on SourceMapConsumer.prototype and
calls this._findSourceIndex and this._findMapping, which only
BasicSourceMapConsumer had, so it threw on every indexed map.

IndexedSourceMapConsumer already keeps every section's mappings, shifted
by the section offset and sorted by original position, in
_originalMappings. Give it the two methods the shared implementation
needs: _findSourceIndex maps a source name (in any form a section
accepts) to its index in this._sources, and _findMapping is the generic
binary search from BasicSourceMapConsumer. Results then cover every
section that has the source, with the same fuzzy line and column
matching as a map without sections.

generatedPositionFor on a nested indexed map called _findSourceIndex on
the nested consumer and threw for the same reason; it works now too.

Fixes #103
Base automatically changed from fix/index-map-column-offset to main November 19, 2026 11:27
@7rulnik
7rulnik marked this pull request as ready for review November 19, 2026 11:28
@7rulnik
7rulnik merged commit 2a7214f into main Nov 19, 2026
6 checks passed
@7rulnik
7rulnik deleted the fix/issue-103 branch November 19, 2026 11:28
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.

allGeneratedPositionsFor throws on index source maps

1 participant