Skip to content

Commit 3c153d8

Browse files
Merge pull request microsoft#35742 from uniqueiniquity/addPositionAssert
Add asserts to narrow down position issue
2 parents 1952ccc + 9f1e389 commit 3c153d8

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/server/scriptInfo.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,10 @@ namespace ts.server {
622622
}
623623

624624
positionToLineOffset(position: number): protocol.Location {
625-
return this.textStorage.positionToLineOffset(position);
625+
failIfInvalidPosition(position);
626+
const location = this.textStorage.positionToLineOffset(position);
627+
failIfInvalidLocation(location);
628+
return location;
626629
}
627630

628631
public isJavaScript() {
@@ -642,4 +645,17 @@ namespace ts.server {
642645
}
643646
}
644647
}
648+
649+
function failIfInvalidPosition(position: number) {
650+
Debug.assert(typeof position === "number", `Expected position ${position} to be a number.`);
651+
Debug.assert(position >= 0, `Expected position to be non-negative.`);
652+
}
653+
654+
function failIfInvalidLocation(location: protocol.Location) {
655+
Debug.assert(typeof location.line === "number", `Expected line ${location.line} to be a number.`);
656+
Debug.assert(typeof location.offset === "number", `Expected offset ${location.offset} to be a number.`);
657+
658+
Debug.assert(location.line > 0, `Expected line to be non-${location.line === 0 ? "zero" : "negative"}`);
659+
Debug.assert(location.offset > 0, `Expected offset to be non-${location.offset === 0 ? "zero" : "negative"}`);
660+
}
645661
}

0 commit comments

Comments
 (0)