Skip to content

fix: clamp PositionToOffset.convert on the line just past the end - #2434

Merged
Connor Peet (connor4312) merged 1 commit into
microsoft:mainfrom
kwy404:fix/position-to-offset-last-line
Oct 26, 2026
Merged

Connor Peet (connor4312) merged 1 commit into
microsoft:mainfrom
kwy404:fix/position-to-offset-last-line

Conversation

@kwy404

Copy link
Copy Markdown
Contributor

Root cause: PositionToOffset.convert in src/common/stringUtils.ts is meant to clamp any line past the end of the source to source.length (the existing test expects line 10 of '\nhello\nworld\n' to give 13). The guard uses lineNumber > this.lines.length, so a line number equal to this.lines.length slips through, this.lines[lineNumber] is undefined, and the result is NaN. For example new PositionToOffset('hello\nworld').convert(new Base0Position(2, 0)) returns NaN instead of 11. With a NaN offset the REPL completions match no node, and extractScopeRenames assigns it to a sticky regex lastIndex, which reads it as 0 and picks up the identifier at the start of the file. getLineOffset in the same class already uses >=.

Fix: change the guard to >=, matching getLineOffset.

Test: added two assertions to the existing positionToOffset test in src/common/stringUtils.test.ts for the line right after the last one. They fail before the change (expected NaN to equal 11) and pass after.

  • Ran: npm run test:unit. The only difference from main is the fixed test (the NodeBinaryProvider failures are local environment ones and happen on main too).
  • Ran: npm run test:types, dprint check and eslint on the touched files, all clean.

@connor4312 Connor Peet (connor4312) left a comment

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.

thanks!

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.

4 participants