Merge pull request #35742 from uniqueiniquity/addPositionAssert

Add asserts to narrow down position issue
This commit is contained in:
Ben Lichtman
2019-12-18 14:19:29 -08:00
committed by GitHub

View File

@@ -622,7 +622,10 @@ namespace ts.server {
}
positionToLineOffset(position: number): protocol.Location {
return this.textStorage.positionToLineOffset(position);
failIfInvalidPosition(position);
const location = this.textStorage.positionToLineOffset(position);
failIfInvalidLocation(location);
return location;
}
public isJavaScript() {
@@ -642,4 +645,17 @@ namespace ts.server {
}
}
}
function failIfInvalidPosition(position: number) {
Debug.assert(typeof position === "number", `Expected position ${position} to be a number.`);
Debug.assert(position >= 0, `Expected position to be non-negative.`);
}
function failIfInvalidLocation(location: protocol.Location) {
Debug.assert(typeof location.line === "number", `Expected line ${location.line} to be a number.`);
Debug.assert(typeof location.offset === "number", `Expected offset ${location.offset} to be a number.`);
Debug.assert(location.line > 0, `Expected line to be non-${location.line === 0 ? "zero" : "negative"}`);
Debug.assert(location.offset > 0, `Expected offset to be non-${location.offset === 0 ? "zero" : "negative"}`);
}
}