|
| 1 | +import {FunctionDefinition} from "@code0-tech/sagittarius-graphql-types"; |
| 2 | + |
| 3 | +export interface FunctionTypes { |
| 4 | + parameters: string[]; |
| 5 | + returnType: string; |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Resolves the types of the parameters and the return type of a NodeFunction. |
| 10 | + */ |
| 11 | +export const getTypesFromFunction = ( |
| 12 | + functionDefinition: FunctionDefinition |
| 13 | +): FunctionTypes => { |
| 14 | + const signature = functionDefinition.signature; |
| 15 | + |
| 16 | + if (!signature) { |
| 17 | + return {parameters: [], returnType: "any"}; |
| 18 | + } |
| 19 | + |
| 20 | + let searchStart = 0; |
| 21 | + // Skip generics |
| 22 | + if (signature.trim().startsWith('<')) { |
| 23 | + let depth = 0; |
| 24 | + for (let i = 0; i < signature.length; i++) { |
| 25 | + const char = signature[i]; |
| 26 | + if (char === '<') depth++; |
| 27 | + else if (char === '>') { |
| 28 | + depth--; |
| 29 | + if (depth === 0) { |
| 30 | + searchStart = i + 1; |
| 31 | + break; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + const paramStart = signature.indexOf('(', searchStart); |
| 38 | + if (paramStart === -1) { |
| 39 | + return {parameters: [], returnType: "any"}; |
| 40 | + } |
| 41 | + |
| 42 | + // Find matching closing paren |
| 43 | + let paramEnd = -1; |
| 44 | + let depth = 0; |
| 45 | + for (let i = paramStart; i < signature.length; i++) { |
| 46 | + const char = signature[i]; |
| 47 | + if (char === '(') depth++; |
| 48 | + else if (char === ')') { |
| 49 | + depth--; |
| 50 | + if (depth === 0) { |
| 51 | + paramEnd = i; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (paramEnd === -1) { |
| 58 | + return {parameters: [], returnType: "any"}; |
| 59 | + } |
| 60 | + |
| 61 | + const paramsString = signature.substring(paramStart + 1, paramEnd); |
| 62 | + let returnTypeString = signature.substring(paramEnd + 1).trim(); |
| 63 | + |
| 64 | + // Parse return type |
| 65 | + if (returnTypeString.startsWith(':')) { |
| 66 | + returnTypeString = returnTypeString.substring(1).trim(); |
| 67 | + } |
| 68 | + const returnType = returnTypeString || "void"; |
| 69 | + |
| 70 | + // Parse parameters |
| 71 | + const parameters: string[] = []; |
| 72 | + if (paramsString.trim()) { |
| 73 | + let currentParam = ""; |
| 74 | + let pDepthParen = 0; |
| 75 | + let pDepthAngle = 0; |
| 76 | + let pDepthBrace = 0; |
| 77 | + let pDepthBracket = 0; |
| 78 | + |
| 79 | + const pushParam = (p: string) => { |
| 80 | + // Extract type from "name: Type" |
| 81 | + // Scan for first colon at top level |
| 82 | + let colonIndex = -1; |
| 83 | + |
| 84 | + let cDepthBrace = 0; |
| 85 | + let cDepthBracket = 0; |
| 86 | + let cDepthParen = 0; |
| 87 | + let cDepthAngle = 0; |
| 88 | + |
| 89 | + for (let i = 0; i < p.length; i++) { |
| 90 | + const c = p[i]; |
| 91 | + if (c === '{') cDepthBrace++; |
| 92 | + else if (c === '}') cDepthBrace--; |
| 93 | + else if (c === '[') cDepthBracket++; |
| 94 | + else if (c === ']') cDepthBracket--; |
| 95 | + else if (c === '(') cDepthParen++; |
| 96 | + else if (c === ')') cDepthParen--; |
| 97 | + else if (c === '<') cDepthAngle++; |
| 98 | + else if (c === '>') cDepthAngle--; |
| 99 | + else if (c === ':' && cDepthBrace === 0 && cDepthBracket === 0 && cDepthParen === 0 && cDepthAngle === 0) { |
| 100 | + colonIndex = i; |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (colonIndex !== -1) { |
| 106 | + parameters.push(p.substring(colonIndex + 1).trim()); |
| 107 | + } else { |
| 108 | + parameters.push("any"); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + for (const char of paramsString) { |
| 113 | + if (char === '(') pDepthParen++; |
| 114 | + else if (char === ')') pDepthParen--; |
| 115 | + else if (char === '<') pDepthAngle++; |
| 116 | + else if (char === '>') pDepthAngle--; |
| 117 | + else if (char === '{') pDepthBrace++; |
| 118 | + else if (char === '}') pDepthBrace--; |
| 119 | + else if (char === '[') pDepthBracket++; |
| 120 | + else if (char === ']') pDepthBracket--; |
| 121 | + else if (char === ',' && pDepthParen === 0 && pDepthAngle === 0 && pDepthBrace === 0 && pDepthBracket === 0) { |
| 122 | + pushParam(currentParam.trim()); |
| 123 | + currentParam = ""; |
| 124 | + continue; |
| 125 | + } |
| 126 | + currentParam += char; |
| 127 | + } |
| 128 | + if (currentParam.trim()) { |
| 129 | + pushParam(currentParam.trim()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return { |
| 134 | + parameters, |
| 135 | + returnType |
| 136 | + }; |
| 137 | +}; |
0 commit comments