Skip to content

Commit 313a4f5

Browse files
committed
fix: Handle undefined symbols in query types
Closes #1660
1 parent 0df9e6c commit 313a4f5

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/lib/converter/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,18 @@ const typeLiteralConverter: TypeConverter<ts.TypeLiteralNode> = {
565565
const queryConverter: TypeConverter<ts.TypeQueryNode> = {
566566
kind: [ts.SyntaxKind.TypeQuery],
567567
convert(context, node) {
568-
const querySymbol = context.expectSymbolAtLocation(node.exprName);
568+
const querySymbol = context.getSymbolAtLocation(node.exprName);
569+
if (!querySymbol) {
570+
// This can happen if someone uses `typeof` on some property
571+
// on a variable typed as `any` with a name that doesn't exist.
572+
return new QueryType(
573+
ReferenceType.createBrokenReference(
574+
node.exprName.getText(),
575+
context.project
576+
)
577+
);
578+
}
579+
569580
return new QueryType(
570581
new ReferenceType(
571582
node.exprName.getText(),

src/test/converter2.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { deepStrictEqual as equal, ok } from "assert";
66
import {
77
DeclarationReflection,
88
ProjectReflection,
9+
QueryType,
910
ReflectionKind,
1011
SignatureReflection,
1112
} from "../lib/models";
@@ -230,6 +231,12 @@ const issueTests: Record<string, (project: ProjectReflection) => void> = {
230231
equal(ctor.sources?.[0]?.line, 2);
231232
equal(ctor.sources?.[0]?.character, 4);
232233
},
234+
235+
gh1660(project) {
236+
const alias = query(project, "SomeType");
237+
ok(alias.type instanceof QueryType);
238+
equal(alias.type.queryType.name, "m.SomeClass.someProp");
239+
},
233240
};
234241

235242
describe("Converter2", () => {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare const m: {
2+
SomeClass: any;
3+
};
4+
5+
export type SomeType = typeof m.SomeClass.someProp;

0 commit comments

Comments
 (0)