@@ -42,35 +42,48 @@ function GetPropertiesFromSourceFileOrModuleDeclarationDescriptor(sourceFile: Ex
4242 return GetMockPropertiesFromDeclarations ( GetPropertiesFromSourceFileOrModuleDeclaration ( symbol , scope ) , [ ] , scope ) ;
4343}
4444
45+ interface ModuleExportsDeclarations {
46+ declaration : ts . Declaration ;
47+ originalDeclaration : ts . NamedDeclaration ;
48+ }
49+
4550export function GetPropertiesFromSourceFileOrModuleDeclaration ( symbol : ts . Symbol , scope : Scope ) : ts . PropertySignature [ ] {
4651 const typeChecker : ts . TypeChecker = TypeChecker ( ) ;
4752 const moduleExports : ts . Symbol [ ] = typeChecker . getExportsOfModule ( symbol ) ;
4853
49- return moduleExports . map ( ( prop : ts . Symbol ) : ts . PropertySignature => {
54+ return moduleExports . map ( ( prop : ts . Symbol ) : ModuleExportsDeclarations => {
5055 const originalSymbol : ts . Symbol = TypescriptHelper . GetAliasedSymbolSafe ( prop ) ;
51- const originalDeclaration : ts . NamedDeclaration = originalSymbol . declarations [ 0 ] ;
52- const declaration : ts . Declaration = prop . declarations [ 0 ] ;
53-
54- if ( ts . isExportAssignment ( declaration ) ) {
55- return TypescriptCreator . createPropertySignature ( 'default' , ts . createTypeQueryNode ( originalDeclaration . name as ts . Identifier ) ) ;
56- }
56+ const originalDeclaration : ts . NamedDeclaration = originalSymbol ?. declarations ?. [ 0 ] ;
57+ const declaration : ts . Declaration = prop ?. declarations ?. [ 0 ] ;
58+
59+ return {
60+ declaration,
61+ originalDeclaration,
62+ } ;
63+ } ) . filter (
64+ ( d : ModuleExportsDeclarations ) => ! ! d . originalDeclaration && d . declaration
65+ ) . map (
66+ ( d : ModuleExportsDeclarations ) : ts . PropertySignature => {
67+ if ( ts . isExportAssignment ( d . declaration ) ) {
68+ return TypescriptCreator . createPropertySignature ( 'default' , ts . createTypeQueryNode ( d . originalDeclaration . name as ts . Identifier ) ) ;
69+ }
5770
58- if ( ts . isExportSpecifier ( declaration ) && ts . isSourceFile ( originalDeclaration ) ) {
59- const exportSpecifierSymbol : ts . Symbol | undefined = typeChecker . getSymbolAtLocation ( declaration . name ) ;
71+ if ( ts . isExportSpecifier ( d . declaration ) && ts . isSourceFile ( d . originalDeclaration ) ) {
72+ const exportSpecifierSymbol : ts . Symbol | undefined = typeChecker . getSymbolAtLocation ( d . declaration . name ) ;
6073
61- if ( ! exportSpecifierSymbol ) {
62- throw new Error (
63- `The type checker failed to look up symbol for \`${ declaration . name . getText ( ) } '.` ,
64- ) ;
65- }
74+ if ( ! exportSpecifierSymbol ) {
75+ throw new Error (
76+ `The type checker failed to look up symbol for \`${ d . declaration . name . getText ( ) } '.` ,
77+ ) ;
78+ }
6679
67- const exportSpecifierAliasSymbol : ts . Symbol = typeChecker . getAliasedSymbol ( exportSpecifierSymbol ) ;
68- const exportSpecifierProperties : ts . PropertySignature [ ] = GetPropertiesFromSourceFileOrModuleDeclaration ( exportSpecifierAliasSymbol , scope ) ;
69- const propertyType : ts . TypeNode = ts . createTypeLiteralNode ( exportSpecifierProperties ) ;
80+ const exportSpecifierAliasSymbol : ts . Symbol = typeChecker . getAliasedSymbol ( exportSpecifierSymbol ) ;
81+ const exportSpecifierProperties : ts . PropertySignature [ ] = GetPropertiesFromSourceFileOrModuleDeclaration ( exportSpecifierAliasSymbol , scope ) ;
82+ const propertyType : ts . TypeNode = ts . createTypeLiteralNode ( exportSpecifierProperties ) ;
7083
71- return TypescriptCreator . createPropertySignature ( declaration . name , propertyType ) ;
72- }
84+ return TypescriptCreator . createPropertySignature ( d . declaration . name , propertyType ) ;
85+ }
7386
74- return TypescriptCreator . createPropertySignature ( originalDeclaration . name as ts . Identifier , ts . createTypeQueryNode ( originalDeclaration . name as ts . Identifier ) ) ;
75- } ) ;
87+ return TypescriptCreator . createPropertySignature ( d . originalDeclaration . name as ts . Identifier , ts . createTypeQueryNode ( d . originalDeclaration . name as ts . Identifier ) ) ;
88+ } ) ;
7689}
0 commit comments