Skip to content

Commit e8cf833

Browse files
authored
Merge pull request #85 from code0-tech/feat/function-schema
Adding function schema
2 parents 3a9920c + 9cb9296 commit e8cf833

3 files changed

Lines changed: 196 additions & 241 deletions

File tree

src/schema/getSignatureSchema.ts

Lines changed: 24 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { DataType, Flow, FunctionDefinition, NodeFunction } from "@code0-tech/sagittarius-graphql-types"
2-
import { createCompilerHost, generateFlowSourceCode, sanitizeId } from "../utils"
3-
import ts, { Type } from "typescript"
4-
import { getSchema, Schema } from "../util/schema.util"
1+
import {DataType, Flow, FunctionDefinition, NodeFunction} from "@code0-tech/sagittarius-graphql-types"
2+
import {createCompilerHost, generateFlowSourceCode, sanitizeId} from "../utils"
3+
import ts, {Type} from "typescript"
4+
import {getSchema, Schema} from "../util/schema.util"
55

66
/**
77
* Represents the schema information for a node parameter.
@@ -11,6 +11,8 @@ export interface NodeSchema {
1111
nodeId: NodeFunction["id"]
1212
/** The schema definition for this node parameter */
1313
schema: Schema
14+
/** The schema definition for the function parameter */
15+
functionSchema: Schema
1416
/** Array of parameter indices that must be resolved before this parameter */
1517
blockedBy?: number[]
1618
}
@@ -83,13 +85,6 @@ export const getSignatureSchema = (
8385
// Extract parameter types from the function definition
8486
const funktionParameterTypes = extractFunctionParameterTypes(checker, funktion, node)
8587

86-
// Combine node and function parameter types, preferring node types when assignable
87-
const combinedParameterTypes = mergeParameterTypes(
88-
checker,
89-
funktionParameterTypes,
90-
nodeParameterTypes,
91-
)
92-
9388
// Identify parameter dependencies based on type parameters
9489
const funktionDependencies = getParameterDependencies(funktion!)
9590

@@ -98,7 +93,8 @@ export const getSignatureSchema = (
9893
nodeId,
9994
checker,
10095
node!,
101-
combinedParameterTypes,
96+
nodeParameterTypes,
97+
funktionParameterTypes,
10298
funktionDependencies,
10399
nodeId ? declaredFunctionsMap : new Map(),
104100
nodeId ? functions : [],
@@ -164,9 +160,9 @@ const extractNodeParameterTypes = (
164160
return undefined
165161
}
166162

167-
const signature = checker.getResolvedSignature(node.initializer)
168-
return signature?.parameters.map((p) =>
169-
checker.getTypeOfSymbolAtLocation(p, node.initializer as ts.CallExpression),
163+
return node.initializer?.arguments.map((p) => {
164+
return checker.getTypeAtLocation(p)
165+
}
170166
)
171167
}
172168

@@ -197,57 +193,6 @@ const extractFunctionParameterTypes = (
197193
})
198194
}
199195

200-
/**
201-
* Merges function and node parameter types by applying type assignability rules.
202-
* Prefers node types when they are assignable to the function's parameter type.
203-
* Handles generic type parameters with constraints.
204-
*
205-
* @param checker - The TypeScript type checker
206-
* @param funktionParameterTypes - Parameter types from function definition
207-
* @param nodeParameterTypes - Parameter types from node's call expression
208-
* @returns Array of resolved parameter types
209-
*/
210-
const mergeParameterTypes = (
211-
checker: ts.TypeChecker,
212-
funktionParameterTypes: Type[] | undefined,
213-
nodeParameterTypes: Type[] | undefined,
214-
): Type[] | undefined => {
215-
if (!funktionParameterTypes) {
216-
return undefined
217-
}
218-
219-
return funktionParameterTypes.map((paramType, index) => {
220-
const nodeType = nodeParameterTypes?.[index]
221-
if (!nodeType) {
222-
return paramType
223-
}
224-
225-
// If both types refer to the same symbol, use the node type
226-
const paramSymbol = paramType.getSymbol()
227-
const nodeSymbol = nodeType.getSymbol()
228-
if (paramSymbol && nodeSymbol && paramSymbol === nodeSymbol) {
229-
return nodeType
230-
}
231-
232-
// Handle generic type parameters
233-
if (paramType.isTypeParameter()) {
234-
const constraint = checker.getBaseConstraintOfType(paramType)
235-
// Use node type if it satisfies the constraint
236-
if (!constraint || checker.isTypeAssignableTo(nodeType, constraint)) {
237-
return nodeType
238-
}
239-
}
240-
241-
// Use node type if assignable to parameter type
242-
if (checker.isTypeAssignableTo(nodeType, paramType)) {
243-
return nodeType
244-
}
245-
246-
// Default to function parameter type
247-
return paramType
248-
})
249-
}
250-
251196
/**
252197
* Identifies parameter dependencies based on shared type parameters.
253198
* Determines which parameters depend on type parameters declared in other parameters.
@@ -292,7 +237,7 @@ const getParameterDependencies = (node: ts.FunctionDeclaration): ParameterDepend
292237
* @param nodeId -
293238
* @param checker - The TypeScript type checker
294239
* @param node - The node's variable declaration
295-
* @param combinedParameterTypes - Merged parameter types to use for schema generation
240+
* @param nodeParameterTypes - Merged parameter types to use for schema generation
296241
* @param funktionDependencies - Parameter dependencies to link with each parameter
297242
* @param declaredFunctionsMap - Map of available functions for schema context
298243
* @param functions - Array of function definitions
@@ -302,23 +247,32 @@ const generateNodeSchemas = (
302247
nodeId: NodeFunction["id"],
303248
checker: ts.TypeChecker,
304249
node: ts.VariableDeclaration,
305-
combinedParameterTypes: Type[] | undefined,
250+
nodeParameterTypes: Type[] | undefined,
251+
functionParameterTypes: Type[] | undefined,
306252
funktionDependencies: ParameterDependency[],
307253
declaredFunctionsMap: Map<string, ts.FunctionDeclaration>,
308254
functions: FunctionDefinition[],
309255
): NodeSchema[] => {
310-
if (!combinedParameterTypes) {
256+
if (!nodeParameterTypes) {
311257
return []
312258
}
313259

314-
return combinedParameterTypes.map((parameterType, index) => ({
260+
return nodeParameterTypes.map((parameterType, index) => ({
315261
nodeId: nodeId,
316262
schema: getSchema(
317263
checker,
318264
node,
319265
parameterType,
320266
Array.from(declaredFunctionsMap.values()),
267+
functions
268+
),
269+
functionSchema: getSchema(
270+
checker,
271+
node,
272+
functionParameterTypes?.[index]!,
273+
Array.from(declaredFunctionsMap.values()),
321274
functions,
275+
false
322276
),
323277
blockedBy: funktionDependencies
324278
.filter((dep) => dep.parameterIndex === index)

0 commit comments

Comments
 (0)