@@ -784,68 +784,62 @@ export function processTypeDeclaration(declaration: string, isExported = true):
784784}
785785
786786/**
787- * Extract complete function signature handling multi-line declarations
787+ * Enhanced function signature extraction with better destructuring and generics support
788788 */
789- export function extractFunctionSignature ( declaration : string ) : {
789+ export interface FunctionSignature {
790790 name : string
791791 params : string
792792 returnType : string
793793 isAsync : boolean
794794 generics : string
795- } {
796- console . log ( '\n[Signature Extraction Debug]' )
797- console . log ( 'Input:' , declaration )
795+ }
796+
798797
799- const isAsync = declaration . includes ( 'async function' )
798+ /**
799+ * Extract complete function signature handling multi-line declarations
800+ */
801+ export function extractFunctionSignature ( declaration : string ) : FunctionSignature {
802+ // Remove export keyword and clean up whitespace
800803 const cleanDeclaration = declaration
801- . replace ( ' export ' , '' )
802- . replace ( ' async function ' , 'function ' )
803- . replace ( ' function ' , '' )
804+ . replace ( / ^ e x p o r t \s + / , '' )
805+ . replace ( / ^ a s y n c \s + / , '' )
806+ . replace ( / ^ f u n c t i o n \s + / , '' )
804807 . trim ( )
805808
806- console . log ( 'Cleaned Declaration:' , cleanDeclaration )
809+ // Extract generics first
810+ const genericsMatch = cleanDeclaration . match ( / < ( [ ^ > ] + ) > / )
811+ const generics = genericsMatch ? `<${ genericsMatch [ 1 ] } >` : ''
807812
808- // Extract name and generics
809- const nameAndGenericsMatch = cleanDeclaration . match ( / ^ ( [ ^ ( < \s ] + ) (?: < ( [ ^ > ] + ) > ) ? / )
810- const name = nameAndGenericsMatch ? nameAndGenericsMatch [ 1 ] : ''
811- const generics = nameAndGenericsMatch && nameAndGenericsMatch [ 2 ]
812- ? `<${ nameAndGenericsMatch [ 2 ] } >`
813- : ''
813+ // Remove generics from declaration for easier parsing
814+ const withoutGenerics = cleanDeclaration . replace ( / < ( [ ^ > ] + ) > / , '' )
814815
815- console . log ( 'Name and Generics:' , { name, generics } )
816+ // Extract function name
817+ const nameMatch = withoutGenerics . match ( / ^ ( [ ^ ( < \s ] + ) / )
818+ const name = nameMatch ? nameMatch [ 1 ] : ''
816819
817- // Extract parameters with improved destructuring handling
818- const paramsMatch = cleanDeclaration . match ( / \( ( [ \s \S ] * ?) \) (? = \s * : ) / )
820+ // Extract parameters section
821+ const paramsMatch = withoutGenerics . match ( / \( ( [ \s \S ] * ?) \) (? = \s * : ) / )
819822 let params = paramsMatch ? paramsMatch [ 1 ] . trim ( ) : ''
820823
821- // Transform destructured parameters by extracting only the type
824+ // Handle destructured parameters
822825 if ( params . startsWith ( '{' ) ) {
823- // Match the type after the colon, handling possible generic types
824826 const typeMatch = params . match ( / \} : \s * ( [ ^ ) ] + ) $ / )
825827 if ( typeMatch ) {
826- const paramType = typeMatch [ 1 ] . trim ( )
827- params = `options: ${ paramType } `
828+ params = `options: ${ typeMatch [ 1 ] . trim ( ) } `
828829 }
829830 }
830831
831- console . log ( 'Processed Params:' , params )
832-
833832 // Extract return type
834- const returnTypeMatch = cleanDeclaration . match ( / \) \s * : \s * ( [ \s \S ] + ?) (? = \{ | $ ) / )
833+ const returnTypeMatch = withoutGenerics . match ( / \) \s * : \s * ( [ \s \S ] + ?) (? = \{ | $ ) / )
835834 const returnType = returnTypeMatch ? returnTypeMatch [ 1 ] . trim ( ) : 'void'
836835
837- console . log ( 'Return Type:' , returnType )
838-
839- const result = {
836+ return {
840837 name,
841838 params,
842839 returnType,
843- isAsync,
840+ isAsync : declaration . includes ( 'async' ) ,
844841 generics,
845842 }
846-
847- console . log ( 'Final Extraction Result:' , result )
848- return result
849843}
850844
851845/**
@@ -856,9 +850,6 @@ export function processFunctionDeclaration(
856850 usedTypes : Set < string > ,
857851 isExported = true ,
858852) : string {
859- console . log ( '\n[Function Declaration Debug]' )
860- console . log ( 'Input:' , declaration )
861-
862853 const {
863854 name,
864855 params,
@@ -867,75 +858,49 @@ export function processFunctionDeclaration(
867858 generics,
868859 } = extractFunctionSignature ( declaration )
869860
870- console . log ( 'Parsed Components:' , {
871- name,
872- params,
873- returnType,
874- isAsync,
875- generics,
876- } )
877-
878- // Track used types from generics
861+ // Track generic types
879862 if ( generics ) {
880- const genericTypes = generics . slice ( 1 , - 1 ) . split ( ',' ) . map ( t => t . trim ( ) )
863+ const genericTypes = generics
864+ . slice ( 1 , - 1 )
865+ . split ( ',' )
866+ . map ( t => t . trim ( ) . split ( 'extends' ) [ 0 ] . trim ( ) )
867+
881868 genericTypes . forEach ( ( type ) => {
882- const baseType = type . split ( 'extends' ) [ 0 ] . trim ( )
883- if ( baseType )
884- usedTypes . add ( baseType )
885-
886- const constraintType = type . split ( 'extends' ) [ 1 ] ?. trim ( )
887- if ( constraintType ) {
888- const typeMatches = constraintType . match ( / ( [ A - Z _ ] \w * ) / gi)
889- if ( typeMatches ) {
890- typeMatches . forEach ( t => usedTypes . add ( t ) )
891- }
892- }
869+ if ( type )
870+ usedTypes . add ( type )
893871 } )
894872 }
895873
896- if ( returnType && returnType !== 'void' ) {
897- const typeMatches = returnType . match ( / ( [ A - Z _ ] \w * ) / gi)
898- if ( typeMatches ) {
899- typeMatches . forEach ( t => usedTypes . add ( t ) )
900- }
901- }
902-
903- // Build the declaration
904- let result = ''
905-
906- // Add export if needed
907- if ( isExported )
908- result += 'export '
909-
910- // Add declare and async
911- result += 'declare '
874+ // Build declaration string
875+ let result = `${ isExported ? 'export ' : '' } declare `
912876 if ( isAsync )
913877 result += 'async '
914878
915- // Add function name
916- result += `function ${ name } `
879+ result += `function ${ name } ${ generics } (${ params } ): ${ returnType } ;`
917880
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 } ;`
881+ return result . trim ( )
882+ }
928883
929- // Clean up the result
930- return result
884+ export function cleanParameters ( params : string ) : string {
885+ return params
931886 . 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 + \( / , '>(' )
887+ . replace ( / \s * , \s * / g, ', ' )
888+ . replace ( / \s * : \s * / g, ': ' )
936889 . trim ( )
937890}
938891
892+ /**
893+ * Analyze a function body to track used type references
894+ */
895+ export function trackUsedTypes ( body : string , usedTypes : Set < string > ) : void {
896+ const typeMatches = body . match ( / [ A - Z ] [ a - z A - Z 0 - 9 ] * (?: < [ ^ > ] + > ) ? / g) || [ ]
897+ typeMatches . forEach ( ( type ) => {
898+ const baseType = type . split ( '<' ) [ 0 ]
899+ if ( baseType )
900+ usedTypes . add ( baseType )
901+ } )
902+ }
903+
939904// Helper functions for line processing
940905export function isCommentLine ( line : string ) : boolean {
941906 return line . startsWith ( '/**' ) || line . startsWith ( '*' ) || line . startsWith ( '*/' )
0 commit comments