Skip to content

Commit

Permalink
Add tests for completions crash (#53472)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch authored Mar 23, 2023
1 parent 9bd1a32 commit 3a3146e
Show file tree
Hide file tree
Showing 3 changed files with 650 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/testRunner/unittests/tsserver/exportMapCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
File,
} from "../virtualFileSystemWithWatch";
import {
baselineTsserverLogs,
configuredProjectAt,
createLoggerWithInMemoryLogs,
createSession,
openFilesForSession,
} from "./helpers";
Expand Down Expand Up @@ -129,6 +131,94 @@ describe("unittests:: tsserver:: exportMapCache", () => {
assert.ok(sigintPropAfter);
assert.notEqual(symbolIdBefore, ts.getSymbolId(sigintPropAfter![0].symbol));
});

it("invalidates the cache when a file is opened with different contents", () => {
const utilsTs: File = {
path: "/utils.ts",
content: `export class Element {
// ...
}
export abstract class Component {
abstract render(): Element;
}`
};
const classesTs: File = {
path: "/classes.ts",
content: `import { Component } from "./utils.js";
export class MyComponent extends Component {
render/**/
}`
};
const host = createServerHost([utilsTs, classesTs, tsconfig]);
const session = createSession(host, { canUseEvents: true, logger: createLoggerWithInMemoryLogs(host) });
const projectService = session.getProjectService();
openFilesForSession([classesTs], session);
session.executeCommandSeq<ts.server.protocol.ConfigureRequest>({
command: ts.server.protocol.CommandTypes.Configure,
arguments: {
preferences: {
includeCompletionsForModuleExports: true,
includeCompletionsWithClassMemberSnippets: true,
includeCompletionsWithInsertText: true,
},
}
});
session.executeCommandSeq<ts.server.protocol.CompletionsRequest>({
command: ts.server.protocol.CommandTypes.CompletionInfo,
arguments: {
file: classesTs.path,
line: 4,
offset: 23,
prefix: "render",
includeExternalModuleExports: true,
includeInsertTextCompletions: true,
}
});

const project = configuredProjectAt(projectService, 0);
const exportMapCache = project.getCachedExportInfoMap();
assert.ok(exportMapCache.isUsableByFile(classesTs.path as ts.Path));
assert.ok(!exportMapCache.isEmpty());

openFilesForSession([{ file: utilsTs.path, content: utilsTs.content.replace("render", "render2") }], session);
session.executeCommandSeq<ts.server.protocol.UpdateOpenRequest>({
command: ts.server.protocol.CommandTypes.UpdateOpen,
arguments: {
changedFiles: [{
fileName: classesTs.path,
textChanges: [{
newText: "",
start: { line: 4, offset: 22 },
end: { line: 4, offset: 23 },
}]
}]
}
});

host.runQueuedTimeoutCallbacks();
project.getPackageJsonAutoImportProvider();

// Cache is invalidated because other file has changed
assert.ok(!exportMapCache.isUsableByFile(classesTs.path as ts.Path));
assert.ok(exportMapCache.isEmpty());

// Does not crash
session.executeCommandSeq<ts.server.protocol.CompletionsRequest>({
command: ts.server.protocol.CommandTypes.CompletionInfo,
arguments: {
file: classesTs.path,
line: 4,
offset: 22,
prefix: "rende",
includeExternalModuleExports: true,
includeInsertTextCompletions: true,
}
});

baselineTsserverLogs("exportMapCache", "invalidates the cache when a file is opened with different contents", session);
});
});

function setup() {
Expand Down
Loading

0 comments on commit 3a3146e

Please sign in to comment.