@@ -65,37 +65,60 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
6565 }
6666
6767 // Handle all declarations
68- const declarationRegex = / ( \/ \* \* [ \s \S ] * ?\* \/ \s * ) ? ( e x p o r t \s + ( c o n s t | i n t e r f a c e | t y p e | f u n c t i o n | a s y n c f u n c t i o n ) \s + ( \w + ) [ \s \S ] * ?(?: ; | \} ) \s * ) / g
68+ const declarationRegex = / ( \/ \* \* [ \s \S ] * ?\* \/ \s * ) ? ( e x p o r t \s + ( c o n s t | i n t e r f a c e | t y p e | f u n c t i o n | a s y n c f u n c t i o n ) \s + ( \w + ) [ \s \S ] * ?(? = e x p o r t \s | $ ) ) / g
6969 const declarationMatches = Array . from ( fileContent . matchAll ( declarationRegex ) )
7070 for ( const [ , comment , declaration , declType , name ] of declarationMatches ) {
7171 if ( ! processedDeclarations . has ( name ) ) {
7272 if ( comment )
7373 declarations += `${ comment . trim ( ) } \n`
7474
7575 if ( declType === 'const' ) {
76- const constMatch = declaration . match ( / e x p o r t \s + c o n s t \s + ( \w + ) \s * (?: : \s * ( [ ^ = ] + ) \s * ) ? = \s * ( \{ [ ^ } ] + \} ) / )
76+ const constMatch = declaration . match ( / e x p o r t \s + c o n s t \s + ( \w + ) ( \s * : \s * ( [ ^ = ] + ) ) ? \s * = \s * ( \{ [ ^ } ] + \} ) / )
7777 if ( constMatch ) {
78- const [ , constName , constType , constValue ] = constMatch
78+ const [ , constName , , , constValue ] = constMatch
7979 // Parse the object literal
8080 const parsedValue = parseObjectLiteral ( constValue . slice ( 1 , - 1 ) )
8181 const formattedValue = Object . entries ( parsedValue )
8282 . map ( ( [ key , value ] ) => ` ${ key } : ${ value . includes ( '/' ) || value . includes ( '\'' ) ? value : `'${ value } '` } ` )
8383 . join ( '\n' )
84+
8485 declarations += `export declare const ${ constName } : {\n${ formattedValue } \n}\n\n`
8586 }
8687 else {
8788 // Fallback to the original declaration if parsing fails
8889 declarations += `export declare ${ declaration . replace ( / e x p o r t \s + / , '' ) . trim ( ) } \n\n`
8990 }
9091 }
92+ else if ( declType === 'interface' || declType === 'type' ) {
93+ declarations += `${ declaration . trim ( ) } \n\n`
94+ }
9195 else if ( declType === 'function' || declType === 'async function' ) {
92- const funcMatch = declaration . match ( / e x p o r t \s + ( a s y n c \s + ) ? f u n c t i o n \s + ( \w + ) \s * \( ( [ ^ ) ] * ) \) \s * : \s * ( [ ^ { ] + ) / )
93- if ( funcMatch ) {
94- const [ , isAsync , funcName , params , returnType ] = funcMatch
95- declarations += `export declare ${ isAsync || '' } function ${ funcName } (${ params } ): ${ returnType . trim ( ) } \n\n`
96+ const funcSignatureRegex = / e x p o r t \s + ( a s y n c \s + ) ? f u n c t i o n \s + ( \w + ) \s * \( ( [ ^ ) ] * ) \) \s * : \s * ( [ ^ { ] + ) /
97+ const funcSignatureMatch = declaration . match ( funcSignatureRegex )
98+
99+ if ( funcSignatureMatch ) {
100+ const [ , isAsync , funcName , params , returnType ] = funcSignatureMatch
101+ declarations += `export declare ${ isAsync || '' } function ${ funcName } (${ params . trim ( ) } ): ${ returnType . trim ( ) } \n\n`
96102 }
97103 else {
98- declarations += `export declare ${ declaration . replace ( / e x p o r t \s + / , '' ) . trim ( ) } \n\n`
104+ // If we can't match the full signature, let's try to extract what we can
105+ const funcNameParamsRegex = / e x p o r t \s + ( a s y n c \s + ) ? f u n c t i o n \s + ( \w + ) \s * \( ( [ ^ ) ] * ) \) /
106+ const funcNameParamsMatch = declaration . match ( funcNameParamsRegex )
107+
108+ if ( funcNameParamsMatch ) {
109+ const [ , isAsync , funcName , params ] = funcNameParamsMatch
110+ // Try to find the return type
111+ const returnTypeRegex = / \) \s * : \s * ( [ ^ { ] + ) /
112+ const returnTypeMatch = declaration . match ( returnTypeRegex )
113+ const returnType = returnTypeMatch ? returnTypeMatch [ 1 ] . trim ( ) : 'any'
114+
115+ declarations += `export declare ${ isAsync || '' } function ${ funcName } (${ params . trim ( ) } ): ${ returnType } \n\n`
116+ }
117+ else {
118+ // If all else fails, just add 'declare' to the original export
119+ const simplifiedDeclaration = declaration . replace ( / e x p o r t \s + / , '' ) . split ( '{' ) [ 0 ] . trim ( )
120+ declarations += `export declare ${ simplifiedDeclaration } \n\n`
121+ }
99122 }
100123 }
101124 else {
0 commit comments