Skip to content

Commit 6e69bdb

Browse files
author
Andy Hanson
committed
Handle empty document -- root node with 0 children
1 parent 09bacbd commit 6e69bdb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/harness/unittests/versionCache.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ namespace ts {
1010
return lineIndex.absolutePositionOfStartOfLine(line) + (col - 1);
1111
}
1212

13+
function foo(lineIndex: server.LineIndex) {
14+
lineIndex.load([]);
15+
lineIndex.positionToLineOffset(0);
16+
}
17+
foo;
18+
1319
function validateEdit(lineIndex: server.LineIndex, sourceText: string, position: number, deleteLength: number, insertString: string): void {
1420
const checkText = editFlat(position, deleteLength, insertString, sourceText);
1521
const snapshot = lineIndex.edit(position, deleteLength, insertString);
@@ -49,6 +55,12 @@ var q:Point=<Point>p;`;
4955
validateEditAtLineCharIndex = undefined;
5056
});
5157

58+
it("handles empty lines array", () => {
59+
const lineIndex = new server.LineIndex();
60+
lineIndex.load([]);
61+
assert.equal(lineIndex.positionToLineOffset(0), { line: 0, offset: 0 });
62+
});
63+
5264
it(`change 9 1 0 1 {"y"}`, () => {
5365
validateEditAtLineCharIndex(9, 1, 0, "y");
5466
});

src/server/scriptVersionCache.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,10 @@ namespace ts.server {
660660
// Input position is relative to the start of this node.
661661
// Output line number is absolute.
662662
charOffsetToLineInfo(lineNumberAccumulator: number, relativePosition: number): { oneBasedLine: number, zeroBasedColumn: number, lineText: string | undefined } {
663-
Debug.assert(this.children.length !== 0);
663+
if (this.children.length === 0) {
664+
// Root node might have no children if this is an empty document.
665+
return { oneBasedLine: lineNumberAccumulator, zeroBasedColumn: relativePosition, lineText: undefined };
666+
}
664667

665668
for (const child of this.children) {
666669
if (child.charCount() > relativePosition) {

0 commit comments

Comments
 (0)