feat(linter/plugins): implement SourceCode#getIndexFromLoc method#14295
feat(linter/plugins): implement SourceCode#getIndexFromLoc method#14295overlookmotel wants to merge 1 commit intomainfrom
SourceCode#getIndexFromLoc method#14295Conversation
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.
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) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.