Skip to content

Commit 199451d

Browse files
committed
Merge branch 'main' of https://github.com/microsoft/TypeScript into fix-43836
2 parents 2ff3a38 + fdc31ba commit 199451d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

scripts/eslint/rules/boolean-trivia.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export = createRule({
2323
const sourceCodeText = sourceCode.getText();
2424

2525
const isSetOrAssert = (name: string): boolean => name.startsWith("set") || name.startsWith("assert");
26-
const isTrivia = (node: TSESTree.CallExpressionArgument): boolean => {
26+
const isTrivia = (node: TSESTree.Node): boolean => {
2727
if (node.type === AST_NODE_TYPES.Identifier) {
2828
return node.name === "undefined";
2929
}
@@ -69,7 +69,7 @@ export = createRule({
6969
return false;
7070
};
7171

72-
const checkArg = (node: TSESTree.CallExpressionArgument): void => {
72+
const checkArg = (node: TSESTree.Node): void => {
7373
if (!isTrivia(node)) {
7474
return;
7575
}

src/server/scriptVersionCache.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ namespace ts.server {
6767
}
6868
const lm = LineIndex.linesFromText(insertedText);
6969
const lines = lm.lines;
70-
if (lines.length > 1) {
71-
if (lines[lines.length - 1] === "") {
72-
lines.pop();
73-
}
70+
if (lines.length > 1 && lines[lines.length - 1] === "") {
71+
lines.pop();
7472
}
7573
let branchParent: LineNode | undefined;
7674
let lastZeroCount: LineCollection | undefined;
@@ -683,8 +681,12 @@ namespace ts.server {
683681
}
684682

685683
// Skipped all children
686-
const { leaf } = this.lineNumberToInfo(this.lineCount(), 0);
687-
return { oneBasedLine: this.lineCount(), zeroBasedColumn: leaf ? leaf.charCount() : 0, lineText: undefined };
684+
const lineCount = this.lineCount();
685+
if (lineCount === 0) { // it's empty! (and lineNumberToInfo expects a one-based line)
686+
return { oneBasedLine: 1, zeroBasedColumn: 0, lineText: undefined };
687+
}
688+
const leaf = Debug.checkDefined(this.lineNumberToInfo(lineCount, 0).leaf);
689+
return { oneBasedLine: lineCount, zeroBasedColumn: leaf.charCount(), lineText: undefined };
688690
}
689691

690692
/**

src/testRunner/unittests/tsserver/versionCache.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ var q:Point=<Point>p;`;
5252
assert.deepEqual(lineIndex.positionToLineOffset(0), { line: 1, offset: 1 });
5353
});
5454

55+
it("handles emptying whole file (GH#44518)", () => {
56+
// See below for the main thing that this tests; it would be better to have a test
57+
// that uses `ScriptInfo.positionToLineOffset` but I couldn't find away to do that
58+
const { lines } = server.LineIndex.linesFromText("function foo() {\n\ndsa\n\n}\n\nfo(dsa\n\n\n ");
59+
const lineIndex = new server.LineIndex();
60+
lineIndex.load(lines);
61+
const snapshot = lineIndex.edit(0, 39);
62+
assert.equal(snapshot.getText(0, snapshot.getLength()), "");
63+
// line must always be >=1, otherwise the failIfInvalidLocation(location) assertion in ScriptInfo.positionToLineOffset will fail
64+
assert.deepEqual(snapshot.positionToLineOffset(0), { line: 1, offset: 1 });
65+
});
66+
5567
it(`change 9 1 0 1 {"y"}`, () => {
5668
validateEditAtLineCharIndex(9, 1, 0, "y");
5769
});

0 commit comments

Comments
 (0)