@siglo/ide-analysis 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +45 -2
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +125 -0
- package/dist/bin.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/mapper.d.ts +12 -0
- package/dist/mapper.d.ts.map +1 -0
- package/dist/mapper.js +72 -0
- package/dist/mapper.js.map +1 -0
- package/dist/parser.d.ts +14 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +164 -0
- package/dist/parser.js.map +1 -0
- package/dist/patterns/step-detector.d.ts +23 -0
- package/dist/patterns/step-detector.d.ts.map +1 -0
- package/dist/patterns/step-detector.js +331 -0
- package/dist/patterns/step-detector.js.map +1 -0
- package/dist/patterns/workflow-detector.d.ts +5 -0
- package/dist/patterns/workflow-detector.d.ts.map +1 -0
- package/dist/patterns/workflow-detector.js +697 -0
- package/dist/patterns/workflow-detector.js.map +1 -0
- package/package.json +51 -4
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.analyzeWorkflowsInSourceFile = analyzeWorkflowsInSourceFile;
|
|
7
|
+
exports.runDiagnostics = runDiagnostics;
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const mapper_1 = require("../mapper");
|
|
10
|
+
const step_detector_1 = require("./step-detector");
|
|
11
|
+
function analyzeWorkflowsInSourceFile(sourceFile, uri, documentVersion = 1) {
|
|
12
|
+
const ctx = {
|
|
13
|
+
sourceFile,
|
|
14
|
+
uri,
|
|
15
|
+
documentVersion,
|
|
16
|
+
stepDefinitions: new Map(),
|
|
17
|
+
flowDefinitions: new Map(),
|
|
18
|
+
};
|
|
19
|
+
// Phase 1: Collect top-level step variable declarations
|
|
20
|
+
collectStepDefinitions(sourceFile, ctx);
|
|
21
|
+
// Phase 2: Find workflow declarations and chains
|
|
22
|
+
const workflows = [];
|
|
23
|
+
walkForWorkflows(sourceFile, ctx, workflows);
|
|
24
|
+
// Fallback: If no workflow call expression was found, but steps exist in file,
|
|
25
|
+
// create a default synthetic workflow document representing the file's steps.
|
|
26
|
+
if (workflows.length === 0 && ctx.stepDefinitions.size > 0) {
|
|
27
|
+
const nodes = Array.from(ctx.stepDefinitions.values());
|
|
28
|
+
const edges = [];
|
|
29
|
+
for (let i = 0; i < nodes.length - 1; i++) {
|
|
30
|
+
edges.push({
|
|
31
|
+
id: `edge_${nodes[i].id}_to_${nodes[i + 1].id}`,
|
|
32
|
+
from: nodes[i].id,
|
|
33
|
+
to: nodes[i + 1].id,
|
|
34
|
+
type: 'sequential',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
workflows.push({
|
|
38
|
+
uri,
|
|
39
|
+
documentVersion,
|
|
40
|
+
workflowName: 'defaultWorkflow',
|
|
41
|
+
nodes,
|
|
42
|
+
edges,
|
|
43
|
+
unresolved: [],
|
|
44
|
+
diagnostics: runDiagnostics(nodes, edges),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return workflows;
|
|
48
|
+
}
|
|
49
|
+
function collectStepDefinitions(sourceFile, ctx) {
|
|
50
|
+
function visit(node) {
|
|
51
|
+
if (typescript_1.default.isVariableDeclaration(node) && node.initializer) {
|
|
52
|
+
const varName = node.name.getText(sourceFile);
|
|
53
|
+
const enclosingRange = (0, mapper_1.getSourceRange)(sourceFile, node);
|
|
54
|
+
const detected = (0, step_detector_1.detectStepFromExpression)(node.initializer, sourceFile, enclosingRange);
|
|
55
|
+
if (detected && detected.kind === 'node') {
|
|
56
|
+
const stepNode = {
|
|
57
|
+
...detected.node,
|
|
58
|
+
declarationRange: enclosingRange,
|
|
59
|
+
};
|
|
60
|
+
ctx.stepDefinitions.set(varName, stepNode);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
typescript_1.default.forEachChild(node, visit);
|
|
64
|
+
}
|
|
65
|
+
visit(sourceFile);
|
|
66
|
+
}
|
|
67
|
+
function getEnclosingName(node, sourceFile) {
|
|
68
|
+
let p = node.parent;
|
|
69
|
+
while (p) {
|
|
70
|
+
if (typescript_1.default.isVariableDeclaration(p)) {
|
|
71
|
+
return p.name.getText(sourceFile);
|
|
72
|
+
}
|
|
73
|
+
if (typescript_1.default.isBinaryExpression(p) && p.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken) {
|
|
74
|
+
return p.left.getText(sourceFile);
|
|
75
|
+
}
|
|
76
|
+
if (typescript_1.default.isPropertyAssignment(p)) {
|
|
77
|
+
return p.name.getText(sourceFile).replace(/['"]/g, '');
|
|
78
|
+
}
|
|
79
|
+
if (typescript_1.default.isExportAssignment(p)) {
|
|
80
|
+
return 'default';
|
|
81
|
+
}
|
|
82
|
+
if (typescript_1.default.isSourceFile(p) || typescript_1.default.isBlock(p))
|
|
83
|
+
break;
|
|
84
|
+
p = p.parent;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
function isOutermostFlowCall(callExpr) {
|
|
89
|
+
const p = callExpr.parent;
|
|
90
|
+
if (p && typescript_1.default.isPropertyAccessExpression(p)) {
|
|
91
|
+
const grandP = p.parent;
|
|
92
|
+
if (grandP && typescript_1.default.isCallExpression(grandP) && grandP.expression === p) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
function walkForWorkflows(node, ctx, outWorkflows) {
|
|
99
|
+
if (typescript_1.default.isCallExpression(node)) {
|
|
100
|
+
if (isOutermostFlowCall(node)) {
|
|
101
|
+
const rootFlow = findRootFlowCall(node, ctx.sourceFile);
|
|
102
|
+
if (!rootFlow) {
|
|
103
|
+
let receiver = node;
|
|
104
|
+
const configuration = [];
|
|
105
|
+
while (typescript_1.default.isCallExpression(receiver) && typescript_1.default.isPropertyAccessExpression(receiver.expression)) {
|
|
106
|
+
configuration.unshift(receiver);
|
|
107
|
+
receiver = receiver.expression.expression;
|
|
108
|
+
}
|
|
109
|
+
if (typescript_1.default.isIdentifier(receiver)) {
|
|
110
|
+
const definition = ctx.flowDefinitions.get(receiver.text);
|
|
111
|
+
if (definition?.properties)
|
|
112
|
+
for (const call of configuration) {
|
|
113
|
+
const method = call.expression.name.text;
|
|
114
|
+
applyFlowProperty(definition.properties, method, [...call.arguments], ctx.sourceFile);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (rootFlow) {
|
|
119
|
+
// Inline flows are expanded by their enclosing combinator/flow. They
|
|
120
|
+
// must not become a separate workflow named after the outer variable.
|
|
121
|
+
let ancestor = node.parent;
|
|
122
|
+
while (ancestor && !typescript_1.default.isStatement(ancestor) && !typescript_1.default.isVariableDeclaration(ancestor)) {
|
|
123
|
+
if (typescript_1.default.isCallExpression(ancestor)) {
|
|
124
|
+
const callee = ancestor.expression;
|
|
125
|
+
const name = typescript_1.default.isIdentifier(callee) ? callee.text : typescript_1.default.isPropertyAccessExpression(callee) ? callee.name.text : '';
|
|
126
|
+
if (['flow', 'pipe', 'parallel', 'all', 'allSettled', 'race', 'map', 'when', 'branch'].includes(name))
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
ancestor = ancestor.parent;
|
|
130
|
+
}
|
|
131
|
+
const doc = parseWorkflowChain(node, rootFlow, ctx);
|
|
132
|
+
if (doc && !outWorkflows.some((w) => w.workflowName === doc.workflowName)) {
|
|
133
|
+
outWorkflows.push(doc);
|
|
134
|
+
if (doc.workflowName) {
|
|
135
|
+
const definition = { nodes: doc.nodes, edges: doc.edges, properties: doc.properties };
|
|
136
|
+
ctx.flowDefinitions.set(doc.workflowName, definition);
|
|
137
|
+
const variableName = getEnclosingName(node, ctx.sourceFile);
|
|
138
|
+
if (variableName)
|
|
139
|
+
ctx.flowDefinitions.set(variableName, definition);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
typescript_1.default.forEachChild(node, (child) => walkForWorkflows(child, ctx, outWorkflows));
|
|
146
|
+
}
|
|
147
|
+
function findRootFlowCall(callExpr, sourceFile) {
|
|
148
|
+
let curr = callExpr;
|
|
149
|
+
let rootCall = null;
|
|
150
|
+
while (curr) {
|
|
151
|
+
if (typescript_1.default.isCallExpression(curr)) {
|
|
152
|
+
const expr = curr.expression;
|
|
153
|
+
if (typescript_1.default.isIdentifier(expr) && (expr.text === 'flow' || expr.text === 'workflow' || expr.text === 'pipe' || expr.text === 'Pipeline')) {
|
|
154
|
+
rootCall = curr;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (typescript_1.default.isPropertyAccessExpression(expr) && ['Step', 'Func', 'Chain'].includes(expr.expression.getText(sourceFile)) && ['flow', 'pipe'].includes(expr.name.text)) {
|
|
158
|
+
rootCall = curr;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
if (typescript_1.default.isPropertyAccessExpression(expr)) {
|
|
162
|
+
curr = expr.expression;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
else if (typescript_1.default.isPropertyAccessExpression(curr)) {
|
|
168
|
+
curr = curr.expression;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (rootCall) {
|
|
175
|
+
let name = '';
|
|
176
|
+
const firstArg = rootCall.arguments[0];
|
|
177
|
+
if (firstArg) {
|
|
178
|
+
if (typescript_1.default.isStringLiteral(firstArg) || typescript_1.default.isNoSubstitutionTemplateLiteral(firstArg)) {
|
|
179
|
+
name = firstArg.text;
|
|
180
|
+
}
|
|
181
|
+
else if (typescript_1.default.isObjectLiteralExpression(firstArg)) {
|
|
182
|
+
for (const prop of firstArg.properties) {
|
|
183
|
+
if (typescript_1.default.isPropertyAssignment(prop) &&
|
|
184
|
+
prop.name.getText(sourceFile).replace(/['"]/g, '') === 'name' &&
|
|
185
|
+
(typescript_1.default.isStringLiteral(prop.initializer) || typescript_1.default.isNoSubstitutionTemplateLiteral(prop.initializer))) {
|
|
186
|
+
name = prop.initializer.text;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (!name) {
|
|
192
|
+
const enclosingName = getEnclosingName(callExpr, sourceFile);
|
|
193
|
+
if (enclosingName) {
|
|
194
|
+
name = enclosingName;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const argNames = rootCall.arguments.map((a) => a.getText(sourceFile)).join(', ');
|
|
198
|
+
name = argNames ? `flow(${argNames})` : 'workflow';
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { call: rootCall, name };
|
|
202
|
+
}
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
function parseWorkflowChain(outerCall, rootFlow, ctx) {
|
|
206
|
+
// Collect the chain of calls from inner rootFlow to outerCall
|
|
207
|
+
const chain = [];
|
|
208
|
+
let curr = outerCall;
|
|
209
|
+
while (curr && curr !== rootFlow.call) {
|
|
210
|
+
if (typescript_1.default.isCallExpression(curr)) {
|
|
211
|
+
const expr = curr.expression;
|
|
212
|
+
if (typescript_1.default.isPropertyAccessExpression(expr)) {
|
|
213
|
+
chain.unshift({
|
|
214
|
+
methodName: expr.name.text,
|
|
215
|
+
args: Array.from(curr.arguments),
|
|
216
|
+
callExpr: curr,
|
|
217
|
+
});
|
|
218
|
+
curr = expr.expression;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
else if (typescript_1.default.isPropertyAccessExpression(curr)) {
|
|
224
|
+
curr = curr.expression;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const properties = {};
|
|
231
|
+
const nodes = [];
|
|
232
|
+
const edges = [];
|
|
233
|
+
const unresolved = [];
|
|
234
|
+
let currentHeadIds = [];
|
|
235
|
+
function addNode(detected) {
|
|
236
|
+
if (detected.kind === 'node') {
|
|
237
|
+
let nodeToAdd = detected.node;
|
|
238
|
+
// If referencing a known definition, merge definition range
|
|
239
|
+
if (detected.variableName && ctx.stepDefinitions.has(detected.variableName)) {
|
|
240
|
+
const def = ctx.stepDefinitions.get(detected.variableName);
|
|
241
|
+
nodeToAdd = {
|
|
242
|
+
...nodeToAdd,
|
|
243
|
+
name: def.name,
|
|
244
|
+
stepKind: def.stepKind,
|
|
245
|
+
location: def.location,
|
|
246
|
+
declarationRange: def.declarationRange,
|
|
247
|
+
executionConfig: { ...def.executionConfig, ...nodeToAdd.executionConfig },
|
|
248
|
+
properties: { ...def.properties, ...nodeToAdd.properties },
|
|
249
|
+
specs: [...(def.specs || []), ...(nodeToAdd.specs || [])],
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
// Check if already in nodes
|
|
253
|
+
const existing = nodes.find((n) => n.id === nodeToAdd.id);
|
|
254
|
+
if (!existing) {
|
|
255
|
+
nodes.push(nodeToAdd);
|
|
256
|
+
}
|
|
257
|
+
return nodeToAdd.id;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
unresolved.push(detected.unresolved);
|
|
261
|
+
return detected.unresolved.id;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function appendSubflow(subflow) {
|
|
265
|
+
if (subflow.nodes.length === 0)
|
|
266
|
+
return;
|
|
267
|
+
for (const n of subflow.nodes) {
|
|
268
|
+
if (!nodes.some((existing) => existing.id === n.id)) {
|
|
269
|
+
nodes.push(n);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
for (const e of subflow.edges) {
|
|
273
|
+
if (!edges.some((existing) => existing.id === e.id)) {
|
|
274
|
+
edges.push(e);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const entryId = subflow.nodes[0].id;
|
|
278
|
+
const exitId = subflow.nodes[subflow.nodes.length - 1].id;
|
|
279
|
+
for (const headId of currentHeadIds) {
|
|
280
|
+
edges.push({
|
|
281
|
+
id: `edge_${headId}_to_${entryId}_${edges.length}`,
|
|
282
|
+
from: headId,
|
|
283
|
+
to: entryId,
|
|
284
|
+
type: 'sequential',
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
currentHeadIds = [exitId];
|
|
288
|
+
}
|
|
289
|
+
function connect(from, to, type = 'sequential', label) {
|
|
290
|
+
for (const id of new Set(from))
|
|
291
|
+
edges.push({
|
|
292
|
+
id: `edge_${id}_to_${to}_${type}_${edges.length}`, from: id, to, type, ...(label ? { label } : {}),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function junction(expr, name, suffix) {
|
|
296
|
+
const location = (0, mapper_1.getSourceRange)(ctx.sourceFile, expr);
|
|
297
|
+
const id = (0, mapper_1.generateDeterministicNodeId)(`${name}_${suffix}`, location);
|
|
298
|
+
nodes.push({ id, name, stepKind: 'custom', location, declarationRange: location });
|
|
299
|
+
return id;
|
|
300
|
+
}
|
|
301
|
+
// Expand combinators before step detection: their arguments are graph fragments,
|
|
302
|
+
// and options objects are metadata rather than executable steps.
|
|
303
|
+
function appendExpression(expr, resolving = new Set()) {
|
|
304
|
+
if (typescript_1.default.isParenthesizedExpression(expr))
|
|
305
|
+
return appendExpression(expr.expression, resolving);
|
|
306
|
+
if (typescript_1.default.isIdentifier(expr)) {
|
|
307
|
+
const definition = findCombinatorDefinition(expr.text);
|
|
308
|
+
if (definition && !resolving.has(expr.text)) {
|
|
309
|
+
appendExpression(definition, new Set([...resolving, expr.text]));
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
if (ctx.flowDefinitions.has(expr.text)) {
|
|
313
|
+
appendSubflow(ctx.flowDefinitions.get(expr.text));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (typescript_1.default.isArrayLiteralExpression(expr)) {
|
|
318
|
+
for (const child of expr.elements)
|
|
319
|
+
appendExpression(child, resolving);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (typescript_1.default.isCallExpression(expr)) {
|
|
323
|
+
const callee = expr.expression;
|
|
324
|
+
const kind = typescript_1.default.isIdentifier(callee) ? callee.text : typescript_1.default.isPropertyAccessExpression(callee) && ['Step', 'Func', 'Chain'].includes(callee.expression.getText(ctx.sourceFile)) ? callee.name.text : '';
|
|
325
|
+
if (['parallel', 'all', 'race', 'allSettled', 'branch', 'when', 'map'].includes(kind)) {
|
|
326
|
+
const fork = junction(expr, kind, 'fork');
|
|
327
|
+
const forkNode = nodes.find(n => n.id === fork);
|
|
328
|
+
forkNode.properties = { operation: kind };
|
|
329
|
+
for (const arg of expr.arguments)
|
|
330
|
+
if (typescript_1.default.isObjectLiteralExpression(arg))
|
|
331
|
+
Object.assign(forkNode.properties, (0, step_detector_1.extractDisplayProperties)(arg, ctx.sourceFile));
|
|
332
|
+
if (kind === 'when' || kind === 'branch')
|
|
333
|
+
forkNode.properties.selector = expr.arguments[0]?.getText(ctx.sourceFile) || 'context route';
|
|
334
|
+
if (kind === 'map')
|
|
335
|
+
forkNode.properties.items = expr.arguments[0]?.getText(ctx.sourceFile) || 'unknown';
|
|
336
|
+
connect(currentHeadIds, fork);
|
|
337
|
+
const exits = [];
|
|
338
|
+
const alternatives = [];
|
|
339
|
+
if (kind === 'branch') {
|
|
340
|
+
const mapIndex = expr.arguments[0] && typescript_1.default.isObjectLiteralExpression(expr.arguments[0]) ? 0 : 1;
|
|
341
|
+
const map = expr.arguments[mapIndex];
|
|
342
|
+
if (map && typescript_1.default.isObjectLiteralExpression(map)) {
|
|
343
|
+
for (const property of map.properties) {
|
|
344
|
+
if (typescript_1.default.isPropertyAssignment(property))
|
|
345
|
+
alternatives.push({ expression: property.initializer, label: property.name.getText(ctx.sourceFile).replace(/['"]/g, '') });
|
|
346
|
+
else if (typescript_1.default.isShorthandPropertyAssignment(property))
|
|
347
|
+
alternatives.push({ expression: property.name, label: property.name.text });
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const fallback = expr.arguments[mapIndex + 1];
|
|
351
|
+
if (fallback)
|
|
352
|
+
alternatives.push({ expression: fallback, label: 'default' });
|
|
353
|
+
}
|
|
354
|
+
else if (kind === 'when') {
|
|
355
|
+
if (expr.arguments[1])
|
|
356
|
+
alternatives.push({ expression: expr.arguments[1], label: 'then' });
|
|
357
|
+
if (expr.arguments[2])
|
|
358
|
+
alternatives.push({ expression: expr.arguments[2], label: 'else' });
|
|
359
|
+
else
|
|
360
|
+
exits.push(fork); // False predicate skips the then path.
|
|
361
|
+
}
|
|
362
|
+
else if (kind === 'map') {
|
|
363
|
+
if (expr.arguments[1])
|
|
364
|
+
alternatives.push({ expression: expr.arguments[1], label: 'each item' });
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
for (const arg of expr.arguments) {
|
|
368
|
+
if (typescript_1.default.isObjectLiteralExpression(arg))
|
|
369
|
+
continue;
|
|
370
|
+
for (const child of typescript_1.default.isArrayLiteralExpression(arg) ? arg.elements : [arg])
|
|
371
|
+
alternatives.push({ expression: child });
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
for (const alternative of alternatives) {
|
|
375
|
+
currentHeadIds = [fork];
|
|
376
|
+
const edgeStart = edges.length;
|
|
377
|
+
appendExpression(alternative.expression, resolving);
|
|
378
|
+
for (const edge of edges.slice(edgeStart))
|
|
379
|
+
if (edge.from === fork) {
|
|
380
|
+
edge.type = kind === 'branch' || kind === 'when' ? 'branch' : 'parallel';
|
|
381
|
+
edge.label = alternative.label || kind;
|
|
382
|
+
}
|
|
383
|
+
exits.push(...currentHeadIds);
|
|
384
|
+
}
|
|
385
|
+
const join = junction(expr, `${kind} join`, 'join');
|
|
386
|
+
connect(exits.length ? exits : [fork], join);
|
|
387
|
+
currentHeadIds = [join];
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (['flow', 'workflow', 'pipe', 'Pipeline'].includes(kind)) {
|
|
391
|
+
for (const child of expr.arguments) {
|
|
392
|
+
if (!typescript_1.default.isStringLiteral(child) && !typescript_1.default.isObjectLiteralExpression(child))
|
|
393
|
+
appendExpression(child, resolving);
|
|
394
|
+
}
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const detected = (0, step_detector_1.detectStepFromExpression)(expr, ctx.sourceFile);
|
|
399
|
+
if (detected) {
|
|
400
|
+
const id = addNode(detected);
|
|
401
|
+
connect(currentHeadIds, id);
|
|
402
|
+
currentHeadIds = [id];
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function findCombinatorDefinition(name) {
|
|
406
|
+
let result;
|
|
407
|
+
function visit(node) {
|
|
408
|
+
if (typescript_1.default.isVariableDeclaration(node) && node.name.getText(ctx.sourceFile) === name && node.initializer && typescript_1.default.isCallExpression(node.initializer) && ['parallel', 'all', 'race', 'allSettled', 'branch', 'when', 'map'].includes(typescript_1.default.isIdentifier(node.initializer.expression) ? node.initializer.expression.text : typescript_1.default.isPropertyAccessExpression(node.initializer.expression) && ['Step', 'Func', 'Chain'].includes(node.initializer.expression.expression.getText(ctx.sourceFile)) ? node.initializer.expression.name.text : ''))
|
|
409
|
+
result = node.initializer;
|
|
410
|
+
typescript_1.default.forEachChild(node, visit);
|
|
411
|
+
}
|
|
412
|
+
visit(ctx.sourceFile);
|
|
413
|
+
return result;
|
|
414
|
+
}
|
|
415
|
+
for (const arg of rootFlow.call.arguments) {
|
|
416
|
+
if (!typescript_1.default.isStringLiteral(arg) && !typescript_1.default.isNoSubstitutionTemplateLiteral(arg) && !typescript_1.default.isObjectLiteralExpression(arg))
|
|
417
|
+
appendExpression(arg);
|
|
418
|
+
}
|
|
419
|
+
// 2. Process chained methods (.then, .parallel, .branch, .fallback, .compensate)
|
|
420
|
+
for (const item of chain) {
|
|
421
|
+
const { methodName, args } = item;
|
|
422
|
+
applyFlowProperty(properties, methodName, args, ctx.sourceFile);
|
|
423
|
+
if (methodName === 'then') {
|
|
424
|
+
for (const arg of args)
|
|
425
|
+
appendExpression(arg);
|
|
426
|
+
}
|
|
427
|
+
else if (methodName === 'parallel') {
|
|
428
|
+
const arg = args[0];
|
|
429
|
+
const nextHeadIds = [];
|
|
430
|
+
if (arg && typescript_1.default.isArrayLiteralExpression(arg)) {
|
|
431
|
+
for (const el of arg.elements) {
|
|
432
|
+
if (typescript_1.default.isIdentifier(el) && ctx.flowDefinitions.has(el.text)) {
|
|
433
|
+
const sub = ctx.flowDefinitions.get(el.text);
|
|
434
|
+
if (sub.nodes.length > 0) {
|
|
435
|
+
for (const n of sub.nodes) {
|
|
436
|
+
if (!nodes.some((existing) => existing.id === n.id))
|
|
437
|
+
nodes.push(n);
|
|
438
|
+
}
|
|
439
|
+
for (const e of sub.edges) {
|
|
440
|
+
if (!edges.some((existing) => existing.id === e.id))
|
|
441
|
+
edges.push(e);
|
|
442
|
+
}
|
|
443
|
+
const entryId = sub.nodes[0].id;
|
|
444
|
+
const exitId = sub.nodes[sub.nodes.length - 1].id;
|
|
445
|
+
for (const headId of currentHeadIds) {
|
|
446
|
+
edges.push({
|
|
447
|
+
id: `edge_${headId}_to_${entryId}_parallel`,
|
|
448
|
+
from: headId,
|
|
449
|
+
to: entryId,
|
|
450
|
+
type: 'parallel',
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
nextHeadIds.push(exitId);
|
|
454
|
+
}
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
const detected = (0, step_detector_1.detectStepFromExpression)(el, ctx.sourceFile);
|
|
458
|
+
if (detected) {
|
|
459
|
+
const nextId = addNode(detected);
|
|
460
|
+
nextHeadIds.push(nextId);
|
|
461
|
+
for (const headId of currentHeadIds) {
|
|
462
|
+
edges.push({
|
|
463
|
+
id: `edge_${headId}_to_${nextId}_parallel`,
|
|
464
|
+
from: headId,
|
|
465
|
+
to: nextId,
|
|
466
|
+
type: 'parallel',
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (nextHeadIds.length > 0) {
|
|
473
|
+
currentHeadIds = nextHeadIds;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
else if (methodName === 'branch') {
|
|
477
|
+
const conditionExpr = args[0];
|
|
478
|
+
const trueArg = args[1];
|
|
479
|
+
const falseArg = args[2];
|
|
480
|
+
const conditionLabel = conditionExpr ? conditionExpr.getText(ctx.sourceFile) : undefined;
|
|
481
|
+
const nextHeadIds = [];
|
|
482
|
+
if (trueArg) {
|
|
483
|
+
const detected = (0, step_detector_1.detectStepFromExpression)(trueArg, ctx.sourceFile);
|
|
484
|
+
if (detected) {
|
|
485
|
+
const trueId = addNode(detected);
|
|
486
|
+
nextHeadIds.push(trueId);
|
|
487
|
+
for (const headId of currentHeadIds) {
|
|
488
|
+
edges.push({
|
|
489
|
+
id: `edge_${headId}_to_${trueId}_branch_true`,
|
|
490
|
+
from: headId,
|
|
491
|
+
to: trueId,
|
|
492
|
+
type: 'branch',
|
|
493
|
+
label: conditionLabel ? `true: ${conditionLabel}` : 'true',
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (falseArg) {
|
|
499
|
+
const detected = (0, step_detector_1.detectStepFromExpression)(falseArg, ctx.sourceFile);
|
|
500
|
+
if (detected) {
|
|
501
|
+
const falseId = addNode(detected);
|
|
502
|
+
nextHeadIds.push(falseId);
|
|
503
|
+
for (const headId of currentHeadIds) {
|
|
504
|
+
edges.push({
|
|
505
|
+
id: `edge_${headId}_to_${falseId}_branch_false`,
|
|
506
|
+
from: headId,
|
|
507
|
+
to: falseId,
|
|
508
|
+
type: 'branch',
|
|
509
|
+
label: conditionLabel ? `false: !(${conditionLabel})` : 'false',
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (nextHeadIds.length > 0) {
|
|
515
|
+
currentHeadIds = nextHeadIds;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
else if (methodName === 'fallback') {
|
|
519
|
+
const arg = args[0];
|
|
520
|
+
if (arg) {
|
|
521
|
+
const detected = (0, step_detector_1.detectStepFromExpression)(arg, ctx.sourceFile);
|
|
522
|
+
if (detected) {
|
|
523
|
+
const fallbackId = addNode(detected);
|
|
524
|
+
for (const headId of currentHeadIds) {
|
|
525
|
+
edges.push({
|
|
526
|
+
id: `edge_${headId}_to_${fallbackId}_fallback`,
|
|
527
|
+
from: headId,
|
|
528
|
+
to: fallbackId,
|
|
529
|
+
type: 'fallback',
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
currentHeadIds.push(fallbackId);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
else if (methodName === 'compensate') {
|
|
537
|
+
const arg = args[0];
|
|
538
|
+
if (arg) {
|
|
539
|
+
const detected = (0, step_detector_1.detectStepFromExpression)(arg, ctx.sourceFile);
|
|
540
|
+
if (detected) {
|
|
541
|
+
const compId = addNode(detected);
|
|
542
|
+
for (const headId of currentHeadIds) {
|
|
543
|
+
edges.push({
|
|
544
|
+
id: `edge_${headId}_to_${compId}_comp`,
|
|
545
|
+
from: headId,
|
|
546
|
+
to: compId,
|
|
547
|
+
type: 'compensation',
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
const routeEdges = new Map();
|
|
555
|
+
const normalizedEdges = [];
|
|
556
|
+
for (const edge of edges) {
|
|
557
|
+
const key = JSON.stringify([edge.from, edge.to, edge.type]);
|
|
558
|
+
const existing = routeEdges.get(key);
|
|
559
|
+
if (existing) {
|
|
560
|
+
if (edge.label && edge.label !== existing.label)
|
|
561
|
+
existing.label = existing.label ? `${existing.label} / ${edge.label}` : edge.label;
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
const normalized = { ...edge, id: `edge_${normalizedEdges.length}_${edge.from}_to_${edge.to}_${edge.type}` };
|
|
565
|
+
routeEdges.set(key, normalized);
|
|
566
|
+
normalizedEdges.push(normalized);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const diagnostics = runDiagnostics(nodes, normalizedEdges);
|
|
570
|
+
return {
|
|
571
|
+
uri: ctx.uri,
|
|
572
|
+
documentVersion: ctx.documentVersion,
|
|
573
|
+
workflowName: rootFlow.name,
|
|
574
|
+
properties,
|
|
575
|
+
nodes,
|
|
576
|
+
edges: normalizedEdges,
|
|
577
|
+
unresolved,
|
|
578
|
+
diagnostics,
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function runDiagnostics(nodes, edges) {
|
|
582
|
+
const diagnostics = [];
|
|
583
|
+
// 1. Detect duplicate node IDs
|
|
584
|
+
const idCounts = new Map();
|
|
585
|
+
for (const node of nodes) {
|
|
586
|
+
idCounts.set(node.id, (idCounts.get(node.id) || 0) + 1);
|
|
587
|
+
}
|
|
588
|
+
for (const node of nodes) {
|
|
589
|
+
if ((idCounts.get(node.id) || 0) > 1) {
|
|
590
|
+
diagnostics.push({
|
|
591
|
+
code: 'SIGLO_DUPLICATE_STEP_ID',
|
|
592
|
+
message: `Duplicate step ID detected: "${node.id}"`,
|
|
593
|
+
severity: 'error',
|
|
594
|
+
range: node.location,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
// 2. Detect cycles in graph edges
|
|
599
|
+
const adj = new Map();
|
|
600
|
+
for (const edge of edges) {
|
|
601
|
+
if (!adj.has(edge.from))
|
|
602
|
+
adj.set(edge.from, []);
|
|
603
|
+
adj.get(edge.from).push(edge.to);
|
|
604
|
+
}
|
|
605
|
+
const visited = new Set();
|
|
606
|
+
const recStack = new Set();
|
|
607
|
+
const cycleNodes = new Set();
|
|
608
|
+
function dfs(nodeId) {
|
|
609
|
+
visited.add(nodeId);
|
|
610
|
+
recStack.add(nodeId);
|
|
611
|
+
const neighbors = adj.get(nodeId) || [];
|
|
612
|
+
for (const neighbor of neighbors) {
|
|
613
|
+
if (!visited.has(neighbor)) {
|
|
614
|
+
dfs(neighbor);
|
|
615
|
+
}
|
|
616
|
+
else if (recStack.has(neighbor)) {
|
|
617
|
+
cycleNodes.add(neighbor);
|
|
618
|
+
cycleNodes.add(nodeId);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
recStack.delete(nodeId);
|
|
622
|
+
}
|
|
623
|
+
for (const node of nodes) {
|
|
624
|
+
if (!visited.has(node.id)) {
|
|
625
|
+
dfs(node.id);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (cycleNodes.size > 0) {
|
|
629
|
+
for (const node of nodes) {
|
|
630
|
+
if (cycleNodes.has(node.id)) {
|
|
631
|
+
diagnostics.push({
|
|
632
|
+
code: 'SIGLO_STATIC_CYCLE',
|
|
633
|
+
message: `Cyclic workflow dependency detected involving step "${node.name}"`,
|
|
634
|
+
severity: 'error',
|
|
635
|
+
range: node.location,
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
// 3. Detect side-effect steps lacking compensation in sagas
|
|
641
|
+
const hasAnyCompensationEdge = edges.some((e) => e.type === 'compensation');
|
|
642
|
+
if (hasAnyCompensationEdge) {
|
|
643
|
+
for (const node of nodes) {
|
|
644
|
+
if ((node.stepKind === 'http' || node.stepKind === 'crypto') && !node.executionConfig?.hasCompensation) {
|
|
645
|
+
const hasOutCompensation = edges.some((e) => e.from === node.id && e.type === 'compensation');
|
|
646
|
+
if (!hasOutCompensation) {
|
|
647
|
+
diagnostics.push({
|
|
648
|
+
code: 'SIGLO_MISSING_COMPENSATION',
|
|
649
|
+
message: `Saga step "${node.name}" performs side effects without a registered compensation step`,
|
|
650
|
+
severity: 'warning',
|
|
651
|
+
range: node.location,
|
|
652
|
+
fixAvailable: true,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return diagnostics;
|
|
659
|
+
}
|
|
660
|
+
function applyFlowProperty(properties, method, args, sourceFile) {
|
|
661
|
+
const arg = args[0];
|
|
662
|
+
if (!arg && method !== 'networkPolicy')
|
|
663
|
+
return;
|
|
664
|
+
const value = arg ? (typescript_1.default.isStringLiteral(arg) || typescript_1.default.isNoSubstitutionTemplateLiteral(arg) ? arg.text : arg.getText(sourceFile)) : 'undefined';
|
|
665
|
+
if (method === 'timeout')
|
|
666
|
+
properties.timeoutMs = value;
|
|
667
|
+
else if (method === 'version')
|
|
668
|
+
properties.workflowVersion = value;
|
|
669
|
+
else if (method === 'monitor')
|
|
670
|
+
properties.monitor = value;
|
|
671
|
+
else if (method === 'networkPolicy') {
|
|
672
|
+
for (const key of Object.keys(properties))
|
|
673
|
+
if (key.startsWith('network.'))
|
|
674
|
+
delete properties[key];
|
|
675
|
+
const defaults = { 'network.enabled': 'true', 'network.defaultPolicy': 'deny', 'network.allowedHosts': 'No hosts configured', 'network.allowLocalhost': 'false' };
|
|
676
|
+
if (!arg || typescript_1.default.isIdentifier(arg) && arg.text === 'undefined')
|
|
677
|
+
Object.assign(properties, defaults);
|
|
678
|
+
else if (typescript_1.default.isObjectLiteralExpression(arg)) {
|
|
679
|
+
if (!arg.properties.some(prop => typescript_1.default.isSpreadAssignment(prop)))
|
|
680
|
+
Object.assign(properties, defaults);
|
|
681
|
+
for (const prop of arg.properties) {
|
|
682
|
+
if (typescript_1.default.isSpreadAssignment(prop))
|
|
683
|
+
properties['network.source'] = value;
|
|
684
|
+
if (typescript_1.default.isPropertyAssignment(prop) || typescript_1.default.isShorthandPropertyAssignment(prop)) {
|
|
685
|
+
const key = prop.name.getText(sourceFile).replace(/['"]/g, '');
|
|
686
|
+
if (['enabled', 'defaultPolicy', 'allowedHosts', 'allowLocalhost', 'allowUnixSockets'].includes(key)) {
|
|
687
|
+
const expression = typescript_1.default.isPropertyAssignment(prop) ? prop.initializer : prop.name;
|
|
688
|
+
properties[`network.${key}`] = typescript_1.default.isArrayLiteralExpression(expression) ? expression.elements.map(e => typescript_1.default.isStringLiteral(e) ? e.text : e.getText(sourceFile)).join('\n') || 'No entries' : typescript_1.default.isStringLiteral(expression) ? expression.text : expression.getText(sourceFile);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
else
|
|
694
|
+
properties['network.source'] = value;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
//# sourceMappingURL=workflow-detector.js.map
|