From 1f5caf554c4c468767097db36cea52e1911e2ce2 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Wed, 12 Aug 2020 10:11:25 +0300 Subject: [PATCH] fix(13503): fix crash on calling getTypeAtLocation with the SourceFile nodes (#39994) --- src/compiler/checker.ts | 4 ++++ src/testRunner/unittests/publicApi.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 003bc879639..0a9740a73e4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; diff --git a/src/testRunner/unittests/publicApi.ts b/src/testRunner/unittests/publicApi.ts index cbb7e1b536a..79d4b9a2082 100644 --- a/src/testRunner/unittests/publicApi.ts +++ b/src/testRunner/unittests/publicApi.ts @@ -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); + }); });