Skip to content

Commit 4e05106

Browse files
committed
feat: refactor utility functions to improve type handling and remove unused code
1 parent 76e723f commit 4e05106

1 file changed

Lines changed: 16 additions & 43 deletions

File tree

src/utils.ts

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Utility functions for node validation
22
import {
33
DataType,
4-
Flow, FunctionDefinition,
4+
Flow,
5+
FunctionDefinition,
56
NodeFunction,
67
NodeFunctionIdWrapper,
78
NodeParameter,
8-
ReferencePath,
99
ReferenceValue
1010
} from "@code0-tech/sagittarius-graphql-types";
1111
import ts from "typescript";
1212
import {createSystem, createVirtualTypeScriptEnvironment, VirtualTypeScriptEnvironment} from "@typescript/vfs"
13-
import {getTypesFromNode} from "./extraction/getTypesFromNode";
1413
import {DataTypeVariant, getTypeVariant} from "./extraction/getTypeVariant";
14+
import {getTypesFromFunction} from "./extraction/getTypesFromFunction";
1515

1616
/**
1717
* Result of a node or flow validation.
@@ -90,37 +90,6 @@ export function getSharedTypeDeclarations(dataTypes?: DataType[], genericType: s
9090
return `${genericDeclarations}\n${typeAliasDeclarations}`;
9191
}
9292

93-
/**
94-
* Determines the type along a reference path for objects.
95-
* @param value The base value to traverse.
96-
* @param referencePath The path of properties to follow.
97-
* @returns The typeof the final value or 'unknown' if path is broken.
98-
*/
99-
export function getTypeFromReferencePath(value: any, referencePath: ReferencePath[]): string {
100-
let current = value;
101-
for (const ref of referencePath) {
102-
if (current == null) return 'unknown';
103-
if (typeof ref.path === 'string') {
104-
current = current[ref.path];
105-
}
106-
}
107-
return typeof current;
108-
}
109-
110-
/**
111-
* Helper to find a node by ID within the flow structure.
112-
*/
113-
function findNodeById(flow?: Flow, nodeId?: string): NodeFunction | undefined {
114-
const nodes = flow?.nodes;
115-
if (!nodes) return undefined;
116-
117-
if (Array.isArray(nodes)) {
118-
return nodes.find((n: any) => n.id === nodeId);
119-
}
120-
121-
return nodes.nodes?.find((n: any) => n.id === nodeId)!;
122-
}
123-
12493
/**
12594
* Sanitizes an ID for use as a TypeScript variable name.
12695
*/
@@ -146,22 +115,24 @@ export function generateFlowSourceCode(
146115
const params = (node.parameters?.nodes as NodeParameter[]) || [];
147116
const args = params.map((p, paramIdx) => {
148117
const val = p.value;
149-
if (!val) return isForInference ? `/* @pos ${nodeId} ${paramIdx} */ {}` : `/* @pos ${nodeId} ${paramIdx} */ undefined`;
118+
if (!val) return isForInference ? `/* @pos ${nodeId} ${paramIdx} */ {}` : `/* @pos ${nodeId} ${paramIdx} */ undefined`;
150119
if (val.__typename === "ReferenceValue") {
151120
const ref = val as ReferenceValue;
152121
if (!ref.nodeFunctionId) return `/* @pos ${nodeId} ${paramIdx} */ undefined`;
153122
let refCode = ref.inputIndex !== undefined
154123
? `p_${sanitizeId(ref.nodeFunctionId)}_${ref.parameterIndex}[${ref.inputIndex}]`
155124
: `node_${sanitizeId(ref.nodeFunctionId)}`;
156-
ref.referencePath?.forEach(pathObj => { refCode += `?.${pathObj.path}`; });
125+
ref.referencePath?.forEach(pathObj => {
126+
refCode += `?.${pathObj.path}`;
127+
});
157128
return `/* @pos ${nodeId} ${paramIdx} */ ${refCode}`;
158129
}
159130
if (val.__typename === "LiteralValue") return `/* @pos ${nodeId} ${paramIdx} */ ${JSON.stringify(val.value)}`;
160131
if (val.__typename === "NodeFunctionIdWrapper") {
161132
const wrapper = val as NodeFunctionIdWrapper;
162133
return generateNodeCall(wrapper.id!, nodeId, paramIdx);
163134
}
164-
return isForInference ? `/* @pos ${nodeId} ${paramIdx} */ ({} as any)` : `/* @pos ${nodeId} ${paramIdx} */ undefined`;
135+
return isForInference ? `/* @pos ${nodeId} ${paramIdx} */ ({} as any)` : `/* @pos ${nodeId} ${paramIdx} */ undefined`;
165136
}).join(", ");
166137

167138
const funcName = `fn_${node.functionDefinition.identifier.replace(/::/g, '_')}`;
@@ -183,22 +154,24 @@ export function generateFlowSourceCode(
183154
if (!funcDef) return `${indent}// Error: Function ${node.functionDefinition.identifier} not found\n`;
184155

185156
// Only use getTypesFromNode if we are NOT already doing inference to avoid infinite recursion
186-
let nodeTypes: any = { parameters: [] };
157+
let nodeTypes: any = {parameters: []};
187158
if (!isForInference) {
188-
nodeTypes = getTypesFromNode(node, functions, dataTypes);
159+
nodeTypes = getTypesFromFunction(funcDef);
189160
}
190161

191162
const params = (node.parameters?.nodes as NodeParameter[]) || [];
192163
const args = params.map((p, index) => {
193164
const val = p.value;
194-
if (!val) return isForInference ? `/* @pos ${nodeId} ${index} */ {}` : `/* @pos ${nodeId} ${index} */ undefined`;
165+
if (!val) return isForInference ? `/* @pos ${nodeId} ${index} */ {}` : `/* @pos ${nodeId} ${index} */ undefined`;
195166
if (val.__typename === "ReferenceValue") {
196167
const ref = val as ReferenceValue;
197168
if (!ref.nodeFunctionId) return `/* @pos ${nodeId} ${index} */ undefined`;
198169
let refCode = ref.inputIndex !== undefined
199170
? `p_${sanitizeId(ref.nodeFunctionId)}_${ref.parameterIndex}[${ref.inputIndex}]`
200171
: `node_${sanitizeId(ref.nodeFunctionId)}`;
201-
ref.referencePath?.forEach(pathObj => { refCode += `?.${pathObj.path}`; });
172+
ref.referencePath?.forEach(pathObj => {
173+
refCode += `?.${pathObj.path}`;
174+
});
202175
return `/* @pos ${nodeId} ${index} */ ${refCode}`;
203176
}
204177
if (val.__typename === "LiteralValue") return `/* @pos ${nodeId} ${index} */ ${JSON.stringify(val.value)}`;
@@ -225,7 +198,7 @@ export function generateFlowSourceCode(
225198
return `/* @pos ${nodeId} ${index} */ (...${lambdaArgName}) => {\n${subTreeCode}${indent}}`;
226199
}
227200
}
228-
return isForInference ? `/* @pos ${nodeId} ${index} */ {}` : `/* @pos ${nodeId} ${index} */ undefined`;
201+
return isForInference ? `/* @pos ${nodeId} ${index} */ {}` : `/* @pos ${nodeId} ${index} */ undefined`;
229202
}).join(", ");
230203

231204
const varName = `node_${sanitizeId(node.id!)}`;
@@ -319,6 +292,6 @@ export function getInferredTypesFromFlow(
319292
};
320293
visit(sourceFile);
321294

322-
return { nodes: nodeTypes, parameters: parameterTypes };
295+
return {nodes: nodeTypes, parameters: parameterTypes};
323296
}
324297

0 commit comments

Comments
 (0)