mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-16 15:43:57 -06:00
0fa0315a75
* fix(cron): reject malformed Code Mode scripts Reject invalid JavaScript syntax during cron add and update so CLI and agent callers receive an actionable location before persistence. Preserve the async Code Mode grammar, including top-level await and return. Fixes #118088. * fix(cron): keep enabled-only patches working for stored malformed scripts
35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import { parse, type Position, type Program } from "acorn";
|
|
|
|
type AcornSyntaxError = SyntaxError & { loc: Position };
|
|
|
|
type CodeModeScriptParseResult =
|
|
| { ok: true; program: Program }
|
|
| { ok: false; message: string; line: number; column: number };
|
|
|
|
/** Mirrors the worker's async-arrow body grammar so valid top-level await/return stay legal. */
|
|
export function buildCodeModeScriptParseSource(code: string): {
|
|
source: string;
|
|
codeOffset: number;
|
|
} {
|
|
const prefix = "(async () => {\n";
|
|
return { source: `${prefix}${code}\n})`, codeOffset: prefix.length };
|
|
}
|
|
|
|
export function parseCodeModeScriptSyntax(code: string): CodeModeScriptParseResult {
|
|
const { source } = buildCodeModeScriptParseSource(code);
|
|
try {
|
|
return {
|
|
ok: true,
|
|
program: parse(source, { ecmaVersion: "latest" }),
|
|
};
|
|
} catch (error) {
|
|
const syntaxError = error as AcornSyntaxError;
|
|
return {
|
|
ok: false,
|
|
message: syntaxError.message.replace(/ \(\d+:\d+\)$/u, ""),
|
|
line: syntaxError.loc.line - 1,
|
|
column: syntaxError.loc.column,
|
|
};
|
|
}
|
|
}
|