From 313a4f5449869fa4319f3253c1a7f31a4ea4d2f7 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 18 Aug 2021 20:02:37 -0600 Subject: [PATCH] fix: Handle undefined symbols in query types Closes #1660 --- src/lib/converter/types.ts | 13 ++++++++++++- src/test/converter2.test.ts | 7 +++++++ src/test/converter2/issues/gh1660.ts | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/test/converter2/issues/gh1660.ts diff --git a/src/lib/converter/types.ts b/src/lib/converter/types.ts index e57b13926..772cc6d8a 100644 --- a/src/lib/converter/types.ts +++ b/src/lib/converter/types.ts @@ -565,7 +565,18 @@ const typeLiteralConverter: TypeConverter = { const queryConverter: TypeConverter = { kind: [ts.SyntaxKind.TypeQuery], convert(context, node) { - const querySymbol = context.expectSymbolAtLocation(node.exprName); + const querySymbol = context.getSymbolAtLocation(node.exprName); + if (!querySymbol) { + // This can happen if someone uses `typeof` on some property + // on a variable typed as `any` with a name that doesn't exist. + return new QueryType( + ReferenceType.createBrokenReference( + node.exprName.getText(), + context.project + ) + ); + } + return new QueryType( new ReferenceType( node.exprName.getText(), diff --git a/src/test/converter2.test.ts b/src/test/converter2.test.ts index 17608a2a9..81101be0d 100644 --- a/src/test/converter2.test.ts +++ b/src/test/converter2.test.ts @@ -6,6 +6,7 @@ import { deepStrictEqual as equal, ok } from "assert"; import { DeclarationReflection, ProjectReflection, + QueryType, ReflectionKind, SignatureReflection, } from "../lib/models"; @@ -230,6 +231,12 @@ const issueTests: Record void> = { equal(ctor.sources?.[0]?.line, 2); equal(ctor.sources?.[0]?.character, 4); }, + + gh1660(project) { + const alias = query(project, "SomeType"); + ok(alias.type instanceof QueryType); + equal(alias.type.queryType.name, "m.SomeClass.someProp"); + }, }; describe("Converter2", () => { diff --git a/src/test/converter2/issues/gh1660.ts b/src/test/converter2/issues/gh1660.ts new file mode 100644 index 000000000..ef572ae95 --- /dev/null +++ b/src/test/converter2/issues/gh1660.ts @@ -0,0 +1,5 @@ +declare const m: { + SomeClass: any; +}; + +export type SomeType = typeof m.SomeClass.someProp;