-
-
Notifications
You must be signed in to change notification settings - Fork 881
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
Closed
overlookmotel
wants to merge
1
commit into
main
from
10-02-feat_linter_plugins_implement_sourcecode_getindexfromloc_method
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,6 +86,8 @@ export function getAst(): Program { | |||||||||
| * Split source text into lines. | ||||||||||
| */ | ||||||||||
| function initLines(): void { | ||||||||||
| if (lines.length !== 0) return; | ||||||||||
|
|
||||||||||
| if (sourceText === null) initSourceText(); | ||||||||||
|
|
||||||||||
| // This implementation is based on the one in ESLint. | ||||||||||
|
|
@@ -174,7 +176,7 @@ export const SOURCE_CODE = Object.freeze({ | |||||||||
|
|
||||||||||
| // Get source text as array of lines, split according to specification's definition of line breaks. | ||||||||||
| get lines(): string[] { | ||||||||||
| if (lines.length === 0) initLines(); | ||||||||||
| initLines(); | ||||||||||
| return lines; | ||||||||||
| }, | ||||||||||
|
|
||||||||||
|
|
@@ -525,7 +527,50 @@ export const SOURCE_CODE = Object.freeze({ | |||||||||
| */ | ||||||||||
| // oxlint-disable-next-line no-unused-vars | ||||||||||
| getIndexFromLoc(loc: LineColumn): number { | ||||||||||
| throw new Error('`sourceCode.getIndexFromLoc` not implemented yet'); // TODO | ||||||||||
| if (loc !== null && typeof loc === 'object') { | ||||||||||
| const { line, column } = loc; | ||||||||||
| if (typeof line === 'number' && typeof column === 'number') { | ||||||||||
| initLines(); | ||||||||||
|
|
||||||||||
| const linesCount = lineStartOffsets.length; | ||||||||||
| if (line <= 0 || line > linesCount) { | ||||||||||
| throw new RangeError( | ||||||||||
| `Line number out of range (line ${line} requested). ` + | ||||||||||
| `Line numbers should be 1-based, and less than or equal to number of lines in file (${linesCount}).`, | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| if (column < 0) throw new RangeError(`Invalid column number (column ${column} requested).`); | ||||||||||
|
|
||||||||||
| const lineStartOffset = lineStartOffsets[line - 1]; | ||||||||||
| const offset = lineStartOffset + column; | ||||||||||
|
|
||||||||||
| // Comment from ESLint implementation: | ||||||||||
| /* | ||||||||||
| * By design, `getIndexFromLoc({ line: lineNum, column: 0 })` should return the start index of | ||||||||||
| * the given line, provided that the line number is valid element of `lines`. Since the | ||||||||||
| * last element of `lines` is an empty string for files with trailing newlines, add a | ||||||||||
| * special case where getting the index for the first location after the end of the file | ||||||||||
| * will return the length of the file, rather than throwing an error. This allows rules to | ||||||||||
| * use `getIndexFromLoc` consistently without worrying about edge cases at the end of a file. | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| let lineEndOffset; | ||||||||||
| if (line === linesCount) { | ||||||||||
| lineEndOffset = sourceText.length; | ||||||||||
| if (offset <= lineEndOffset) return offset; | ||||||||||
| } else { | ||||||||||
|
||||||||||
| } else { | |
| } else { | |
| // line is guaranteed to be < linesCount here, so lineStartOffsets[line] is in bounds | |
| console.assert(line < linesCount, "lineStartOffsets[line] out of bounds"); |
Oops, something went wrong.
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.
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 callsinitLines()which may callinitSourceText(), but there's no guarantee thatsourceTextis not null at this point. Add a null check or ensuresourceTextis properly initialized.