Repository navigation
Support allGeneratedPositionsFor on indexed source maps - #105
Merged
Merged
Conversation
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
This was referenced Nov 19, 2026
7rulnik
marked this pull request as ready for review
November 19, 2026 11:28
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.
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
allGeneratedPositionsForthrewTypeError: this._findSourceIndex is not a functionon every indexed source map. It's defined onSourceMapConsumer.prototypeand callsthis._findSourceIndexandthis._findMapping, which onlyBasicSourceMapConsumerhad.Fix
IndexedSourceMapConsumeralready 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:_findSourceIndexmaps a source name to its index inthis._sources. It accepts any form a section accepts: relative to the section'ssourceRoot, or a URL fromsources._findMappingis the generic binary search fromBasicSourceMapConsumer.That gives the same behaviour as a map without sections:
util.testMapandutil.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-mappingdoes. That column 4 depends on #104.Side effect:
generatedPositionForon a nested indexed map threw the sameTypeError, because it calls_findSourceIndexon each section's consumer. It works now.Not fixed here
The indexed
generatedPositionForreturns a made-up position instead of nulls when the first section that has the source finds no match. It adds the section offset toline: null. With b.js mapped only at original line 5, in a section atoffset: { 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:
testMap;All three fail without the change.
pnpm test(233 tests) passes on Node 22, 24 and 26.13.1, andpnpm test:coverageandpnpm format:checkpass on 26.13.1.