import type { WorkflowDetail, WorkflowTask } from "../types.js"; import type { NodeScriptLintReport, WorkflowScriptLintReport, ScriptIssue } from "./types.js"; import { lintJavaScriptCode } from "./lint-js.js"; import { lintMvelCode, collectAllWorkflowNodeNames } from "./lint-mvel.js"; /** * 扫描并全面静态校验工作流中的全部代码节点(JS 与 MVEL) */ export function lintWorkflowScripts( workflow: WorkflowDetail, targetNodeQuery?: string ): WorkflowScriptLintReport { const tasks = workflow.tasks || []; const allNodeNames = collectAllWorkflowNodeNames(tasks); const reports: NodeScriptLintReport[] = []; function traverse(list: WorkflowTask[]) { for (const task of list) { if (!task) continue; if (task.type === "code" || task.type === "code-js2") { if (!targetNodeQuery || task.name === targetNodeQuery || task.label === targetNodeQuery || task.displayName === targetNodeQuery) { const rawCode = (task as any).code || ""; const lineCount = rawCode ? rawCode.split(/\r?\n/).length : 0; const mappings = Array.isArray((task as any).mapping) ? (task as any).mapping : []; const boundVariables: Array<{ key: string; upstreamTarget?: string }> = mappings.map((m: any) => ({ key: m.key, upstreamTarget: m.value, })); let issues: ScriptIssue[] = []; let hasReturn = false; let referencedNodes: Array<{ name: string; exists: boolean }> = []; if (task.type === "code-js2") { const paramNames = boundVariables.map((b) => b.key); const res = lintJavaScriptCode(rawCode, paramNames); issues = res.issues; hasReturn = res.hasReturn; } else { const res = lintMvelCode(rawCode, allNodeNames); issues = res.issues; hasReturn = res.hasReturn; referencedNodes = res.referencedNodes; } const hasErrors = issues.some((i) => i.level === "error"); reports.push({ nodeName: task.name, displayName: task.displayName || undefined, label: task.label || undefined, type: task.type, codeLength: rawCode.length, lineCount, hasReturn, boundVariables, referencedNodes, issues, valid: !hasErrors, }); } } if (Array.isArray(task.tasks) && task.tasks.length > 0) { traverse(task.tasks); } } } traverse(tasks); const validCount = reports.filter((r) => r.valid).length; const invalidCount = reports.length - validCount; return { pipelineId: workflow.id, label: workflow.label, totalCodeNodes: reports.length, validCount, invalidCount, reports, allValid: invalidCount === 0, }; }