aea5614a8b
useTaskProgressLoop 的 DEFAULT_TERMINAL 只认 SUCCESS|FAILED,而 COMPLETED≡SUCCESS
(task-status-text 把两者都映射成「已完成」,看板 SUCCESS_STATUSES_ORDERED 也含两者)。
收到 COMPLETED 不退出的轮询会一直空转到瞬断预算耗尽——这形状先前在跟价与风控两页踩过。
唯一被这次改宽影响的页面是 BrandCollectDataTab(它不传 isTerminal);BrandPublishTab
传自己那份含 COMPLETED+CANCELLED 的判定,行为不变,已用单独一条钉子钉住。
判据原先是"变量名恰为 status"的源码正则,同形异名全 src 有 16 处抓不到,等于恒绿;
现在用 typescript(node_modules 里已有)做 AST 尺子,并把"旧正则对三条反例失明"写进断言。
ShopMatch 的 waitForScheduledTaskStageExit 等的是阶段不是任务终态,为此给骨架加了可选
exitOn,保住它原有语义、未知状态不会挂死。
口径更正:窄集合"全 src 应为 0"不成立——实测 20 处/8 文件是 fileStatus(结果文件维度)、
loop-run 带 STOPPED 的词表、显示与下载判定,不同维度不该混进一本账;改成迁移面硬 0 +
逐文件台账只许减。catch 同理:grep 'catch (' 的 292 数不到无绑定 catch {,AST 口径 295/84。
103 lines
3.9 KiB
TypeScript
103 lines
3.9 KiB
TypeScript
/**
|
||
* catch 静默扫描器(tests 工具层,判据在 tests/catch-silence-ratchet.test.ts)。
|
||
*
|
||
* 口径(与四面 9 分口径文档一致,逐条落到 AST 上,不靠 grep 数行数):
|
||
* 一个 catch 子句算"静默",当且仅当
|
||
* ① 块体是空的;或
|
||
* ② 块体第一条语句是 console.* 调用,且整块里没有 throw、也没有 ElMessage/ElNotification 这类用户可见提示。
|
||
* 其余形状(只 catch 后什么都不做但赋了个值、写了注释、调了别的函数)都**不**算——棘轮要能钉得住,
|
||
* 口径就得是可复算的,不能靠人临场判断。
|
||
*
|
||
* 与文档基线 292/81 的口径差:那张表的"分母与 `grep -c 'catch ('` 对齐",而 grep 数不到
|
||
* 无绑定的 `catch {`(本仓有)与 `} catch (e) {` 单行紧凑写法;本扫描器按 AST 数 CatchClause,
|
||
* 实测 296 块 / 84 静默。**这里钉 296/84 这条实扫基线**,只许减不许增。
|
||
*/
|
||
import { createRequire } from 'node:module'
|
||
import { extractScript, parseModule } from './task-terminal-ast-ruler.ts'
|
||
|
||
const require = createRequire(import.meta.url)
|
||
const ts = require('typescript')
|
||
|
||
export interface SilentCatch {
|
||
file: string
|
||
line: number
|
||
/** 'empty'=空块;'console-only'=只往控制台打了一句 */
|
||
kind: 'empty' | 'console-only'
|
||
snippet: string
|
||
}
|
||
|
||
export interface CatchScan {
|
||
file: string
|
||
total: number
|
||
silent: SilentCatch[]
|
||
}
|
||
|
||
/** 用户可见提示的符号名(本仓只有 Element Plus 这一套)。 */
|
||
const TOAST_SYMBOLS = new Set(['ElMessage', 'ElNotification', 'ElMessageBox'])
|
||
|
||
function walk(node: any, cb: (n: any) => void): void {
|
||
cb(node)
|
||
ts.forEachChild(node, (child: any) => walk(child, cb))
|
||
}
|
||
|
||
function firstStatementIsConsole(node: any): boolean {
|
||
if (!ts.isExpressionStatement(node)) return false
|
||
const call = node.expression
|
||
if (!ts.isCallExpression(call)) return false
|
||
const callee = call.expression
|
||
if (!ts.isPropertyAccessExpression(callee)) return false
|
||
const base = callee.expression
|
||
return ts.isIdentifier(base) && base.text === 'console'
|
||
}
|
||
|
||
/** 块里有没有"响"的出口:throw(语句或表达式两支)或任意 ElMessage.* / 裸 ElMessage 引用。 */
|
||
function hasLoudExit(body: any): boolean {
|
||
let loud = false
|
||
walk(body, (n: any) => {
|
||
if (ts.isThrowStatement(n) || n.kind === ts.SyntaxKind.ThrowExpression) loud = true
|
||
if (ts.isIdentifier(n) && TOAST_SYMBOLS.has(n.text)) loud = true
|
||
})
|
||
return loud
|
||
}
|
||
|
||
export function scanCatchSilence(code: string, file: string): CatchScan {
|
||
const { script, lineOffset } = extractScript(code, file)
|
||
const sf = parseModule(script, file)
|
||
let total = 0
|
||
const silent: SilentCatch[] = []
|
||
walk(sf, (node: any) => {
|
||
if (!ts.isCatchClause(node)) return
|
||
total += 1
|
||
const block = node.block
|
||
const line = sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1 + lineOffset
|
||
const snippet = script.slice(node.getStart(sf), block.getEnd()).replace(/\s+/g, ' ').trim()
|
||
if (block.statements.length === 0) {
|
||
silent.push({ file, line, kind: 'empty', snippet })
|
||
return
|
||
}
|
||
if (!firstStatementIsConsole(block.statements[0])) return
|
||
if (hasLoudExit(block)) return
|
||
silent.push({ file, line, kind: 'console-only', snippet })
|
||
})
|
||
return { file, total, silent }
|
||
}
|
||
|
||
/** 把逐文件结果汇成一个可对比的台账:`相对路径 -> 静默处数`。 */
|
||
export function toLedger(scans: CatchScan[]): Record<string, number> {
|
||
const ledger: Record<string, number> = {}
|
||
for (const s of scans) if (s.silent.length) ledger[s.file] = s.silent.length
|
||
return ledger
|
||
}
|
||
|
||
export function totals(scans: CatchScan[]): { blocks: number; silent: number; byFile: number } {
|
||
let blocks = 0
|
||
let silent = 0
|
||
let byFile = 0
|
||
for (const s of scans) {
|
||
blocks += s.total
|
||
silent += s.silent.length
|
||
if (s.silent.length) byFile += 1
|
||
}
|
||
return { blocks, silent, byFile }
|
||
}
|