fix(13503): fix crash on calling getTypeAtLocation with the SourceFile nodes (#39994)

This commit is contained in:
Alexander T 2020-08-12 10:11:25 +03:00 committed by GitHub
parent d371ae770d
commit 1f5caf554c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -36620,6 +36620,10 @@ namespace ts {
}
function getTypeOfNode(node: Node): Type {
if (isSourceFile(node) && !isExternalModule(node)) {
return errorType;
}
if (node.flags & NodeFlags.InWithStatement) {
// We cannot answer semantic questions within a with block, do not proceed any further
return errorType;

View File

@ -104,4 +104,23 @@ describe("unittests:: Public APIs:: getTypeAtLocation", () => {
assert.ok(!(type.flags & ts.TypeFlags.Any));
assert.equal(type, checker.getTypeAtLocation(propertyAccess.name));
});
it("works on SourceFile", () => {
const content = `const foo = 1;`;
const host = new fakes.CompilerHost(vfs.createFromFileSystem(
Harness.IO,
/*ignoreCase*/ true,
{ documents: [new documents.TextDocument("/file.ts", content)], cwd: "/" }));
const program = ts.createProgram({
host,
rootNames: ["/file.ts"],
options: { noLib: true }
});
const checker = program.getTypeChecker();
const file = program.getSourceFile("/file.ts")!;
const type = checker.getTypeAtLocation(file);
assert.equal(type.flags, ts.TypeFlags.Any);
});
});