Skip to content

Commit 43cfa3f

Browse files
author
nicosammito
committed
feat: add nodeId and parameterIndex to diagnostics for enhanced error tracking
1 parent 63d1992 commit 43cfa3f

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/validation/getFlowValidation.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ts, {flattenDiagnosticMessageText} from "typescript";
22
import {
33
DataType,
44
Flow,
5-
FunctionDefinition,
5+
FunctionDefinition, NodeFunction,
66
NodeFunctionIdWrapper,
77
NodeParameter,
88
ReferenceValue
@@ -52,8 +52,8 @@ export const getFlowValidation = (
5252
if (!ref.nodeFunctionId) return "undefined";
5353

5454
let refCode = ref.parameterIndex !== undefined
55-
? `p_${sanitizeId(ref.nodeFunctionId)}_${ref.parameterIndex}`
56-
: `node_${sanitizeId(ref.nodeFunctionId)}`;
55+
? `/* @pos ${nodeId} ${index} */ p_${sanitizeId(ref.nodeFunctionId)}_${ref.parameterIndex}`
56+
: `/* @pos ${nodeId} ${index} */ node_${sanitizeId(ref.nodeFunctionId)}`;
5757

5858
ref.referencePath?.forEach(pathObj => {
5959
refCode += `?.${pathObj.path}`;
@@ -63,14 +63,14 @@ export const getFlowValidation = (
6363
}
6464

6565
if (val.__typename === "LiteralValue") {
66-
return JSON.stringify(val.value);
66+
return `/* @pos ${nodeId} ${index} */ ${JSON.stringify(val.value)}`;
6767
}
6868

6969
if (val.__typename === "NodeFunctionIdWrapper") {
7070
const wrapper = val as NodeFunctionIdWrapper;
7171
const lambdaArgName = `p_${sanitizeId(node.id!)}_${index}`;
7272
const subTreeCode = generateNodeCode(wrapper.id!, indent + " ");
73-
return `(${lambdaArgName}) => {\n${subTreeCode}${indent}}`;
73+
return `/* @pos ${nodeId} ${index} */ (${lambdaArgName}) => {\n${subTreeCode}${indent}}`;
7474
}
7575

7676
return "undefined";
@@ -121,10 +121,25 @@ export const getFlowValidation = (
121121

122122
if (isMockError) return null;
123123

124+
let nodeId: NodeFunction['id'] | undefined;
125+
let parameterIndex: number | undefined;
126+
127+
if (d.start !== undefined) {
128+
const fullText = sourceFile.getFullText();
129+
const textBefore = fullText.substring(0, d.start);
130+
const posMatch = textBefore.match(/\/\* @pos ([^ ]+) (\d+) \*\/\s*$/);
131+
if (posMatch) {
132+
nodeId = posMatch[1] as NodeFunction['id'];
133+
parameterIndex = parseInt(posMatch[2], 10);
134+
}
135+
}
136+
124137
return {
125138
message,
126139
code: d.code,
127140
severity: "error" as const,
141+
nodeId,
142+
parameterIndex
128143
};
129144
}).filter((e) => e !== null);
130145

test/flowValidation.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,32 @@ describe('getFlowValidation - Integrationstest', () => {
113113
expect(result.diagnostics).toHaveLength(0);
114114
});
115115

116+
it('sollte nodeId und parameterIndex in den Diagnostics zurückgeben', () => {
117+
const flow: Flow = {
118+
nodes: {
119+
nodes: [
120+
{
121+
id: "gid://sagittarius/NodeFunction/1",
122+
functionDefinition: { identifier: "std::math::add" },
123+
parameters: {
124+
nodes: [
125+
{ value: { __typename: "LiteralValue", value: "not accessibility a number" } },
126+
{ value: { __typename: "LiteralValue", value: 10 } }
127+
]
128+
}
129+
}
130+
]
131+
}
132+
};
133+
134+
const result = getFlowValidation(flow, FUNCTION_SIGNATURES, DATA_TYPES);
135+
136+
console.log(result);
137+
138+
expect(result.isValid).toBe(false);
139+
const diagnostic = result.diagnostics.find(d => d.nodeId === "gid://sagittarius/NodeFunction/1" && d.parameterIndex === 0);
140+
expect(diagnostic).toBeDefined();
141+
expect(diagnostic?.message).toContain("number");
142+
});
143+
116144
});

0 commit comments

Comments
 (0)