Skip to content

Commit 4fdbacf

Browse files
committed
refactor(linter/plugins): add more debug assertions (#16508)
Add more debug assertions to functions related to source text lines.
1 parent 77ee7c9 commit 4fdbacf

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { ast, initAst, initSourceText, sourceText } from "./source_code.ts";
77
import visitorKeys from "../generated/keys.ts";
8-
import { debugAssertIsNonNull } from "../utils/asserts.ts";
8+
import { debugAssert, debugAssertIsNonNull } from "../utils/asserts.ts";
99

1010
import type { Node } from "./types.ts";
1111
import type { Node as ESTreeNode } from "../generated/types.d.ts";
@@ -79,7 +79,11 @@ export function initLines(): void {
7979
* and uses match.index to get the correct line start indices.
8080
*/
8181

82-
// Note: `lineStartIndices` starts as `[0]`
82+
// `lineStartIndices` starts as `[0]`, and is reset to length 1 in `resetLines`.
83+
// Debug check that `lines` and `lineStartIndices` are not already initialized.
84+
debugAssert(lines.length === 0);
85+
debugAssert(lineStartIndices.length === 1);
86+
8387
let lastOffset = 0,
8488
offset,
8589
match;
@@ -89,6 +93,17 @@ export function initLines(): void {
8993
lineStartIndices.push((lastOffset = offset + match[0].length));
9094
}
9195
lines.push(sourceText.slice(lastOffset));
96+
97+
debugAssertLinesIsInitialized();
98+
}
99+
100+
/**
101+
* Debug assert that `lines` and `lineStartIndices` are initialized.
102+
* No-op in release build - TSDown will remove this function and all calls to it.
103+
*/
104+
function debugAssertLinesIsInitialized(): void {
105+
debugAssert(lines.length > 0);
106+
debugAssert(lines.length === lineStartIndices.length);
92107
}
93108

94109
/**
@@ -115,6 +130,7 @@ export function getLineColumnFromOffset(offset: number): LineColumn {
115130
// This also decodes `sourceText` if it wasn't already.
116131
if (lines.length === 0) initLines();
117132
debugAssertIsNonNull(sourceText);
133+
debugAssertLinesIsInitialized();
118134

119135
if (offset > sourceText.length) {
120136
throw new RangeError(
@@ -134,6 +150,8 @@ export function getLineColumnFromOffset(offset: number): LineColumn {
134150
* @returns `{line, column}` location object with 1-indexed line and 0-indexed column.
135151
*/
136152
function getLineColumnFromOffsetUnchecked(offset: number): LineColumn {
153+
debugAssertLinesIsInitialized();
154+
137155
// Binary search `lineStartIndices` for the line containing `offset`
138156
let low = 0,
139157
high = lineStartIndices.length,
@@ -170,6 +188,7 @@ export function getOffsetFromLineColumn(loc: LineColumn): number {
170188
// This also decodes `sourceText` if it wasn't already.
171189
if (lines.length === 0) initLines();
172190
debugAssertIsNonNull(sourceText);
191+
debugAssertLinesIsInitialized();
173192

174193
const linesCount = lineStartIndices.length;
175194
if (line <= 0 || line > linesCount) {

0 commit comments

Comments
 (0)