Skip to content

Commit f064f80

Browse files
committed
docs(linter/plugins): correct comment about offset to line-column conversion (#20506)
Correct a comment. It didn't accurately describe what the code does.
1 parent a187333 commit f064f80

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ export function getLineColumnFromOffset(offset: number): LineColumn {
171171
function populateLineColumn(offset: number, out: LineColumn): void {
172172
debugAssertLinesIsInitialized();
173173

174-
// Binary search `lineStartIndices` for the line containing `offset`
174+
// Find first line that starts *after* `offset`, via binary search of `lineStartIndices`.
175+
// `lineStartIndices` is sorted and `lineStartIndices[0]` is always 0.
176+
//
177+
// After the loop, `low` is the index of the first line whose start is *past* `offset`.
178+
// This is also the 1-indexed line number of the line containing `offset`.
179+
// e.g. if `offset` is on the 3rd line, `low` = 3, and `lineStartIndices[2]` is that line's start.
180+
// `do...while` is safe because `lineStartIndices` always has at least one entry, so `low < high` at start of loop.
175181
let low = 0,
176182
high = lineStartIndices.length,
177183
mid: number;
@@ -184,8 +190,8 @@ function populateLineColumn(offset: number, out: LineColumn): void {
184190
}
185191
} while (low < high);
186192

187-
out.line = low;
188-
out.column = offset - lineStartIndices[low - 1];
193+
out.line = low; // 1-indexed line number
194+
out.column = offset - lineStartIndices[low - 1]; // Offset from start of the line
189195
}
190196

191197
/**

0 commit comments

Comments
 (0)