@@ -24,22 +24,33 @@ export { transformer };
2424
2525type CompatibleStatement = ts . InterfaceDeclaration | ts . FunctionDeclaration | ts . ClassDeclaration | ts . ModuleDeclaration ;
2626
27- function visitNode ( node : ts . CallExpression , declaration : ts . FunctionDeclaration ) : ts . Node {
28- const typeQueryNode : ts . TypeNode = node . typeArguments [ 0 ] ;
27+ function visitNode ( node : ts . CallExpression & { typeArguments : ts . NodeArray < ts . TypeNode > } , declaration : ts . FunctionDeclaration ) : ts . Node {
28+ const [ nodeToMock ] : ts . NodeArray < ts . TypeNode > = node . typeArguments ;
2929
30- if ( isCreateDefinitelyTypedMock ( declaration ) && ts . isTypeQueryNode ( typeQueryNode ) ) {
30+ if ( isCreateDefinitelyTypedMock ( declaration ) && ts . isTypeQueryNode ( nodeToMock ) ) {
3131 const typeChecker : ts . TypeChecker = TypeChecker ( ) ;
32- const typeQuerySymbol : ts . Symbol = typeChecker . getSymbolAtLocation ( typeQueryNode . exprName ) ;
32+ const typeQuerySymbol : ts . Symbol | undefined = typeChecker . getSymbolAtLocation ( nodeToMock . exprName ) ;
33+
34+ if ( ! typeQuerySymbol ) {
35+ return getMock ( nodeToMock , node ) ;
36+ }
37+
3338 const typeQuerySymbolDeclaration : ts . ImportEqualsDeclaration = typeQuerySymbol . declarations [ 0 ] as ts . ImportEqualsDeclaration ;
34- const symbolAlias : ts . Symbol = typeChecker . getSymbolAtLocation ( typeQuerySymbolDeclaration . name ) ;
39+ const symbolAlias : ts . Symbol | undefined = typeChecker . getSymbolAtLocation ( typeQuerySymbolDeclaration . name ) ;
40+
41+ if ( ! symbolAlias ) {
42+ return getMock ( nodeToMock , node ) ;
43+ }
44+
3545 const symbol : ts . Symbol = typeChecker . getAliasedSymbol ( symbolAlias ) ;
3646
47+
3748 if ( ! symbol . declarations ) {
3849 const moduleName : string =
3950 ( ( typeQuerySymbolDeclaration . moduleReference as ts . ExternalModuleReference ) . expression as ts . StringLiteral ) . text ;
4051 const pathModule = path . resolve ( moduleName ) ;
4152 const moduleWithoutExportsFile : ts . SourceFile = GetProgram ( ) . getSourceFiles ( ) . find ( ( file : ts . SourceFile ) =>
42- file . fileName . includes ( `${ pathModule } /index.d.ts` ) ) ;
53+ file . fileName . includes ( `${ pathModule } /index.d.ts` ) ) as ts . SourceFile ;
4354
4455 const compatibleStatements : ts . Statement [ ] = moduleWithoutExportsFile . statements . filter (
4556 ( statement : ts . Statement ) => statement . kind === ts . SyntaxKind . InterfaceDeclaration
@@ -72,12 +83,12 @@ function visitNode(node: ts.CallExpression, declaration: ts.FunctionDeclaration)
7283 return node ;
7384 }
7485
75- return getMock ( typeQueryNode , node ) ;
86+ return getMock ( nodeToMock , node ) ;
7687 }
7788
7889 return node ;
7990}
8091
8192function isCreateDefinitelyTypedMock ( declaration : ts . FunctionDeclaration ) : boolean {
82- return declaration . name && declaration . name . getText ( ) === 'createDefinitelyTypedMock' ;
93+ return ! ! declaration . name && declaration . name . getText ( ) === 'createDefinitelyTypedMock' ;
8394}
0 commit comments