Skip to content

Commit

Permalink
fix: Handle undefined symbols in query types
Browse files Browse the repository at this point in the history
Closes #1660
  • Loading branch information
Gerrit0 committed Aug 19, 2021
1 parent 0df9e6c commit 313a4f5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib/converter/types.ts
Expand Up @@ -565,7 +565,18 @@ const typeLiteralConverter: TypeConverter<ts.TypeLiteralNode> = {
const queryConverter: TypeConverter<ts.TypeQueryNode> = {
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(),
Expand Down
7 changes: 7 additions & 0 deletions src/test/converter2.test.ts
Expand Up @@ -6,6 +6,7 @@ import { deepStrictEqual as equal, ok } from "assert";
import {
DeclarationReflection,
ProjectReflection,
QueryType,
ReflectionKind,
SignatureReflection,
} from "../lib/models";
Expand Down Expand Up @@ -230,6 +231,12 @@ const issueTests: Record<string, (project: ProjectReflection) => 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", () => {
Expand Down
5 changes: 5 additions & 0 deletions src/test/converter2/issues/gh1660.ts
@@ -0,0 +1,5 @@
declare const m: {
SomeClass: any;
};

export type SomeType = typeof m.SomeClass.someProp;

0 comments on commit 313a4f5

Please sign in to comment.