@@ -65,37 +65,60 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
65
65
}
66
66
67
67
// 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
69
69
const declarationMatches = Array . from ( fileContent . matchAll ( declarationRegex ) )
70
70
for ( const [ , comment , declaration , declType , name ] of declarationMatches ) {
71
71
if ( ! processedDeclarations . has ( name ) ) {
72
72
if ( comment )
73
73
declarations += `${ comment . trim ( ) } \n`
74
74
75
75
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 * ( \{ [ ^ } ] + \} ) / )
77
77
if ( constMatch ) {
78
- const [ , constName , constType , constValue ] = constMatch
78
+ const [ , constName , , , constValue ] = constMatch
79
79
// Parse the object literal
80
80
const parsedValue = parseObjectLiteral ( constValue . slice ( 1 , - 1 ) )
81
81
const formattedValue = Object . entries ( parsedValue )
82
82
. map ( ( [ key , value ] ) => ` ${ key } : ${ value . includes ( '/' ) || value . includes ( '\'' ) ? value : `'${ value } '` } ` )
83
83
. join ( '\n' )
84
+
84
85
declarations += `export declare const ${ constName } : {\n${ formattedValue } \n}\n\n`
85
86
}
86
87
else {
87
88
// Fallback to the original declaration if parsing fails
88
89
declarations += `export declare ${ declaration . replace ( / e x p o r t \s + / , '' ) . trim ( ) } \n\n`
89
90
}
90
91
}
92
+ else if ( declType === 'interface' || declType === 'type' ) {
93
+ declarations += `${ declaration . trim ( ) } \n\n`
94
+ }
91
95
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`
96
102
}
97
103
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
+ }
99
122
}
100
123
}
101
124
else {
0 commit comments