-
-
Notifications
You must be signed in to change notification settings - Fork 722
feat(linter/plugins): implement SourceCode#getIndexFromLoc method
#14295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(linter/plugins): implement SourceCode#getIndexFromLoc method
#14295
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements the sourceCode.getIndexFromLoc method that converts line/column positions to character indices in the source text. The implementation uses the lines array infrastructure added in a previous PR.
- Implements complete
getIndexFromLocmethod with input validation and edge case handling - Optimizes
initLinesfunction by adding early return guard to prevent redundant initialization - Replaces explicit length check with direct
initLines()call in lines getter
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| */ | ||
|
|
||
| let lineEndOffset; | ||
| if (line === linesCount) { |
Copilot
AI
Oct 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing null check for sourceText. The code calls initLines() which may call initSourceText(), but there's no guarantee that sourceText is not null at this point. Add a null check or ensure sourceText is properly initialized.
| if (line === linesCount) { | |
| if (line === linesCount) { | |
| if (sourceText == null) { | |
| throw new TypeError("sourceText is null or undefined when accessing its length."); | |
| } |
| if (line === linesCount) { | ||
| lineEndOffset = sourceText.length; | ||
| if (offset <= lineEndOffset) return offset; | ||
| } else { |
Copilot
AI
Oct 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential array bounds error. The code accesses lineStartOffsets[line] where line could equal linesCount, but lineStartOffsets may only have linesCount elements (0-indexed). This could cause an out-of-bounds access when line === linesCount.
| } else { | |
| } else { | |
| // line is guaranteed to be < linesCount here, so lineStartOffsets[line] is in bounds | |
| console.assert(line < linesCount, "lineStartOffsets[line] out of bounds"); |
|
Superceded by #14303. |

WIP. Need to add tests and refactor.
Implement
sourceCode.getIndexFromLocAPI, using thelinesarray added in #14290.