Skip to content

Range mappings: Change index encoding to start at 0#252

Merged
takikawa merged 4 commits intotc39:proposal-range-mappingsfrom
takikawa:range-mappings-0-encoding
Apr 15, 2026
Merged

Range mappings: Change index encoding to start at 0#252
takikawa merged 4 commits intotc39:proposal-range-mappingsfrom
takikawa:range-mappings-0-encoding

Conversation

@takikawa
Copy link
Copy Markdown
Collaborator

@takikawa takikawa commented Apr 8, 2026

This implements the change discussed in the April TG4 meeting about reverting the range mapping encoding so that it will start at 0 with relative offset instead of at -1.

Currently "BB" means the first and second mappings are
range mappings. Instead, we'll have "AB" encode the same.
"BA" will be an (optional) error because a duplicate
index is encoded.
@takikawa takikawa changed the title Change range mapping index encoding to start at 0 Range mappings: Change index encoding to start at 0 Apr 8, 2026
Comment thread spec.emu Outdated
<emu-alg>
1. Let _relativeIndex_ be the VLQUnsignedValue of |Vlq|.
1. If _relativeIndex_ is 0, then
1. If _relativeIndex_ = 0 and _state_.[[MappingIndex]] ≠ 0, then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this means AA is valid, both marking the 0th mapping as a range.

This comment was marked as spam.

@szuend
Copy link
Copy Markdown
Collaborator

szuend commented Apr 9, 2026

One alternative that I forgot about yesterday, is that we could solve this in the grammar itself. Then you don't even need the check. We add a VlqWithoutZero or NonZeroVlq alongside Vlq that doesn't allow A.

Then the range mapping grammar is something like:

RangeMappingList :
  FirstRangeMapping
  FirstRangeMapping RangeMappingListRest

RangeMappingListRest :
  RangeMapping
  RangeMapping RangeMappingListRest

FirstRangeMapping :
  Vlq

RangeMapping :
  NonZeroVlq

Probably a lot of addition to the spec for one use-site. But maybe something to keep in mind if we ever have a need for non-zero VLQs (both signed or unsigned).

Disallows a range mapping string like "AA" that
wasn't covered by the previous attempt.
@takikawa
Copy link
Copy Markdown
Collaborator Author

I think the NonZeroVlq approach is a nice way to do it too, but for now I went with something that adds less to the core grammar.

In the latest push, I initialized the decode state's [[MappingIndex]] to NONE and then just check that a relative index of 0 can't appear if the state isn't NONE. I think this should address @jridgewell's comment about the old check not catching "AA" (thanks for pointing that out!).

Comment thread spec.emu
</tr>
<tr>
<td>[[MappingIndex]]</td>
<td>a non-negative integer or ~none~</td>
Copy link
Copy Markdown
Member

@jridgewell jridgewell Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could allow this to be negative, initialize to -1, and verify it's positive in appending the new Range Mapping Offset Record.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer ~none~, as it makes the intent clearer when reading.

@takikawa takikawa merged commit cd34a78 into tc39:proposal-range-mappings Apr 15, 2026
3 checks passed
khan7546jsn-afk

This comment was marked as spam.

jridgewell pushed a commit to jridgewell/sourcemaps that referenced this pull request Apr 20, 2026
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.

5 participants