Skip to content

Commit a863e00

Browse files
committed
fix(13503): fix crash on calling getTypeAtLocation with the SourceFile nodes
1 parent 668bbc6 commit a863e00

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36582,6 +36582,10 @@ namespace ts {
3658236582
}
3658336583

3658436584
function getTypeOfNode(node: Node): Type {
36585+
if (isSourceFile(node) && !isExternalModule(node)) {
36586+
return errorType;
36587+
}
36588+
3658536589
if (node.flags & NodeFlags.InWithStatement) {
3658636590
// We cannot answer semantic questions within a with block, do not proceed any further
3658736591
return errorType;

src/testRunner/unittests/publicApi.ts

Lines changed: 19 additions & 0 deletions
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)