Skip to content

Commit 1de4933

Browse files
committed
feat: refactor signature schema generation to include return type and improve structure
1 parent 04b3b0d commit 1de4933

1 file changed

Lines changed: 68 additions & 7 deletions

File tree

src/schema/getSignatureSchema.ts

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {getSchema, mergeSchemas, Schema} from "../util/schema.util"
88
* Includes the parameter's schema definition and any parameter dependencies that block it.
99
*/
1010
export interface NodeSchema {
11-
nodeId: NodeFunction["id"]
1211
/**
1312
* The schema definition for this node parameter. Produced by merging the
1413
* function-declared parameter schema with the node's concrete value schema:
@@ -21,6 +20,20 @@ export interface NodeSchema {
2120
blockedBy?: number[]
2221
}
2322

23+
/**
24+
* Represents the full schema information for a function signature.
25+
* Wraps the per-parameter schemas together with the schema of the signature's
26+
* return type.
27+
*/
28+
export interface SignatureSchema {
29+
/** The analyzed node's ID, or undefined when the flow signature itself is analyzed */
30+
nodeId: NodeFunction["id"]
31+
/** Schema for each parameter of the signature */
32+
parameters: NodeSchema[]
33+
/** Schema describing the signature's return type */
34+
return: Schema
35+
}
36+
2437
/**
2538
* Represents a parameter dependency relationship.
2639
* Indicates which parameters depend on type parameters defined in other parameters.
@@ -57,7 +70,7 @@ export const getSignatureSchema = (
5770
dataTypes: DataType[],
5871
functions: FunctionDefinition[],
5972
nodeId?: NodeFunction["id"],
60-
): NodeSchema[] => {
73+
): SignatureSchema => {
6174
// Generate TypeScript source code from the flow definition
6275
const sourceCode = generateFlowSourceCode(flow, functions, dataTypes)
6376

@@ -108,8 +121,7 @@ export const getSignatureSchema = (
108121
)
109122

110123
// Generate schema for each parameter
111-
return generateNodeSchemas(
112-
nodeId,
124+
const parameters = generateNodeSchemas(
113125
checker,
114126
node!,
115127
mergedParameterTypes,
@@ -119,6 +131,58 @@ export const getSignatureSchema = (
119131
nodeId ? functions : [],
120132
valueProvidedByIndex,
121133
)
134+
135+
// Resolve the signature's return type and build its schema. The return type
136+
// describes the value the function produces, so it carries no input
137+
// suggestions.
138+
const returnType = extractReturnType(checker, node, funktion)
139+
const returnSchema: Schema = returnType
140+
? getSchema(
141+
checker,
142+
node,
143+
returnType,
144+
Array.from(declaredFunctionsMap.values()),
145+
functions,
146+
false,
147+
)
148+
: {input: "generic"}
149+
150+
return {nodeId, parameters, return: returnSchema}
151+
}
152+
153+
/**
154+
* Extracts the return type of the signature being analyzed.
155+
*
156+
* The node's call expression is preferred because its resolved signature
157+
* substitutes concrete type arguments (e.g. `REST_ADAPTER_INPUT<T>` becomes the
158+
* instantiated type based on the supplied arguments). When no call expression is
159+
* available, the function declaration's own signature is used as a fallback.
160+
*
161+
* @param checker - The TypeScript type checker
162+
* @param node - The variable declaration containing the call expression
163+
* @param funktion - The function declaration for the signature
164+
* @returns The resolved return type, or undefined if it cannot be determined
165+
*/
166+
const extractReturnType = (
167+
checker: ts.TypeChecker,
168+
node: ts.VariableDeclaration | undefined,
169+
funktion: ts.FunctionDeclaration | undefined,
170+
): Type | undefined => {
171+
if (node?.initializer && ts.isCallExpression(node.initializer)) {
172+
const signature = checker.getResolvedSignature(node.initializer)
173+
if (signature) {
174+
return checker.getReturnTypeOfSignature(signature)
175+
}
176+
}
177+
178+
if (funktion) {
179+
const signature = checker.getSignatureFromDeclaration(funktion)
180+
if (signature) {
181+
return checker.getReturnTypeOfSignature(signature)
182+
}
183+
}
184+
185+
return undefined
122186
}
123187

124188
/**
@@ -301,7 +365,6 @@ const getParameterDependencies = (
301365
* Generates node schemas for all parameters.
302366
* Creates schema objects for each parameter with their dependencies.
303367
*
304-
* @param nodeId -
305368
* @param checker - The TypeScript type checker
306369
* @param node - The node's variable declaration
307370
* @param nodeParameterTypes - Merged parameter types to use for schema generation
@@ -313,7 +376,6 @@ const getParameterDependencies = (
313376
* @returns Array of NodeSchema objects
314377
*/
315378
const generateNodeSchemas = (
316-
nodeId: NodeFunction["id"],
317379
checker: ts.TypeChecker,
318380
node: ts.VariableDeclaration,
319381
nodeParameterTypes: Type[] | undefined,
@@ -359,7 +421,6 @@ const generateNodeSchemas = (
359421
: undefined
360422

361423
return {
362-
nodeId: nodeId,
363424
schema: mergeSchemas(
364425
functionSchema,
365426
nodeSchema,

0 commit comments

Comments
 (0)