From 925582d03487b2ca3c2454527c9bedca60ccec6c Mon Sep 17 00:00:00 2001 From: Ben Lichtman Date: Tue, 17 Dec 2019 17:04:54 -0800 Subject: [PATCH 1/3] Add asserts to narrow down position issue --- src/server/scriptInfo.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/server/scriptInfo.ts b/src/server/scriptInfo.ts index 15ab756887c..4f471d6ab37 100644 --- a/src/server/scriptInfo.ts +++ b/src/server/scriptInfo.ts @@ -622,7 +622,10 @@ namespace ts.server { } positionToLineOffset(position: number): protocol.Location { - return this.textStorage.positionToLineOffset(position); + const location = this.textStorage.positionToLineOffset(position); + Debug.assert(typeof location.line === "number" && location.line > 0, `Expected line ${location.line} to be greater than 0.`); + Debug.assert(typeof location.offset === "number" && location.offset > 0, `Expected offset ${location.offset} to be greater than 0.`); + return location; } public isJavaScript() { From 42dc4155ce13d80f31875d2525f64e31e4180927 Mon Sep 17 00:00:00 2001 From: Ben Lichtman Date: Tue, 17 Dec 2019 17:41:38 -0800 Subject: [PATCH 2/3] Refactor to make failure messages more consistent --- src/server/scriptInfo.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/server/scriptInfo.ts b/src/server/scriptInfo.ts index 4f471d6ab37..e3f4bd2d195 100644 --- a/src/server/scriptInfo.ts +++ b/src/server/scriptInfo.ts @@ -623,8 +623,7 @@ namespace ts.server { positionToLineOffset(position: number): protocol.Location { const location = this.textStorage.positionToLineOffset(position); - Debug.assert(typeof location.line === "number" && location.line > 0, `Expected line ${location.line} to be greater than 0.`); - Debug.assert(typeof location.offset === "number" && location.offset > 0, `Expected offset ${location.offset} to be greater than 0.`); + failIfInvalidLocation(location); return location; } @@ -645,4 +644,13 @@ namespace ts.server { } } } + + /*@internal*/ + 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"}`); + } } From 9f1e389441bb5ec92338c216df45efbcd0a008de Mon Sep 17 00:00:00 2001 From: Ben Lichtman Date: Wed, 18 Dec 2019 10:44:40 -0800 Subject: [PATCH 3/3] Remove unnecessary internal tags, handle bad input --- src/server/scriptInfo.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/server/scriptInfo.ts b/src/server/scriptInfo.ts index e3f4bd2d195..53bbc6cba75 100644 --- a/src/server/scriptInfo.ts +++ b/src/server/scriptInfo.ts @@ -622,6 +622,7 @@ namespace ts.server { } positionToLineOffset(position: number): protocol.Location { + failIfInvalidPosition(position); const location = this.textStorage.positionToLineOffset(position); failIfInvalidLocation(location); return location; @@ -645,7 +646,11 @@ namespace ts.server { } } - /*@internal*/ + 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.`);