@@ -5,77 +5,76 @@ import {createCompilerHost, getSharedTypeDeclarations} from "../utils";
55 * Suggests NodeFunctions based on a given type and a list of available FunctionDefinitions.
66 * Returns functions whose return type is compatible with the target type.
77 */
8- export function getNodeSuggestions ( type : string , functions : FunctionDefinition [ ] , dataTypes : DataType [ ] ) : NodeFunction [ ] {
9- if ( ! type || ! functions || functions . length === 0 ) {
10- return [ ] ;
11- }
8+ export const getNodeSuggestions = (
9+ type ?: string ,
10+ functions ?: FunctionDefinition [ ] ,
11+ dataTypes ?: DataType [ ]
12+ ) : NodeFunction [ ] => {
1213
13- function getGenericsCount ( input : string ) : number {
14- const match = input . match ( / < ( [ ^ > ] + ) > / ) ;
15- if ( ! match ) return 0 ;
16- return match [ 1 ] . split ( ',' ) . map ( s => s . trim ( ) ) . filter ( Boolean ) . length ;
17- }
14+ let functionToSuggest = functions
1815
19- const sharedTypes = getSharedTypeDeclarations ( dataTypes ) ;
20- const sourceCode = `
21- ${ sharedTypes }
22- type TargetType = ${ type } ;
23- ${ functions . map ( ( f , i ) => {
16+ if ( type && functions ) {
17+ function getGenericsCount ( input : string ) : number {
18+ const match = input . match ( / < ( [ ^ > ] + ) > / ) ;
19+ if ( ! match ) return 0 ;
20+ return match [ 1 ] . split ( ',' ) . map ( s => s . trim ( ) ) . filter ( Boolean ) . length ;
21+ }
2422
25- return `
26- declare function Fu${ i } ${ f . signature } ;
27- type F${ i } = ReturnType<typeof Fu${ i } ${ getGenericsCount ( f . signature ! ) > 0 ? `<${ Array ( getGenericsCount ( f . signature ! ) ) . fill ( "any" ) . join ( ", " ) } >` : "" } >;
28- ` ;
29- } ) . join ( "\n" ) }
30- ${ functions . map ( ( _ , i ) => `const check${ i } : TargetType = {} as F${ i } ;` ) . join ( "\n" ) }
23+ const sourceCode = `
24+ ${ getSharedTypeDeclarations ( dataTypes ) }
25+ type TargetType = ${ type } ;
26+ ${ functions ?. map ( ( f , i ) => {
27+ return `
28+ declare function Fu${ i } ${ f . signature } ;
29+ type F${ i } = ReturnType<typeof Fu${ i } ${ getGenericsCount ( f . signature ! ) > 0 ? `<${ Array ( getGenericsCount ( f . signature ! ) ) . fill ( "any" ) . join ( ", " ) } >` : "" } >;
30+ ` ;
31+ } ) . join ( "\n" ) }
32+ ${ functions ?. map ( ( _ , i ) => `const check${ i } : TargetType = {} as F${ i } ;` ) . join ( "\n" ) }
3133 ` ;
3234
33- const fileName = "index.ts" ;
34- const host = createCompilerHost ( fileName , sourceCode ) ;
35- const sourceFile = host . getSourceFile ( fileName ) ! ;
36- const program = host . languageService . getProgram ( ) ! ;
35+ const fileName = "index.ts" ;
36+ const host = createCompilerHost ( fileName , sourceCode ) ;
37+ const sourceFile = host . getSourceFile ( fileName ) ! ;
38+ const program = host . languageService . getProgram ( ) ! ;
3739
38- const diagnostics = program . getSemanticDiagnostics ( ) ;
39- const errorLines = new Set < number > ( ) ;
40- diagnostics . forEach ( diag => {
41- if ( diag . file === sourceFile && diag . start !== undefined ) {
42- errorLines . add ( sourceFile . getLineAndCharacterOfPosition ( diag . start ) . line ) ;
43- }
44- } ) ;
40+ const diagnostics = program . getSemanticDiagnostics ( ) ;
41+ const errorLines = new Set < number > ( ) ;
42+ diagnostics . forEach ( diag => {
43+ if ( diag . file === sourceFile && diag . start !== undefined ) {
44+ errorLines . add ( sourceFile . getLineAndCharacterOfPosition ( diag . start ) . line ) ;
45+ }
46+ } ) ;
4547
46- return functions
47- . map ( ( f , i ) => {
48- // Find the line number of 'const check${i}'
48+ functionToSuggest = functions . filter ( ( _ , i ) => {
4949 const lineToMatch = `const check${ i } : TargetType = {} as F${ i } ;` ;
5050 const lines = sourceCode . split ( "\n" ) ;
5151 const actualLine = lines . findIndex ( l => l . includes ( lineToMatch ) ) ;
52+ return actualLine !== - 1 && ! errorLines . has ( actualLine ) ;
53+ } ) ;
54+ }
5255
5356
54- if ( actualLine !== - 1 && errorLines . has ( actualLine ) ) {
55- return null ;
57+ return functionToSuggest ?. map ( f => {
58+ return {
59+ __typename : "NodeFunction" ,
60+ id : `gid://sagittarius/NodeFunction/1` ,
61+ functionDefinition : {
62+ __typename : "FunctionDefinition" ,
63+ id : f . identifier as any ,
64+ identifier : f . identifier ,
65+ } ,
66+ parameters : {
67+ __typename : "NodeParameterConnection" ,
68+ nodes : ( f . parameterDefinitions ?. nodes || [ ] ) . map ( p => ( {
69+ __typename : "NodeParameter" ,
70+ parameterDefinition : {
71+ __typename : "ParameterDefinition" ,
72+ id : p ?. identifier as any ,
73+ identifier : p ?. identifier
74+ } ,
75+ value : null
76+ } ) )
5677 }
57-
58- return {
59- __typename : "NodeFunction" ,
60- id : `gid://sagittarius/NodeFunction/1` ,
61- functionDefinition : {
62- __typename : "FunctionDefinition" ,
63- id : f . identifier as any ,
64- identifier : f . identifier ,
65- } ,
66- parameters : {
67- __typename : "NodeParameterConnection" ,
68- nodes : ( f . parameterDefinitions ?. nodes || [ ] ) . map ( p => ( {
69- __typename : "NodeParameter" ,
70- parameterDefinition : {
71- __typename : "ParameterDefinition" ,
72- id : p ?. identifier as any ,
73- identifier : p ?. identifier
74- } ,
75- value : null
76- } ) )
77- }
78- } as any as NodeFunction ;
79- } )
80- . filter ( ( f ) : f is NodeFunction => f !== null ) ;
78+ } as any as NodeFunction ;
79+ } ) . filter ( ( f ) : f is NodeFunction => f !== null ) ?? [ ] ;
8180}
0 commit comments