1- import { NodeFunction } from "@code0-tech/sagittarius-graphql-types" ;
2- import ts from "typescript" ;
3- import { createCompilerHost , DEFAULT_COMPILER_OPTIONS , ExtendedFunction , getSharedTypeDeclarations } from "../utils" ;
4- import { DATA_TYPES } from "../../test/data" ;
1+ import { DataType , FunctionDefinition , NodeFunction } from "@code0-tech/sagittarius-graphql-types" ;
2+ import { createCompilerHost , getSharedTypeDeclarations } from "../utils" ;
53
64/**
75 * Suggests NodeFunctions based on a given type and a list of available FunctionDefinitions.
86 * Returns functions whose return type is compatible with the target type.
97 */
10- export function getNodeSuggestions ( type : string , functions : ExtendedFunction [ ] ) : NodeFunction [ ] {
8+ export function getNodeSuggestions ( type : string , functions : FunctionDefinition [ ] , dataTypes : DataType [ ] ) : NodeFunction [ ] {
119 if ( ! type || ! functions || functions . length === 0 ) {
1210 return [ ] ;
1311 }
1412
15- const fileName = "suggestions.ts" ;
16-
17- const sharedTypes = getSharedTypeDeclarations ( DATA_TYPES ) ;
18-
1913 function getGenericsCount ( input : string ) : number {
2014 const match = input . match ( / < ( [ ^ > ] + ) > / ) ;
2115 if ( ! match ) return 0 ;
2216 return match [ 1 ] . split ( ',' ) . map ( s => s . trim ( ) ) . filter ( Boolean ) . length ;
2317 }
2418
19+ const sharedTypes = getSharedTypeDeclarations ( dataTypes ) ;
2520 const sourceCode = `
2621 ${ sharedTypes }
2722 type TargetType = ${ type } ;
2823 ${ functions . map ( ( f , i ) => {
29-
24+
3025 return `
3126 declare function Fu${ i } ${ f . signature } ;
32- type F${ i } = ReturnType<typeof Fu${ i } ${ getGenericsCount ( f . signature ) > 0 ? `<${ Array ( getGenericsCount ( f . signature ) ) . fill ( "any" ) . join ( ", " ) } >` : "" } >;
27+ type F${ i } = ReturnType<typeof Fu${ i } ${ getGenericsCount ( f . signature ! ) > 0 ? `<${ Array ( getGenericsCount ( f . signature ! ) ) . fill ( "any" ) . join ( ", " ) } >` : "" } >;
3328 ` ;
3429 } ) . join ( "\n" ) }
3530 ${ functions . map ( ( _ , i ) => `const check${ i } : TargetType = {} as F${ i } ;` ) . join ( "\n" ) }
3631 ` ;
3732
38- console . log ( sourceCode )
39-
40- const sourceFile = ts . createSourceFile ( fileName , sourceCode , ts . ScriptTarget . Latest ) ;
41- const host = createCompilerHost ( fileName , sourceCode , sourceFile ) ;
42- const program = ts . createProgram ( [ fileName ] , {
43- ...DEFAULT_COMPILER_OPTIONS
44- } , host ) ;
33+ const fileName = "index.ts" ;
34+ const host = createCompilerHost ( fileName , sourceCode ) ;
35+ const sourceFile = host . getSourceFile ( fileName ) ! ;
36+ const program = host . languageService . getProgram ( ) ! ;
4537
4638 const diagnostics = program . getSemanticDiagnostics ( ) ;
4739 const errorLines = new Set < number > ( ) ;
@@ -59,7 +51,6 @@ export function getNodeSuggestions(type: string, functions: ExtendedFunction[]):
5951 const actualLine = lines . findIndex ( l => l . includes ( lineToMatch ) ) ;
6052
6153
62-
6354 if ( actualLine !== - 1 && errorLines . has ( actualLine ) ) {
6455 return null ;
6556 }
0 commit comments