3987b8d7fe
- 此前全树 0 处全局错误处理器:装 app.config.errorHandler + window error + unhandledrejection, 每条都留可诊断记录、界面只出中文、处理器自身异常不重抛也不白屏(9 例判据)。 - safeHttpUrl 未覆盖的 3 处(两个品牌 Tab 与 useVersionUpdate 直装服务端 file_url)收口; 令牌键 aiimage_auth_token 由 4 处各自定义 + 2 处内联收成唯一定义点。 - 「localStorage 有 uid 就免校验」改走 auth-gate 单点,剩余例外台账化且同口径必须能抓到 评审点名的原写法(6 例);卸载配对 3 处补齐 + 全树判据(7 例)。 - task-status-text 把 CANCELLED 显示成"执行中"改对,CANCELED/CANCELLED 两拼写都映射(4 例)。 - 主审计补的四处修复:新判据里两处拿数组和 0 比(怎么写都红);静默 catch 的"用户可见出口" 符号表认不出本仓 userNotice 与注入式 notify,把真报了用户的 catch 判成静默; 新错误出口带来的 2 个 try/catch 按棘轮合同写理由进基线(296→298,silent 一格没涨)。 - 验收:node --test 820 例 0 红、npx vue-tsc --noEmit rc=0。
107 lines
4.3 KiB
TypeScript
107 lines
4.3 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 那一套,加上本仓自己的提示出口 `userNotice`,
|
||
* 以及注入式提示句柄(`app-error.ts` 的 `deps.notify(...)`——生产里传的就是 userNotice,
|
||
* 扫描器看不穿注入,于是把"报了用户"误判成"只 log",这是尺子的盲区而不是代码的静默)。
|
||
* 放宽的边界由 tests/catch-silence-ratchet.test.ts 的分母自证兜着:
|
||
* api-secret-store.ts 与 DesktopLoginPage.vue 的静默数必须仍然 >0,抓不到就是这条规则放得太开。 */
|
||
const TOAST_SYMBOLS = new Set(['ElMessage', 'ElNotification', 'ElMessageBox', 'userNotice', 'notify'])
|
||
|
||
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 }
|
||
}
|