Files
hose-use/src/domains/platform/workflow/script/lint-mvel.ts
T

179 lines
5.3 KiB
TypeScript

import type { WorkflowTask } from "../../types.js";
import type { ScriptIssue } from "./types.js";
import { hasMvelFinalExpression } from "./mvel-subset.js";
/** Mask literals/comments without moving offsets used by diagnostics. */
function scanMvelCode(code: string, issues: ScriptIssue[]): string {
const masked = code.split("");
let mode: "code" | "line" | "block" | "'" | '"' = "code";
let start = { line: 1, column: 1 };
let line = 1;
let column = 1;
let escaped = false;
for (let i = 0; i < code.length; i++) {
const ch = code[i];
const next = code[i + 1];
if (mode === "code") {
if (ch === "'" || ch === '"') {
mode = ch;
start = { line, column };
masked[i] = " ";
} else if (ch === "/" && (next === "/" || next === "*")) {
mode = next === "/" ? "line" : "block";
start = { line, column };
masked[i] = masked[i + 1] = " ";
i++;
column++;
}
} else {
if (ch !== "\n" && ch !== "\r") masked[i] = " ";
if (mode === "line") {
if (ch === "\n" || ch === "\r") mode = "code";
} else if (mode === "block") {
if (ch === "*" && next === "/") {
masked[++i] = " ";
column++;
mode = "code";
}
} else if (escaped) {
escaped = false;
} else if (ch === "\\") {
escaped = true;
} else if (ch === mode) {
mode = "code";
}
}
if (ch === "\n") { line++; column = 1; }
else column++;
}
if (mode === "block" || mode === "'" || mode === '"') {
issues.push({
level: "error",
...start,
rule: mode === "block" ? "unterminated-comment" : "unterminated-string",
message: mode === "block" ? "未闭合的块注释" : `未闭合的字符串: ${mode}`,
});
}
return masked.join("");
}
/**
* 静态检查 MVEL (code) 括号闭合与上游节点引用有效性
*/
export function lintMvelCode(
code: string,
existingNodeNames?: Set<string>
): {
issues: ScriptIssue[];
referencedNodes: Array<{ name: string; exists: boolean }>;
hasReturn: boolean;
} {
const issues: ScriptIssue[] = [];
const referencedNodes: Array<{ name: string; exists: boolean }> = [];
const trimmed = code.trim();
if (!trimmed) {
issues.push({
level: "error",
message: "MVEL 代码内容为空,合思调度时将报错",
rule: "empty-script",
});
return { issues, referencedNodes, hasReturn: false };
}
const executableCode = scanMvelCode(code, issues);
const hasRet = /\breturn\b/.test(executableCode);
if (!hasRet && !hasMvelFinalExpression(code)) {
issues.push({
level: "warning",
message: "MVEL 代码中未发现显式 return 或可确定的末尾结果表达式;请确认所需结果位于脚本末尾",
rule: "missing-return",
});
}
// 1. 括号与引号对称性平衡检查
const stack: Array<{ char: string; line: number }> = [];
const lines = executableCode.split(/\r?\n/);
for (let l = 0; l < lines.length; l++) {
const lineText = lines[l];
// 忽略单行注释,以及扫描器已屏蔽的字符串与块注释。
const cleanLine = lineText;
for (let c = 0; c < cleanLine.length; c++) {
const ch = cleanLine[c];
if (ch === "{" || ch === "(" || ch === "[") {
stack.push({ char: ch, line: l + 1 });
} else if (ch === "}" || ch === ")" || ch === "]") {
const top = stack.pop();
const expected = ch === "}" ? "{" : ch === ")" ? "(" : "[";
if (!top || top.char !== expected) {
issues.push({
level: "error",
line: l + 1,
column: c + 1,
message: `括号不匹配: 遇到闭合 '${ch}',但预期匹配 '${expected}'`,
rule: "unmatched-brackets",
});
}
}
}
}
while (stack.length > 0) {
const unclosed = stack.pop()!;
issues.push({
level: "error",
line: unclosed.line,
message: `未闭合的括号: '${unclosed.char}' 缺少对应的闭合括号`,
rule: "unclosed-bracket",
});
}
// 2. 提取引用的上游节点标识并比对有效性
// 合思节点命名规范形如 action_xxx, control_xxx, trigger_xxx
const nodeRefRegex = /\b(action_[a-zA-Z0-9_]+|control_[a-zA-Z0-9_]+|trigger_[a-zA-Z0-9_]+)\b/g;
const foundRefs = new Set<string>();
let match: RegExpExecArray | null;
while ((match = nodeRefRegex.exec(executableCode)) !== null) {
foundRefs.add(match[1]);
}
for (const ref of foundRefs) {
const exists = existingNodeNames ? existingNodeNames.has(ref) : true;
referencedNodes.push({ name: ref, exists });
if (!exists) {
issues.push({
level: "error",
message: `引用的上游节点 [${ref}] 在工作流中不存在(可能已被重命名或移除)`,
rule: "dangling-node-reference",
});
}
}
return { issues, referencedNodes, hasReturn: hasRet };
}
/**
* 递归收集工作流任务树中所有节点标识
*/
export function collectAllWorkflowNodeNames(tasks: WorkflowTask[]): Set<string> {
const names = new Set<string>();
function walk(list: WorkflowTask[]) {
for (const t of list) {
if (!t) continue;
if (t.name) names.add(t.name);
if (Array.isArray(t.tasks) && t.tasks.length > 0) {
walk(t.tasks);
}
}
}
walk(tasks);
return names;
}