Skip to content

Fix originalPositionFor at the start of an index map section - #99

Merged
7rulnik merged 1 commit into
mainfrom
fix/issue-82
Nov 19, 2026
Merged

7rulnik merged 1 commit into
mainfrom
fix/issue-82

Conversation

@7rulnik

@7rulnik 7rulnik commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Fixes #82.

The bug

In an index map, originalPositionFor returned all-null fields for the exact position where a section starts, even when the section has a mapping there. One column later worked. With the map from the issue:

Lookup Before After
1:0 null a.js 1:0
2:4 null b.js 1:0
1:2 a.js 1:2 a.js 1:2
2:5 b.js 1:0 b.js 1:0

The "after" column matches trace-mapping 0.3.31 for the same map. source-map 0.6.1 has the same bug: the code comes from there.

Cause

IndexedSourceMapConsumer#originalPositionFor first binary-searches for the section that contains the position. Its comparator subtracted section.generatedOffset.generatedColumn, which is stored 1-based (offset.column + 1), from the needle's 0-based column. At a section's first column the comparator returned -1 instead of 0, so the search picked the previous section, or none. The code that then converts the needle into the section's coordinates already subtracts the 1; this does the same in the comparator.

Not fixed here

A separate bug in the same area: the column offset of a section whose offset.line is above 0 is applied to the section's second line instead of its first, in decoding (lib/source-map-consumer.js:1267) and in generatedPositionFor (line 1210). test/test-ecma426-conformance.js has a todo test for it.

Testing

New regression test with the map from the issue. pnpm test (227 tests), pnpm test:coverage and pnpm format:check pass on Node 26.13.1.

The section search compared the needle's 0-based column with the
section's 1-based generatedOffset.generatedColumn, so a lookup at a
section's exact first position landed in the previous section (or none)
and returned nulls.

Fixes #82
@7rulnik
7rulnik marked this pull request as ready for review November 19, 2026 10:25
@7rulnik
7rulnik merged commit e561b1f into main Nov 19, 2026
6 checks passed
@7rulnik
7rulnik deleted the fix/issue-82 branch November 19, 2026 10:26
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.

IndexedSourceMapConsumer.originalPositionFor returns null at the start column of every section

1 participant