修改特殊注码和打注一体的data格式

This commit is contained in:
2026-11-14 16:21:34 +08:00
parent 46cbe888ac
commit eade886d0e
19 changed files with 657 additions and 156 deletions
+43
View File
@@ -1,3 +1,4 @@
import * as acorn from 'acorn'
import type { StrategyInstance } from '../bridge/contracts'
/**
@@ -19,3 +20,45 @@ export function hasNoEditableRows(instance: StrategyInstance): boolean {
const rows = (data as { rows?: unknown }).rows
return Array.isArray(rows) && rows.length === 0
}
/**
* 剥掉策略代码里的注释(保存流程提交前用,只清文本、不改语义)。
*
* **不能用正则** —— 字符串 / 正则字面量 / 模板串里的 `//`、`/*` 会被误伤
* (`"http://x"`、`/a\/b/`)。用 acorn 解析、读注释区间,按 offset 删除。
*
* ⚠️ 解析失败 ⇒ 原样返回。剥离是清理,不是校验 —— 代码能不能跑由 native 判。
* ⚠️ 块注释含换行时必须换成等量换行:既防 `return` 后跟含换行的块注释再接
* `1` 时粘成 `return1`(块注释内的换行在 ASI 里算行终止符),
* 也让 native 报错行号对得上。
*
* @param code 完整 JS 源码字符串(含 IIFE 外壳)。
* @returns 去掉注释的源码;无注释 / 解析失败 ⇒ 原样返回。
*/
export function stripJsComments(code: string): string {
const comments: Array<{ block: boolean; start: number; end: number; text: string }> = []
try {
acorn.parse(code, {
ecmaVersion: 'latest',
sourceType: 'script',
allowReturnOutsideFunction: true,
onComment: (block: boolean, text: string, start: number, end: number) =>
comments.push({ block, start, end, text }),
})
} catch {
return code
}
if (comments.length === 0) return code
let out = ''
let cursor = 0
for (const c of comments) {
out += code.slice(cursor, c.start)
if (c.block) {
const newlines = (c.text.match(/\n/g) ?? []).length
out += newlines > 0 ? '\n'.repeat(newlines) : ' '
}
cursor = c.end
}
return out + code.slice(cursor)
}