@@ -875,7 +875,7 @@ export function processFunctionDeclaration(
875875 generics,
876876 } )
877877
878- // Track used types
878+ // Track used types from generics
879879 if ( generics ) {
880880 const genericTypes = generics . slice ( 1 , - 1 ) . split ( ',' ) . map ( t => t . trim ( ) )
881881 genericTypes . forEach ( ( type ) => {
@@ -893,41 +893,46 @@ export function processFunctionDeclaration(
893893 } )
894894 }
895895
896- // Track return type usage
897896 if ( returnType && returnType !== 'void' ) {
898897 const typeMatches = returnType . match ( / ( [ A - Z _ ] \w * ) / gi)
899898 if ( typeMatches ) {
900899 typeMatches . forEach ( t => usedTypes . add ( t ) )
901900 }
902901 }
903902
904- // Ensure proper generic closing before params
905- const genericPart = generics ? ` ${ generics . endsWith ( '>' ) ? generics : ` ${ generics } >` } ` : ''
903+ // Build the declaration
904+ let result = ''
906905
907- // Build the declaration ensuring proper spacing and bracket placement
908- const parts = [
909- isExported ? 'export ' : '' ,
910- 'declare ' ,
911- isAsync ? 'async ' : '' ,
912- 'function ' ,
913- name ,
914- genericPart ,
915- '(' ,
916- params ,
917- '): ' ,
918- returnType ,
919- ';' ,
920- ]
921-
922- // Join with proper spacing and clean up
923- return parts . join ( '' )
924- . replace ( / \s + / g, ' ' ) // Normalize spaces
925- . replace ( / \s * ( [ < > ( ) , ; ] ) \s * / g, '$1' ) // Clean up around punctuation
926- . replace ( / , ( [ ^ , \s ] ) / g, ', $1' ) // Ensure space after commas
927- . replace ( / > \s * \( / g, '>(' ) // No space between generic close and params
928- . replace ( / \( \s * \) / g, '()' ) // Clean empty params
929- . replace ( / f u n c t i o n \s + f u n c t i o n / , 'function' ) // Remove duplicate function
930- . replace ( / ( [ ^ < ] ) \s + > / g, '$1>' ) // Clean space before closing generic
906+ // Add export if needed
907+ if ( isExported )
908+ result += 'export '
909+
910+ // Add declare and async
911+ result += 'declare '
912+ if ( isAsync )
913+ result += 'async '
914+
915+ // Add function name
916+ result += `function ${ name } `
917+
918+ // Add generic type with proper closing
919+ if ( generics ) {
920+ let genericPart = generics
921+ if ( ! genericPart . endsWith ( '>' ) )
922+ genericPart += '>'
923+ result += genericPart
924+ }
925+
926+ // Add parameters and return type
927+ result += `(${ params } ): ${ returnType } ;`
928+
929+ // Clean up the result
930+ return result
931+ . replace ( / \s + / g, ' ' )
932+ . replace ( / \s * ( [ < > ( ) , ; ] ) \s * / g, '$1' )
933+ . replace ( / , ( [ ^ , \s ] ) / g, ', $1' )
934+ . replace ( / f u n c t i o n \s + f u n c t i o n / , 'function' )
935+ . replace ( / > \s + \( / , '>(' )
931936 . trim ( )
932937}
933938
0 commit comments