Honor TokenReader subrange bounds in peek and backtrack. - #485
Open
Christopher Pruijsen (cpruijsen) wants to merge 1 commit into
Open
Honor TokenReader subrange bounds in peek and backtrack.#485Christopher Pruijsen (cpruijsen) wants to merge 1 commit into
Christopher Pruijsen (cpruijsen) wants to merge 1 commit into
Conversation
Embedded readers could observe tokens outside their TokenSequence window because peekToken, peekPreviousTokenKind, and backtrackToMarker ignored the start/end indexes that the other peek methods already enforce. Fixes microsoft#481
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.
TokenReader.peekToken(),peekPreviousTokenKind(), andbacktrackToMarker()now honor the same[_readerStartIndex, _readerEndIndex)window thatpeekTokenKind()/peekTokenAfterKind()/peekTokenAfterAfterKind()already enforce. Fixes #481: an embedded reader could observe the token before its start or after its end, andbacktrackToMarkercould rewind into the outer stream.TokenReaderis not a public export. CurrentNodeParserhot paths already callpeekTokenKind()first, which is why existing parser snapshots did not fail.Decision
peekToken()past the window returns the stream'sEndOfInputtoken (tokens[tokens.length - 1]);peekPreviousTokenKind()treats_readerStartIndexas start-of-input;backtrackToMarkerthrows ifmarker < _readerStartIndex.Alternative: throw from
peekToken()(likereadToken()), or clamp an out-of-range marker to the window start. Open PR #482 takes the throw-from-peekToken()option.readToken()already states that peek must always return a validToken, and the other peek methods already signalEndOfInputat the window edge. Expired markers already throw as a parser bug. Can switch to throw-on-peek or clamp-on-backtrack.Test plan
tsdoc/src/parser/__tests__/TokenReader.test.tsfails without the source change and passes with it (embedded predecessor leak, next-token leak, emptyTokenSequence.createEmpty, out-of-range backtrack).@microsoft/tsdocHeft test run (typecheck / ESLint / API Extractor / 259 Jest tests).rush change --verifyon CI (change file included).Change log
Rush change file:
common/changes/@microsoft/tsdoc/fix-tokenreader-bounds_2026-09-11-13-00.json(patch).