Skip to content

Commit 1f5caf5

Browse files
authored
fix(13503): fix crash on calling getTypeAtLocation with the SourceFile nodes (microsoft#39994)
1 parent d371ae7 commit 1f5caf5

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/compiler/checker.ts

+4
Original file line numberDiff line numberDiff line change
@@ -36620,6 +36620,10 @@ namespace ts {
3662036620
}
3662136621

3662236622
function getTypeOfNode(node: Node): Type {
36623+
if (isSourceFile(node) && !isExternalModule(node)) {
36624+
return errorType;
36625+
}
36626+
3662336627
if (node.flags & NodeFlags.InWithStatement) {
3662436628
// We cannot answer semantic questions within a with block, do not proceed any further
3662536629
return errorType;

src/testRunner/unittests/publicApi.ts

+19
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,23 @@ describe("unittests:: Public APIs:: getTypeAtLocation", () => {
104104
assert.ok(!(type.flags & ts.TypeFlags.Any));
105105
assert.equal(type, checker.getTypeAtLocation(propertyAccess.name));
106106
});
107+
108+
it("works on SourceFile", () => {
109+
const content = `const foo = 1;`;
110+
const host = new fakes.CompilerHost(vfs.createFromFileSystem(
111+
Harness.IO,
112+
/*ignoreCase*/ true,
113+
{ documents: [new documents.TextDocument("/file.ts", content)], cwd: "/" }));
114+
115+
const program = ts.createProgram({
116+
host,
117+
rootNames: ["/file.ts"],
118+
options: { noLib: true }
119+
});
120+
121+
const checker = program.getTypeChecker();
122+
const file = program.getSourceFile("/file.ts")!;
123+
const type = checker.getTypeAtLocation(file);
124+
assert.equal(type.flags, ts.TypeFlags.Any);
125+
});
107126
});

0 commit comments

Comments
 (0)