@@ -10,72 +10,101 @@ import { Scope } from '../../../../src/transformer/scope/scope';
1010import { core } from '../../../../src/transformer/core/core' ;
1111import { DefinitelyTypedTransformerLogger } from '../logger' ;
1212
13- type CompatibleStatement = ts . InterfaceDeclaration | ts . FunctionDeclaration | ts . ClassDeclaration | ts . ModuleDeclaration ;
13+ type CompatibleStatement =
14+ | ts . InterfaceDeclaration
15+ | ts . FunctionDeclaration
16+ | ts . ClassDeclaration
17+ | ts . ModuleDeclaration ;
1418
15- export const createDefinitelyTypedMockCustomFunction : CustomFunction = customFunctionWithTypeArgument (
16- 'create-definitely-typed-mock.d.ts' ,
17- 'createDefinitelyTypedMock' ,
18- ( node : ts . CallExpression , nodeToMock : ts . TypeNode ) : ts . Node => {
19- if ( core . ts . isTypeQueryNode ( nodeToMock ) ) {
20- SetCurrentCreateMock ( node ) ;
21- const typeChecker : ts . TypeChecker = core . typeChecker ;
22- const typeQuerySymbol : ts . Symbol | undefined = typeChecker . getSymbolAtLocation ( nodeToMock . exprName ) ;
19+ export const createDefinitelyTypedMockCustomFunction : CustomFunction =
20+ customFunctionWithTypeArgument (
21+ 'create-definitely-typed-mock.d.ts' ,
22+ 'createDefinitelyTypedMock' ,
23+ ( node : ts . CallExpression , nodeToMock : ts . TypeNode ) : ts . Node => {
24+ if ( core . ts . isTypeQueryNode ( nodeToMock ) ) {
25+ SetCurrentCreateMock ( node ) ;
26+ const typeChecker : ts . TypeChecker = core . typeChecker ;
27+ const typeQuerySymbol : ts . Symbol | undefined =
28+ typeChecker . getSymbolAtLocation ( nodeToMock . exprName ) ;
2329
24- if ( ! typeQuerySymbol ) {
25- return getMock ( node , { nodeToMock } ) ;
26- }
30+ if ( ! typeQuerySymbol ) {
31+ return getMock ( node , { nodeToMock } ) ;
32+ }
2733
28- const typeQuerySymbolDeclaration : ts . ImportEqualsDeclaration = typeQuerySymbol . declarations [ 0 ] as ts . ImportEqualsDeclaration ;
29- const symbolAlias : ts . Symbol | undefined = typeChecker . getSymbolAtLocation ( typeQuerySymbolDeclaration . name ) ;
34+ const typeQuerySymbolDeclaration : ts . ImportEqualsDeclaration =
35+ typeQuerySymbol . declarations ?. [ 0 ] as ts . ImportEqualsDeclaration ;
36+ const symbolAlias : ts . Symbol | undefined =
37+ typeChecker . getSymbolAtLocation ( typeQuerySymbolDeclaration . name ) ;
3038
31- if ( ! symbolAlias ) {
32- return getMock ( node , { nodeToMock } ) ;
33- }
39+ if ( ! symbolAlias ) {
40+ return getMock ( node , { nodeToMock } ) ;
41+ }
3442
35- const symbol : ts . Symbol = typeChecker . getAliasedSymbol ( symbolAlias ) ;
43+ const symbol : ts . Symbol = typeChecker . getAliasedSymbol ( symbolAlias ) ;
3644
37- if ( ! symbol . declarations ) {
38- const moduleName : string =
39- ( ( typeQuerySymbolDeclaration . moduleReference as ts . ExternalModuleReference ) . expression as ts . StringLiteral ) . text ;
40- const pathModule : string = path . resolve ( moduleName ) ;
41- const moduleWithoutExportsFile : ts . SourceFile = core . program . getSourceFiles ( ) . find ( ( file : ts . SourceFile ) =>
42- path . relative ( file . fileName , path . join ( pathModule , 'index.d.ts' ) ) === ''
43- ) as ts . SourceFile ;
45+ if ( ! symbol . declarations ) {
46+ const moduleName : string = (
47+ (
48+ typeQuerySymbolDeclaration . moduleReference as ts . ExternalModuleReference
49+ ) . expression as ts . StringLiteral
50+ ) . text ;
51+ const pathModule : string = path . resolve ( moduleName ) ;
52+ const moduleWithoutExportsFile : ts . SourceFile = core . program
53+ . getSourceFiles ( )
54+ . find (
55+ ( file : ts . SourceFile ) =>
56+ path . relative (
57+ file . fileName ,
58+ path . join ( pathModule , 'index.d.ts' )
59+ ) === ''
60+ ) as ts . SourceFile ;
4461
45- const compatibleStatements : ts . Statement [ ] = moduleWithoutExportsFile . statements . filter (
46- ( statement : ts . Statement ) => statement . kind === core . ts . SyntaxKind . InterfaceDeclaration
47- || statement . kind === core . ts . SyntaxKind . FunctionDeclaration
48- || statement . kind === core . ts . SyntaxKind . ClassDeclaration
49- || statement . kind === core . ts . SyntaxKind . ModuleDeclaration
50- ) ;
62+ const compatibleStatements : ts . Statement [ ] =
63+ moduleWithoutExportsFile . statements . filter (
64+ ( statement : ts . Statement ) =>
65+ statement . kind === core . ts . SyntaxKind . InterfaceDeclaration ||
66+ statement . kind === core . ts . SyntaxKind . FunctionDeclaration ||
67+ statement . kind === core . ts . SyntaxKind . ClassDeclaration ||
68+ statement . kind === core . ts . SyntaxKind . ModuleDeclaration
69+ ) ;
5170
52- if ( compatibleStatements . length > 0 ) {
53- return core . ts . createArrayLiteral ( compatibleStatements . map (
54- ( workingStatement : CompatibleStatement ) => {
55- const name : ts . Identifier = workingStatement . name as ts . Identifier ;
56- const scope = new Scope ( ) ;
71+ if ( compatibleStatements . length > 0 ) {
72+ return core . ts . createArrayLiteral (
73+ compatibleStatements . map (
74+ ( workingStatement : CompatibleStatement ) => {
75+ const name : ts . Identifier =
76+ workingStatement . name as ts . Identifier ;
77+ const scope = new Scope ( ) ;
5778
58- if ( core . ts . isModuleDeclaration ( workingStatement ) ) {
59- return GetMockPropertiesFromDeclarations (
60- GetPropertiesFromSourceFileOrModuleDeclaration ( ( workingStatement as any ) . symbol , scope ) ,
61- [ ] ,
62- scope
63- ) ;
64- }
79+ if ( core . ts . isModuleDeclaration ( workingStatement ) ) {
80+ return GetMockPropertiesFromDeclarations (
81+ GetPropertiesFromSourceFileOrModuleDeclaration (
82+ ( workingStatement as any ) . symbol ,
83+ scope
84+ ) ,
85+ [ ] ,
86+ scope
87+ ) ;
88+ }
6589
66- const nodeToMock : ts . TypeReferenceNode = core . ts . createTypeReferenceNode ( name , undefined ) ;
67- return getMock ( node , { nodeToMock } ) ;
90+ const nodeToMock : ts . TypeReferenceNode =
91+ core . ts . createTypeReferenceNode ( name , undefined ) ;
92+ return getMock ( node , { nodeToMock } ) ;
93+ } ,
94+ [ ]
95+ )
96+ ) ;
97+ }
98+ DefinitelyTypedTransformerLogger ( ) . moduleWithoutValidTypeStatements (
99+ moduleName
100+ ) ;
68101
69- } , [ ] ) ) ;
102+ return node ;
70103 }
71- DefinitelyTypedTransformerLogger ( ) . moduleWithoutValidTypeStatements ( moduleName ) ;
72104
73- return node ;
105+ return getMock ( node , { nodeToMock } ) ;
74106 }
75107
76- return getMock ( node , { nodeToMock } ) ;
108+ return node ;
77109 }
78-
79- return node ;
80- }
81- ) ;
110+ ) ;
0 commit comments