@@ -784,68 +784,62 @@ export function processTypeDeclaration(declaration: string, isExported = true):
784
784
}
785
785
786
786
/**
787
- * Extract complete function signature handling multi-line declarations
787
+ * Enhanced function signature extraction with better destructuring and generics support
788
788
*/
789
- export function extractFunctionSignature ( declaration : string ) : {
789
+ export interface FunctionSignature {
790
790
name : string
791
791
params : string
792
792
returnType : string
793
793
isAsync : boolean
794
794
generics : string
795
- } {
796
- console . log ( '\n[Signature Extraction Debug]' )
797
- console . log ( 'Input:' , declaration )
795
+ }
796
+
798
797
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
800
803
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 + / , '' )
804
807
. trim ( )
805
808
806
- console . log ( 'Cleaned Declaration:' , cleanDeclaration )
809
+ // Extract generics first
810
+ const genericsMatch = cleanDeclaration . match ( / < ( [ ^ > ] + ) > / )
811
+ const generics = genericsMatch ? `<${ genericsMatch [ 1 ] } >` : ''
807
812
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 ( / < ( [ ^ > ] + ) > / , '' )
814
815
815
- console . log ( 'Name and Generics:' , { name, generics } )
816
+ // Extract function name
817
+ const nameMatch = withoutGenerics . match ( / ^ ( [ ^ ( < \s ] + ) / )
818
+ const name = nameMatch ? nameMatch [ 1 ] : ''
816
819
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 * : ) / )
819
822
let params = paramsMatch ? paramsMatch [ 1 ] . trim ( ) : ''
820
823
821
- // Transform destructured parameters by extracting only the type
824
+ // Handle destructured parameters
822
825
if ( params . startsWith ( '{' ) ) {
823
- // Match the type after the colon, handling possible generic types
824
826
const typeMatch = params . match ( / \} : \s * ( [ ^ ) ] + ) $ / )
825
827
if ( typeMatch ) {
826
- const paramType = typeMatch [ 1 ] . trim ( )
827
- params = `options: ${ paramType } `
828
+ params = `options: ${ typeMatch [ 1 ] . trim ( ) } `
828
829
}
829
830
}
830
831
831
- console . log ( 'Processed Params:' , params )
832
-
833
832
// Extract return type
834
- const returnTypeMatch = cleanDeclaration . match ( / \) \s * : \s * ( [ \s \S ] + ?) (? = \{ | $ ) / )
833
+ const returnTypeMatch = withoutGenerics . match ( / \) \s * : \s * ( [ \s \S ] + ?) (? = \{ | $ ) / )
835
834
const returnType = returnTypeMatch ? returnTypeMatch [ 1 ] . trim ( ) : 'void'
836
835
837
- console . log ( 'Return Type:' , returnType )
838
-
839
- const result = {
836
+ return {
840
837
name,
841
838
params,
842
839
returnType,
843
- isAsync,
840
+ isAsync : declaration . includes ( 'async' ) ,
844
841
generics,
845
842
}
846
-
847
- console . log ( 'Final Extraction Result:' , result )
848
- return result
849
843
}
850
844
851
845
/**
@@ -856,9 +850,6 @@ export function processFunctionDeclaration(
856
850
usedTypes : Set < string > ,
857
851
isExported = true ,
858
852
) : string {
859
- console . log ( '\n[Function Declaration Debug]' )
860
- console . log ( 'Input:' , declaration )
861
-
862
853
const {
863
854
name,
864
855
params,
@@ -867,75 +858,49 @@ export function processFunctionDeclaration(
867
858
generics,
868
859
} = extractFunctionSignature ( declaration )
869
860
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
879
862
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
+
881
868
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 )
893
871
} )
894
872
}
895
873
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 `
912
876
if ( isAsync )
913
877
result += 'async '
914
878
915
- // Add function name
916
- result += `function ${ name } `
879
+ result += `function ${ name } ${ generics } (${ params } ): ${ returnType } ;`
917
880
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
+ }
928
883
929
- // Clean up the result
930
- return result
884
+ export function cleanParameters ( params : string ) : string {
885
+ return params
931
886
. 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, ': ' )
936
889
. trim ( )
937
890
}
938
891
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
+
939
904
// Helper functions for line processing
940
905
export function isCommentLine ( line : string ) : boolean {
941
906
return line . startsWith ( '/**' ) || line . startsWith ( '*' ) || line . startsWith ( '*/' )
0 commit comments