|
| 1 | +import ts from "typescript"; |
| 2 | +import {Flow, NodeFunction, NodeParameter} from "@code0-tech/sagittarius-graphql-types"; |
| 3 | +import { |
| 4 | + createCompilerHost, |
| 5 | + DEFAULT_COMPILER_OPTIONS, |
| 6 | + ExtendedDataType, |
| 7 | + ExtendedFunction, |
| 8 | + getParameterCode, |
| 9 | + getSharedTypeDeclarations, |
| 10 | +} from "../utils"; |
| 11 | +import {getNodeValidation} from "../validation/getNodeValidation"; // Wieder hinzugefügt |
| 12 | + |
| 13 | +export interface NodeTypes { |
| 14 | + parameters: string[]; |
| 15 | + returnType: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Resolves the types of the parameters and the return type of a NodeFunction. |
| 20 | + */ |
| 21 | +export const getTypesFromNode = ( |
| 22 | + node: NodeFunction, |
| 23 | + functions: ExtendedFunction[], |
| 24 | + dataTypes: ExtendedDataType[] |
| 25 | +): NodeTypes => { |
| 26 | + const funcMap = new Map(functions.map(f => [f.identifier, f])); |
| 27 | + const funcDef = funcMap.get(node.functionDefinition?.identifier); |
| 28 | + |
| 29 | + if (!funcDef) { |
| 30 | + return { |
| 31 | + parameters: [], |
| 32 | + returnType: "any", |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + const mockFlow: Flow = { |
| 37 | + id: "gid://sagittarius/Flow/0" as any, |
| 38 | + nodes: { __typename: "NodeFunctionConnection", nodes: [node] } |
| 39 | + } as Flow; |
| 40 | + |
| 41 | + const params = (node.parameters?.nodes as NodeParameter[]) || []; |
| 42 | + const paramCodes = params.map(param => getParameterCode(param, mockFlow, (f, n) => getNodeValidation(f, n, functions, dataTypes))); |
| 43 | + |
| 44 | + const funcCallArgs = paramCodes.map(code => code === 'undefined' ? '({} as any)' : code).join(", "); |
| 45 | + |
| 46 | + const signature = funcDef.signature; |
| 47 | + const sourceCode = ` |
| 48 | + ${getSharedTypeDeclarations(dataTypes)} |
| 49 | + declare function testFunc${signature}; |
| 50 | + const result = testFunc(${funcCallArgs}); |
| 51 | + `; |
| 52 | + |
| 53 | + const fileName = "node_types_virtual.ts"; |
| 54 | + const sourceFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest); |
| 55 | + const host = createCompilerHost(fileName, sourceCode, sourceFile); |
| 56 | + |
| 57 | + const program = ts.createProgram([fileName], DEFAULT_COMPILER_OPTIONS, host); |
| 58 | + const checker = program.getTypeChecker(); |
| 59 | + |
| 60 | + let inferredReturnType = "any"; |
| 61 | + let inferredParameterTypes: string[] = []; |
| 62 | + |
| 63 | + const visitor = (n: ts.Node) => { |
| 64 | + if (ts.isVariableDeclaration(n) && n.name.getText() === "result") { |
| 65 | + const resultType = checker.getTypeAtLocation(n); |
| 66 | + inferredReturnType = checker.typeToString( |
| 67 | + resultType, |
| 68 | + n, |
| 69 | + ts.TypeFormatFlags.NoTruncation |
| 70 | + ); |
| 71 | + |
| 72 | + if (ts.isCallExpression(n.initializer!)) { |
| 73 | + const callExpr = n.initializer; |
| 74 | + const signature = checker.getResolvedSignature(callExpr); |
| 75 | + if (signature) { |
| 76 | + inferredParameterTypes = signature.getParameters().map(p => { |
| 77 | + const type = checker.getTypeOfSymbolAtLocation(p, callExpr); |
| 78 | + return checker.typeToString( |
| 79 | + type, |
| 80 | + callExpr, |
| 81 | + ts.TypeFormatFlags.NoTruncation |
| 82 | + ); |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + ts.forEachChild(n, visitor); |
| 88 | + }; |
| 89 | + |
| 90 | + visitor(sourceFile); |
| 91 | + |
| 92 | + return { |
| 93 | + parameters: inferredParameterTypes, |
| 94 | + returnType: inferredReturnType, |
| 95 | + }; |
| 96 | +}; |
0 commit comments