Skip to content

Commit 058e3ad

Browse files
author
Andy
authored
Improve assertion in computePositionOfLineAndCharacter (microsoft#21361)
1 parent e6685ab commit 058e3ad

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/compiler/core.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ namespace ts {
340340
return false;
341341
}
342342

343+
export function arraysEqual<T>(a: ReadonlyArray<T>, b: ReadonlyArray<T>, equalityComparer: EqualityComparer<T> = equateValues): boolean {
344+
return a.length === b.length && a.every((x, i) => equalityComparer(x, b[i]));
345+
}
346+
343347
export function indexOfAnyCharCode(text: string, charCodes: ReadonlyArray<number>, start?: number): number {
344348
for (let i = start || 0; i < text.length; i++) {
345349
if (contains(charCodes, text.charCodeAt(i))) {

src/compiler/scanner.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,10 @@ namespace ts {
335335

336336
/* @internal */
337337
export function computePositionOfLineAndCharacter(lineStarts: ReadonlyArray<number>, line: number, character: number, debugText?: string): number {
338-
Debug.assert(line >= 0 && line < lineStarts.length);
338+
if (line < 0 || line >= lineStarts.length) {
339+
Debug.fail(`Bad line number. Line: ${line}, lineStarts.length: ${lineStarts.length} , line map is correct? ${debugText !== undefined ? arraysEqual(lineStarts, computeLineStarts(debugText)) : "unknown"}`);
340+
}
341+
339342
const res = lineStarts[line] + character;
340343
if (line < lineStarts.length - 1) {
341344
Debug.assert(res < lineStarts[line + 1]);

0 commit comments

Comments
 (0)