Skip to content

Commit 77ee7c9

Browse files
committed
refactor(linter/plugins): rename lineStartIndices (#16507)
Pure refactor. Rename `lineStartOffsets` to `lineStartIndices`, to match ESLint.
1 parent 459e2b8 commit 77ee7c9

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

apps/oxlint/src-js/plugins/location.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export interface LineColumn {
5555
const LINE_BREAK_PATTERN = /\r\n|[\r\n\u2028\u2029]/gu;
5656

5757
// Lazily populated when `SOURCE_CODE.lines` is accessed.
58-
// `lineStartOffsets` starts as `[0]`, and `resetLines` doesn't remove that initial element, so it's never empty.
58+
// `lineStartIndices` starts as `[0]`, and `resetLines` doesn't remove that initial element, so it's never empty.
5959
export const lines: string[] = [];
60-
const lineStartOffsets: number[] = [0];
60+
const lineStartIndices: number[] = [0];
6161

6262
/**
6363
* Split source text into lines.
@@ -79,14 +79,14 @@ export function initLines(): void {
7979
* and uses match.index to get the correct line start indices.
8080
*/
8181

82-
// Note: `lineStartOffsets` starts as `[0]`
82+
// Note: `lineStartIndices` starts as `[0]`
8383
let lastOffset = 0,
8484
offset,
8585
match;
8686
while ((match = LINE_BREAK_PATTERN.exec(sourceText)) !== null) {
8787
offset = match.index;
8888
lines.push(sourceText.slice(lastOffset, offset));
89-
lineStartOffsets.push((lastOffset = offset + match[0].length));
89+
lineStartIndices.push((lastOffset = offset + match[0].length));
9090
}
9191
lines.push(sourceText.slice(lastOffset));
9292
}
@@ -97,7 +97,7 @@ export function initLines(): void {
9797
export function resetLines(): void {
9898
lines.length = 0;
9999
// Leave first entry (0) in place, discard the rest
100-
lineStartOffsets.length = 1;
100+
lineStartIndices.length = 1;
101101
}
102102

103103
/**
@@ -111,7 +111,7 @@ export function getLineColumnFromOffset(offset: number): LineColumn {
111111
throw new TypeError("Expected `offset` to be a non-negative integer.");
112112
}
113113

114-
// Build `lines` and `lineStartOffsets` tables if they haven't been already.
114+
// Build `lines` and `lineStartIndices` tables if they haven't been already.
115115
// This also decodes `sourceText` if it wasn't already.
116116
if (lines.length === 0) initLines();
117117
debugAssertIsNonNull(sourceText);
@@ -128,26 +128,26 @@ export function getLineColumnFromOffset(offset: number): LineColumn {
128128
/**
129129
* Convert a source text index into a (line, column) pair without:
130130
* 1. Checking type of `offset`, or that it's in range.
131-
* 2. Initializing `lineStartOffsets`. Caller must do that before calling this method.
131+
* 2. Initializing `lineStartIndices`. Caller must do that before calling this method.
132132
*
133133
* @param offset - The index of a character in a file.
134134
* @returns `{line, column}` location object with 1-indexed line and 0-indexed column.
135135
*/
136136
function getLineColumnFromOffsetUnchecked(offset: number): LineColumn {
137-
// Binary search `lineStartOffsets` for the line containing `offset`
137+
// Binary search `lineStartIndices` for the line containing `offset`
138138
let low = 0,
139-
high = lineStartOffsets.length,
139+
high = lineStartIndices.length,
140140
mid: number;
141141
do {
142142
mid = ((low + high) / 2) | 0; // Use bitwise OR to floor the division
143-
if (offset < lineStartOffsets[mid]) {
143+
if (offset < lineStartIndices[mid]) {
144144
high = mid;
145145
} else {
146146
low = mid + 1;
147147
}
148148
} while (low < high);
149149

150-
return { line: low, column: offset - lineStartOffsets[low - 1] };
150+
return { line: low, column: offset - lineStartIndices[low - 1] };
151151
}
152152

153153
/**
@@ -166,12 +166,12 @@ export function getOffsetFromLineColumn(loc: LineColumn): number {
166166
(line | 0) === line &&
167167
(column | 0) === column
168168
) {
169-
// Build `lines` and `lineStartOffsets` tables if they haven't been already.
169+
// Build `lines` and `lineStartIndices` tables if they haven't been already.
170170
// This also decodes `sourceText` if it wasn't already.
171171
if (lines.length === 0) initLines();
172172
debugAssertIsNonNull(sourceText);
173173

174-
const linesCount = lineStartOffsets.length;
174+
const linesCount = lineStartIndices.length;
175175
if (line <= 0 || line > linesCount) {
176176
throw new RangeError(
177177
`Line number out of range (line ${line} requested). ` +
@@ -180,7 +180,7 @@ export function getOffsetFromLineColumn(loc: LineColumn): number {
180180
}
181181
if (column < 0) throw new RangeError(`Invalid column number (column ${column} requested).`);
182182

183-
const lineOffset = lineStartOffsets[line - 1];
183+
const lineOffset = lineStartIndices[line - 1];
184184
const offset = lineOffset + column;
185185

186186
// Comment from ESLint implementation:
@@ -198,7 +198,7 @@ export function getOffsetFromLineColumn(loc: LineColumn): number {
198198
nextLineOffset = sourceText.length;
199199
if (offset <= nextLineOffset) return offset;
200200
} else {
201-
nextLineOffset = lineStartOffsets[line];
201+
nextLineOffset = lineStartIndices[line];
202202
if (offset < nextLineOffset) return offset;
203203
}
204204

@@ -226,7 +226,7 @@ export function getOffsetFromLineColumn(loc: LineColumn): number {
226226
* @returns Location
227227
*/
228228
export function getNodeLoc(node: Node): Location {
229-
// Build `lines` and `lineStartOffsets` tables if they haven't been already.
229+
// Build `lines` and `lineStartIndices` tables if they haven't been already.
230230
// This also decodes `sourceText` if it wasn't already.
231231
if (lines.length === 0) initLines();
232232

0 commit comments

Comments
 (0)