Skip to content

Commit

Permalink
fix(typequery): add support for typequery undefined (#227)
Browse files Browse the repository at this point in the history
* fix(typequery): add support for typequery undefined

* fix(typequery): remove unnecessary casting and binary expression because all tests pass
  • Loading branch information
uittorio authored Feb 8, 2020
1 parent 3006c84 commit 0b835b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/transformer/descriptor/typeQuery/typeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ import { GetModuleDescriptor } from '../module/module';
import { GetNullDescriptor } from '../null/null';
import { GetType } from '../type/type';
import { GetTypeReferenceDescriptor } from '../typeReference/typeReference';
import { GetUndefinedDescriptor } from '../undefined/undefined';

export function GetTypeQueryDescriptor(node: ts.TypeQueryNode, scope: Scope): ts.Expression {
const declaration: ts.NamedDeclaration = getTypeQueryDeclaration(node);
const symbol: ts.Symbol = getTypeQuerySymbol(node);

if (!symbol.declarations.length) {
return GetUndefinedDescriptor();
}

const declaration: ts.NamedDeclaration = getTypeQueryDeclarationFromSymbol(symbol);

return GetTypeQueryDescriptorFromDeclaration(declaration, scope);
}

Expand Down Expand Up @@ -68,22 +76,8 @@ export function GetTypeQueryDescriptorFromDeclaration(declaration: ts.NamedDecla
}
}

function getTypeQueryDeclaration(node: ts.TypeQueryNode): ts.NamedDeclaration {
const typeChecker: ts.TypeChecker = TypeChecker();
/*
TODO: Find different workaround without casting to any
Cast to any is been done because getSymbolAtLocation doesn't work when the node is an inferred identifier of a type query of a type query
Use case is:
```
const myVar = MyEnum;
createMock<typeof myVar>();
```
here `typeof myVar` is inferred `typeof MyEnum` and the `MyEnum` identifier doesn't play well with getSymbolAtLocation and it returns undefined.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const symbol: ts.Symbol = typeChecker.getSymbolAtLocation(node.exprName) || (node.exprName as any).symbol;

return getTypeQueryDeclarationFromSymbol(symbol);
function getTypeQuerySymbol(node: ts.TypeQueryNode): ts.Symbol {
return TypeChecker().getSymbolAtLocation(node.exprName);
}

function getTypeQueryDeclarationFromSymbol(symbol: ts.Symbol): ts.NamedDeclaration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createMock } from 'ts-auto-mock';

describe('typeQuery undefined ', () => {
it('should return undefined', () => {
const type: typeof undefined = createMock<typeof undefined>();

expect(type).toBeUndefined();
});
});

0 comments on commit 0b835b1

Please sign in to comment.