fix(workflow): 收敛并发/守卫/return 三处判定,修复漏告警与误判
并发保护此前只存在于 CLI 调用点(node.ts 自己重读比对),SDK 出口 updateWorkflow 完全没有,
任何绕过 CLI 的调用方都会静默覆盖他人的修改:
- updateWorkflow 第三参数支持 `{ platformId, expectedVersion }` 对象形式(字符串写法向后兼容),
提供 expectedVersion 时提交前重读远端比对,不一致抛 ValidationError(WORKFLOW_VERSION_CONFLICT);
- node.ts saveNodeChanges 改为复用该能力,删除重复实现,CLI 与 SDK 共用一条路径;
- 测试改为伪造 Piper 客户端而非 spy updates,覆盖:期望版本一致/不一致、旧字符串写法、
无期望值时不额外读取、chooses 部分失败。
状态守卫判定(UNSAFE_SKIP_WITHOUT_STATUS_GUARD)此前把条件树 JSON.stringify 后用子串包含判断,
只要任意位置出现节点名且出现 $status 即判为已守卫,短节点名会普遍误命中,
规则因此静默失效:
- 改为收集条件内的字符串,取 ${a.b} / a.b 形式的引用目标后整体比对 `<节点名>.$status|status|result|data`;
- 同时统一了 combineFilters/filters/conditions/inputs 的取值入口,消除各处顺序不一致;
- 新增四条回归用例,含"节点名子串不得误判为守卫"。
return 识别此前只跳过整行注释,行尾 `// return`、块注释内部行与字符串里的 "return"
都被当真,missing-return 告警永不触发:新增 stripCommentsAndStrings 处理行注释、
块注释、单/双引号与模板串(含转义),新增专项测试。
同步更新 management、script 与 workflow 三处模块 README。
This commit is contained in:
1 parent
1ee46fd79f
commit
a0058c1c66
10 files changed
+380
-40
No files matched your search
@@ -17,6 +17,8 @@
|
||||
|
||||
JavaScript 在独立 QuickJS 运行时中执行,设置超时、内存及输入/输出、日志限制;输入同时提供变量和 `inputs` 命名空间。求值仅支持同步结果,不共享宿主构造器,不把本地求值视为服务端脚本执行的完整替代。
|
||||
|
||||
return 识别先剔除注释与字符串字面量(`stripCommentsAndStrings`)再匹配关键字,避免行尾注释、块注释内部行和字符串里的 `return` 让 `missing-return` 告警失效。该能力同样需要增加回归案例。
|
||||
|
||||
MVEL 本地支持范围以 `mvel-subset.ts` 为准,不等价完整 Java/MVEL 运行时。静态检查与本地求值分别报告问题和结果;不能用“本地通过”推断租户权限、服务端上下文或业务写入一定成功。
|
||||
|
||||
修改语法支持时增加支持/拒绝的回归案例,并保持沙箱资源释放。测试见 [index.test.ts](./index.test.ts)、[mvel-subset.test.ts](./mvel-subset.test.ts);命令适配在 [script.ts](../../../../cli/commands/workflow/script.ts),业务脚本编写参考位于 [MVEL 产品技能](../../../../../skills/hose-use/multi/ekuaibao-mvel/SKILL.md)。
|
||||
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { hasReturnStatement, stripCommentsAndStrings, lintJavaScriptCode } from "./lint-js.js";
|
||||
|
||||
/**
|
||||
* return 检测曾经只跳过整行注释,行尾注释、块注释内部行和字符串里的 "return"
|
||||
* 都会让 missing-return 告警彻底失效。以下用例固定住这一修复。
|
||||
*/
|
||||
describe("JavaScript return detection", () => {
|
||||
it("finds real return statements", () => {
|
||||
expect(hasReturnStatement("return a + b;")).toBe(true);
|
||||
expect(hasReturnStatement("const x = 1;\nif (x) { return x; }\nreturn 0;")).toBe(true);
|
||||
});
|
||||
|
||||
it("ignores returns in trailing comments", () => {
|
||||
expect(hasReturnStatement("const total = sum;\n// 记得 return 结果给下游")).toBe(false);
|
||||
});
|
||||
|
||||
it("ignores returns inside block comments", () => {
|
||||
expect(hasReturnStatement("/*\nreturn 1;\n*/\nprepare();")).toBe(false);
|
||||
expect(hasReturnStatement("/* return 1 */ prepare();")).toBe(false);
|
||||
});
|
||||
|
||||
it("ignores returns inside string literals", () => {
|
||||
expect(hasReturnStatement('log("return value: ");')).toBe(false);
|
||||
expect(hasReturnStatement("log('return value');")).toBe(false);
|
||||
expect(hasReturnStatement("log(`return ${x}`);")).toBe(false);
|
||||
});
|
||||
|
||||
it("does not treat identifiers containing return as a return", () => {
|
||||
expect(hasReturnStatement("returnValue = 1;")).toBe(false);
|
||||
expect(hasReturnStatement("const return_of_day = 3;")).toBe(false);
|
||||
});
|
||||
|
||||
it("still detects a return that follows commented-out code", () => {
|
||||
expect(hasReturnStatement("// return 1;\nreturn 2;")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("stripCommentsAndStrings", () => {
|
||||
it("keeps code characters and blanks out comment bodies", () => {
|
||||
expect(stripCommentsAndStrings("// c\nkeep")).toBe(`${" ".repeat(4)}\nkeep`);
|
||||
const blockStripped = stripCommentsAndStrings("/* x */ keep");
|
||||
expect(blockStripped).toMatch(/^\s+keep$/);
|
||||
});
|
||||
|
||||
it("handles escaped quotes inside strings", () => {
|
||||
const stripped = stripCommentsAndStrings('"a\\"b" + x');
|
||||
expect(stripped).not.toContain('"');
|
||||
expect(stripped).toMatch(/\+\s*x$/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("lintJavaScriptCode missing-return reporting", () => {
|
||||
it("warns when only comments mention return", () => {
|
||||
const { issues } = lintJavaScriptCode("// 记得 return 值\nnormalize(data);");
|
||||
expect(issues.map((i) => i.rule)).toContain("missing-return");
|
||||
});
|
||||
|
||||
it("stays quiet when a real return exists", () => {
|
||||
const { issues, hasReturn } = lintJavaScriptCode("normalize(data);\nreturn data;");
|
||||
expect(hasReturn).toBe(true);
|
||||
expect(issues.map((i) => i.rule)).not.toContain("missing-return");
|
||||
});
|
||||
});
|
||||
@@ -1,20 +1,61 @@
|
||||
import type { ScriptIssue } from "./types.js";
|
||||
|
||||
type ScanState = "code" | "lineComment" | "blockComment" | "singleQuote" | "doubleQuote" | "template";
|
||||
|
||||
/**
|
||||
* 检查代码中是否存在 return 关键字(排除纯注释中的 return)
|
||||
* 剔除注释与字符串字面量,其余字符原样保留、被剔除部分用空格占位(保持偏移便于将来定位)。
|
||||
* 覆盖行注释、块注释、单/双引号与模板串(含转义)。正则字面量中的 return 仍可能误判,
|
||||
* 由后续语法解析兜底。
|
||||
*/
|
||||
export function hasReturnStatement(code: string): boolean {
|
||||
const lines = code.split(/\r?\n/);
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.startsWith("//") || trimmed.startsWith("/*") || trimmed.startsWith("*")) {
|
||||
continue;
|
||||
}
|
||||
if (/\breturn\b/.test(trimmed)) {
|
||||
return true;
|
||||
export function stripCommentsAndStrings(code: string): string {
|
||||
let output = "";
|
||||
let state: ScanState = "code";
|
||||
let index = 0;
|
||||
while (index < code.length) {
|
||||
const char = code[index]!;
|
||||
const next = code[index + 1];
|
||||
switch (state) {
|
||||
case "code":
|
||||
if (char === "/" && next === "/") { state = "lineComment"; output += " "; index += 2; continue; }
|
||||
if (char === "/" && next === "*") { state = "blockComment"; output += " "; index += 2; continue; }
|
||||
if (char === "'" || char === '"' || char === "`") {
|
||||
state = char === "'" ? "singleQuote" : char === '"' ? "doubleQuote" : "template";
|
||||
output += " ";
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
output += char;
|
||||
index += 1;
|
||||
continue;
|
||||
case "lineComment":
|
||||
if (char === "\n") { state = "code"; output += char; } else if (char !== "\r") { output += " "; }
|
||||
index += 1;
|
||||
continue;
|
||||
case "blockComment":
|
||||
if (char === "*" && next === "/") { state = "code"; output += " "; index += 2; continue; }
|
||||
output += char === "\n" ? char : " ";
|
||||
index += 1;
|
||||
continue;
|
||||
default: {
|
||||
if (char === "\\") { output += " "; index += 2; continue; }
|
||||
const closer = state === "singleQuote" ? "'" : state === "doubleQuote" ? '"' : "`";
|
||||
if (char === closer) { state = "code"; output += " "; index += 1; continue; }
|
||||
output += char === "\n" ? char : " ";
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断脚本是否真的写了 return。
|
||||
* 先前只跳过整行注释,行尾注释 `// return`、块注释内部行与字符串里的 "return"
|
||||
* 都会被当成真语句,导致 missing-return 告警永不触发。
|
||||
*/
|
||||
export function hasReturnStatement(code: string): boolean {
|
||||
return /\breturn\b/.test(stripCommentsAndStrings(code));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user