fix(终态): 消掉第二套任务终态词表,并把判据从正则换成 AST
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。
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* 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 }
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
/**
|
||||
* 终态词表的 AST 尺子(判据从"源码正则"升级来的那一半,跑在 node --test 里,无新依赖)。
|
||||
*
|
||||
* 为什么必须弃用正则:上一版尺子长这样
|
||||
* /===\s*'SUCCESS'\s*\|\|\s*status\s*===\s*'FAILED'\s*\)/
|
||||
* 它把"变量名恰好叫 status"和"两个比较挨在一起"当成了词表的定义。于是
|
||||
* const x = (s: string) => s === 'SUCCESS' || s === 'FAILED' ← 同形异名,抓不到
|
||||
* if (st === 'SUCCESS' || st === 'FAILED') return ← 同上
|
||||
* [TaskStatus.SUCCESS, TaskStatus.FAILED].includes(status) ← 数组形态,同上
|
||||
* 三类都从尺子底下滑过去,尺子就**恒绿**了——恒绿的判据比没有判据更糟,它让下一次抄袭变得安全。
|
||||
*
|
||||
* 现在按 AST 结构判:把 `||` 链(含嵌在数组里的 `.includes()` 形态)归约成"这条判断认得哪些状态词",
|
||||
* 只要同时认得 SUCCESS 与 FAILED、又不认得 COMPLETED,就是窄集合。不看变量名、不看排版、不数空格。
|
||||
*
|
||||
* 精度闸(防误红,误红的判据下次没人信):链里还出现 RUNNING/PENDING/SCHEDULED 这类**非终态**词的,
|
||||
* 是"这行要不要显示成执行中"的显示判断,不是"任务到没到终态"的等待判断,不计入。
|
||||
*/
|
||||
import { readFileSync, readdirSync, statSync } from 'node:fs'
|
||||
import { join, relative } from 'node:path'
|
||||
import { createRequire } from 'node:module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const ts = require('typescript')
|
||||
|
||||
/** 大写状态词的形状:SUCCESS / COMPLETED / DELETED_XXX;小写与普通标识符不算状态词。 */
|
||||
const STATUS_WORD = /^[A-Z][A-Z0-9_]{2,}$/
|
||||
|
||||
/** 与骨架一致:这两个词是"任务确实结束了"的最小充分对。 */
|
||||
const NARROW_PAIR = ['SUCCESS', 'FAILED']
|
||||
/** COMPLETED≡SUCCESS 的出处见 shared/utils/task-status-text.ts;出现它就不算窄集合。 */
|
||||
const WIDE_MARKER = 'COMPLETED'
|
||||
/** 非终态词:出现即说明这条判断不是"等终态",不计入窄集合。 */
|
||||
const NON_TERMINAL_WORDS = new Set([
|
||||
'RUNNING', 'PENDING', 'SCHEDULED', 'QUEUED', 'PAUSED', 'WAITING', 'PROCESSING', 'INIT', 'CREATED',
|
||||
])
|
||||
|
||||
export interface NarrowHit {
|
||||
/** 文件相对路径(相对仓库根,即 frontend-vue) */
|
||||
file: string
|
||||
line: number
|
||||
/** 命中的那条判断的源码片段(报错时给出可读证据,不只给个数字) */
|
||||
snippet: string
|
||||
/** 这条判断认得的状态词 */
|
||||
words: string[]
|
||||
}
|
||||
|
||||
function walk(node: any, cb: (n: any) => void): void {
|
||||
cb(node)
|
||||
ts.forEachChild(node, (child: any) => walk(child, cb))
|
||||
}
|
||||
|
||||
/** .vue 只取 `<script>` 段;顺带记下段首行号,好把命中位置折算回文件真实行。 */
|
||||
export function extractScript(code: string, file: string): { script: string; lineOffset: number } {
|
||||
if (!file.endsWith('.vue')) return { script: code, lineOffset: 0 }
|
||||
const blocks = [...code.matchAll(/<script\s+[^>]*>([\s\S]*?)<\/script>/g)]
|
||||
if (blocks.length !== 1) throw new Error(`${file} 的 <script> 段数是 ${blocks.length}(要求恰 1 段),尺子无法定位`)
|
||||
const m = blocks[0] as unknown as RegExpExecArray & { index: number }
|
||||
const scriptStart = m.index + m[0].indexOf(m[1])
|
||||
return { script: m[1], lineOffset: code.slice(0, scriptStart).split('\n').length - 1 }
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析失败必须**抛**,不许返回空数组。
|
||||
* 返回空数组="这文件没毛病",于是语法坏掉的文件的窄集合永久隐身(恒绿的第三条路)。
|
||||
*/
|
||||
export function parseModule(script: string, file: string): any {
|
||||
const sf = ts.createSourceFile(file, script, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
|
||||
const diags = sf.parseDiagnostics || []
|
||||
if (diags.length) {
|
||||
const first = diags[0]
|
||||
const { line } = sf.getLineAndCharacterOfPosition(first.start)
|
||||
throw new Error(`${file} 语法解析失败(第 ${line + 1} 行起 ${diags.length} 处):${ts.flattenDiagnosticMessageText(first.messageText, ' ')}`)
|
||||
}
|
||||
return sf
|
||||
}
|
||||
|
||||
function unwrap(node: any): any {
|
||||
let n = node
|
||||
while (n && (ts.isAsExpression(n) || ts.isParenthesizedExpression(n)
|
||||
// `<TaskStatus.SUCCESS>` 老式尖括号断言与 `x!` 非空断言:按 kind 比,不依赖版本里的谓词函数名
|
||||
|| n.kind === ts.SyntaxKind.TypeAssertionExpression || n.kind === ts.SyntaxKind.NonNullExpression)) n = n.expression
|
||||
return n
|
||||
}
|
||||
|
||||
/** 字符串字面量与 `TaskStatus.SUCCESS` 这类枚举成员都算状态词来源。 */
|
||||
function statusWord(node: any): string | null {
|
||||
const n = unwrap(node)
|
||||
if (!n) return null
|
||||
if (ts.isStringLiteral(n) || ts.isNoSubstitutionTemplateLiteral(n)) return STATUS_WORD.test(n.text) ? n.text : null
|
||||
if (ts.isPropertyAccessExpression(n) && typeof n.name?.text === 'string' && STATUS_WORD.test(n.name.text)) return n.name.text
|
||||
return null
|
||||
}
|
||||
|
||||
function isBarBar(node: any): boolean {
|
||||
return ts.isBinaryExpression(node) && node.operatorToken?.kind === ts.SyntaxKind.BarBarToken
|
||||
}
|
||||
|
||||
function isIncludesCall(node: any): boolean {
|
||||
return ts.isCallExpression(node)
|
||||
&& ts.isPropertyAccessExpression(node.expression)
|
||||
&& node.expression.name?.text === 'includes'
|
||||
}
|
||||
|
||||
/** `.includes()` 的接收者若是数组字面量,就把它当作词表读出来。 */
|
||||
function includesArrayLiteral(node: any): any | null {
|
||||
if (!isIncludesCall(node)) return null
|
||||
const arr = unwrap(node.expression.expression)
|
||||
return arr && ts.isArrayLiteralExpression(arr) ? arr : null
|
||||
}
|
||||
|
||||
/** 沿 `||` 链收集状态词:`a === 'SUCCESS' || b === 'FAILED'` → {SUCCESS, FAILED}。 */
|
||||
function collectWords(node: any, out: Set<string>): void {
|
||||
if (isBarBar(node)) {
|
||||
collectWords(node.left, out)
|
||||
collectWords(node.right, out)
|
||||
return
|
||||
}
|
||||
if (ts.isBinaryExpression(node)
|
||||
&& (node.operatorToken?.kind === ts.SyntaxKind.EqualsEqualsEqualsToken || node.operatorToken?.kind === ts.SyntaxKind.EqualsEqualsToken)) {
|
||||
const word = statusWord(node.right) ?? statusWord(node.left)
|
||||
if (word) out.add(word)
|
||||
return
|
||||
}
|
||||
const arr = includesArrayLiteral(node)
|
||||
if (arr) for (const el of arr.elements) {
|
||||
const word = statusWord(el)
|
||||
if (word) out.add(word)
|
||||
}
|
||||
}
|
||||
|
||||
function isNarrow(words: Set<string>): boolean {
|
||||
if (!NARROW_PAIR.every((w) => words.has(w))) return false
|
||||
if (words.has(WIDE_MARKER)) return false
|
||||
for (const w of words) if (NON_TERMINAL_WORDS.has(w)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/** 数组字面量是不是"某某 .includes()" 的接收者——是的话由那条调用一起判,避免同一处报两遍。 */
|
||||
function isIncludesReceiver(node: any, parent: any): boolean {
|
||||
return !!parent && ts.isPropertyAccessExpression(parent) && parent.name?.text === 'includes' && parent.expression === node
|
||||
}
|
||||
|
||||
export function findNarrowTerminalSets(code: string, file: string): NarrowHit[] {
|
||||
const { script, lineOffset } = extractScript(code, file)
|
||||
const sf = parseModule(script, file)
|
||||
const hits: NarrowHit[] = []
|
||||
walk(sf, (node: any) => {
|
||||
let subject: any = null
|
||||
if (isBarBar(node)) {
|
||||
// 只判链顶:子链是父链的一部分,重复报会虚增处数
|
||||
if (node.parent && isBarBar(node.parent)) return
|
||||
subject = node
|
||||
} else if (includesArrayLiteral(node)) {
|
||||
subject = node
|
||||
} else if (ts.isArrayLiteralExpression(node) && !isIncludesReceiver(node, node.parent)) {
|
||||
subject = node
|
||||
}
|
||||
if (!subject) return
|
||||
|
||||
const words = new Set<string>()
|
||||
if (subject === node && isBarBar(subject)) {
|
||||
collectWords(subject, words)
|
||||
} else {
|
||||
const arr = subject === node && ts.isArrayLiteralExpression(subject) ? subject : includesArrayLiteral(subject)
|
||||
for (const el of arr.elements) {
|
||||
const word = statusWord(el)
|
||||
// 元素里混了非状态词(变量、数字)就不是词表,别猜
|
||||
if (!word) return
|
||||
words.add(word)
|
||||
}
|
||||
if (arr.elements.length === 0) return
|
||||
}
|
||||
if (!isNarrow(words)) return
|
||||
hits.push({
|
||||
file,
|
||||
line: sf.getLineAndCharacterOfPosition(subject.getStart(sf)).line + 1 + lineOffset,
|
||||
snippet: script.slice(subject.getStart(sf), subject.getEnd()).replace(/\s+/g, ' ').trim(),
|
||||
words: [...words],
|
||||
})
|
||||
})
|
||||
return hits
|
||||
}
|
||||
|
||||
/** 待扫文件:src 下的 .ts/.vue(.d.ts 是声明文件,不含判断逻辑,排掉)。 */
|
||||
export function listSourceFiles(rootDir: string): string[] {
|
||||
const out: string[] = []
|
||||
const stack = [rootDir]
|
||||
while (stack.length) {
|
||||
const dir = stack.pop() as string
|
||||
for (const name of readdirSync(dir)) {
|
||||
const p = join(dir, name)
|
||||
if (statSync(p).isDirectory()) { stack.push(p); continue }
|
||||
if (!/\.(ts|vue)$/.test(name)) continue
|
||||
if (/\.d\.ts$/.test(name)) continue
|
||||
out.push(p)
|
||||
}
|
||||
}
|
||||
return out.sort()
|
||||
}
|
||||
|
||||
export function readSource(relFromRoot: string, repoRoot: string): string {
|
||||
return readFileSync(join(repoRoot, relFromRoot), 'utf-8')
|
||||
}
|
||||
|
||||
/** 全库扫一遍;返回命中清单与扫过的文件数(分母要能自证,别只给一个数)。 */
|
||||
export function scanNarrowTerminalSets(repoRoot: string, relFiles: string[]): { hits: NarrowHit[]; filesWithHits: number } {
|
||||
const hits: NarrowHit[] = []
|
||||
let filesWithHits = 0
|
||||
for (const rel of relFiles) {
|
||||
const found = findNarrowTerminalSets(readSource(rel, repoRoot), rel)
|
||||
if (found.length) filesWithHits += 1
|
||||
hits.push(...found)
|
||||
}
|
||||
return { hits, filesWithHits }
|
||||
}
|
||||
|
||||
export function formatHits(hits: NarrowHit[]): string {
|
||||
return hits.map((h) => `${relative('.', h.file) || h.file}:${h.line} [${h.words.join(', ')}] ${h.snippet.slice(0, 90)}`).join('\n')
|
||||
}
|
||||
|
||||
/** 一条等待函数的形状(上一版尺子用字符串切函数体,名字一改就失效;这里按 AST 找)。 */
|
||||
export interface WaitFunctionShape {
|
||||
name: string
|
||||
line: number
|
||||
/** 函数体里调用到的被调名(标识符调用与 `a.b()` 的 b 都收) */
|
||||
calls: string[]
|
||||
/** 有没有自己写 `while (true)` 循环 */
|
||||
hasWhileTrue: boolean
|
||||
/** 有没有自己声明瞬断计数器(骨架外再数一份预算的标志) */
|
||||
declaresTransientCounter: boolean
|
||||
/** 有没有传 exitOn(阶段型等待的退出谓词) */
|
||||
passesExitOn: boolean
|
||||
/** 有没有传 terminal(页专属终态词表,例如巡店删除的 DELETED) */
|
||||
passesTerminal: boolean
|
||||
}
|
||||
|
||||
function calleeNames(node: any, out: Set<string>): void {
|
||||
if (ts.isCallExpression(node)) {
|
||||
const c = node.expression
|
||||
// 命名导入的别名(`waitForTaskTerminal as waitForTaskTerminalShared`)在调用点就是标识符本身
|
||||
if (ts.isIdentifier(c)) out.add(c.text)
|
||||
else if (ts.isPropertyAccessExpression(c) && c.name) out.add(c.name.text)
|
||||
}
|
||||
ts.forEachChild(node, (child: any) => calleeNames(child, out))
|
||||
}
|
||||
|
||||
/** 对象字面量的键名集合(含嵌套):骨架调用有没有传 exitOn / terminal 就看这个。 */
|
||||
function objectLiteralKeys(node: any, out: Set<string>): void {
|
||||
if (ts.isObjectLiteralExpression(node)) {
|
||||
for (const prop of node.properties) {
|
||||
const key = prop.name
|
||||
if (!key) continue
|
||||
if (ts.isIdentifier(key) || ts.isStringLiteral(key) || ts.isNumericLiteral(key)) out.add(key.text)
|
||||
}
|
||||
}
|
||||
ts.forEachChild(node, (child: any) => objectLiteralKeys(child, out))
|
||||
}
|
||||
|
||||
/**
|
||||
* 找出文件里所有名字以 `waitFor` 开头的函数声明,返回它们的形状。
|
||||
* 上一版判据是 `source.indexOf('async function waitForTaskTerminal(taskId: number)')` 切字符串,
|
||||
* 签名一改(加参数、改缩进、换行写)就找不到而静默放行;这里按 AST 认函数名。
|
||||
*/
|
||||
export function findWaitFunctions(code: string, file: string): WaitFunctionShape[] {
|
||||
const { script, lineOffset } = extractScript(code, file)
|
||||
const sf = parseModule(script, file)
|
||||
const out: WaitFunctionShape[] = []
|
||||
walk(sf, (node: any) => {
|
||||
if (!ts.isFunctionDeclaration(node)) return
|
||||
const name = node.name?.text
|
||||
if (typeof name !== 'string' || !name.startsWith('waitFor')) return
|
||||
const calls = new Set<string>()
|
||||
calleeNames(node, calls)
|
||||
let hasWhileTrue = false
|
||||
let declaresTransientCounter = false
|
||||
walk(node, (inner: any) => {
|
||||
if (ts.isWhileStatement(inner)) {
|
||||
const expr = inner.expression
|
||||
if (expr.kind === ts.SyntaxKind.TrueKeyword) hasWhileTrue = true
|
||||
}
|
||||
if (ts.isVariableDeclaration(inner)) {
|
||||
const t = name2text(inner.name)
|
||||
// 只认**可变**计数变量(`let transientErrorCount`);`const TRANSIENT = [...]` 是错误特征串表,不是自己数瞬断
|
||||
const list = inner.parent
|
||||
const isConst = !!list && (list.flags & ts.NodeFlags.Const) !== 0
|
||||
if (/transient/i.test(t) && !isConst) declaresTransientCounter = true
|
||||
}
|
||||
})
|
||||
const keys = new Set<string>()
|
||||
objectLiteralKeys(node, keys)
|
||||
out.push({
|
||||
name,
|
||||
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1 + lineOffset,
|
||||
calls: [...calls],
|
||||
hasWhileTrue,
|
||||
declaresTransientCounter,
|
||||
passesExitOn: keys.has('exitOn'),
|
||||
passesTerminal: keys.has('terminal'),
|
||||
})
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
function name2text(name: any): string {
|
||||
if (!name) return ''
|
||||
if (ts.isIdentifier(name)) return name.text
|
||||
return ''
|
||||
}
|
||||
|
||||
/** 页面喂给 useTaskProgressLoop 的终态配置形状。 */
|
||||
export interface ProgressLoopConfig {
|
||||
file: string
|
||||
line: number
|
||||
/** options 里有没有显式传 `isTerminal`;没传=这页吃的就是骨架那份词表 */
|
||||
passesIsTerminal: boolean
|
||||
/** 传了的话,值是本页哪个函数(只认标识符形式) */
|
||||
isTerminalRef: string | null
|
||||
/** 那个函数认得的状态词:从 `||` 链与数组词表里读出来 */
|
||||
isTerminalWords: string[]
|
||||
/** 那个函数是否直接委托给共用判定(委托时词表取骨架那份) */
|
||||
delegatesToShared: boolean
|
||||
}
|
||||
|
||||
function wordsOfExpressionContainer(node: any, out: Set<string>): void {
|
||||
if (isBarBar(node) || (ts.isBinaryExpression(node)
|
||||
&& (node.operatorToken?.kind === ts.SyntaxKind.EqualsEqualsEqualsToken || node.operatorToken?.kind === ts.SyntaxKind.EqualsEqualsToken))) {
|
||||
collectWords(node, out)
|
||||
}
|
||||
if (ts.isArrayLiteralExpression(node)) {
|
||||
for (const el of node.elements) {
|
||||
const word = statusWord(el)
|
||||
if (word) out.add(word)
|
||||
}
|
||||
}
|
||||
ts.forEachChild(node, (child: any) => wordsOfExpressionContainer(child, out))
|
||||
}
|
||||
|
||||
/**
|
||||
* 读出页面里 `useTaskProgressLoop({...})` 的终态配置。
|
||||
* 用途不是代替行为断言,而是让行为断言**用页面自己那份词表**去跑,
|
||||
* 免得测试里手抄一遍状态字面量(手抄的副本会和源码漂移,那才是真正的恒绿)。
|
||||
*/
|
||||
export function readProgressLoopConfigs(code: string, file: string, sharedPredicateNames: string[] = ['isTerminalTaskStatus']): ProgressLoopConfig[] {
|
||||
const { script, lineOffset } = extractScript(code, file)
|
||||
const sf = parseModule(script, file)
|
||||
const decls = new Map<string, any>()
|
||||
walk(sf, (n: any) => {
|
||||
if ((ts.isFunctionDeclaration(n) || ts.isArrowFunction(n)) && n.name && ts.isIdentifier(n.name)) decls.set(n.name.text, n)
|
||||
if (ts.isVariableStatement(n)) for (const d of n.declarationList.declarations) {
|
||||
if (ts.isIdentifier(d.name) && d.initializer) decls.set(d.name.text, d.initializer)
|
||||
}
|
||||
})
|
||||
|
||||
const out: ProgressLoopConfig[] = []
|
||||
walk(sf, (node: any) => {
|
||||
if (!ts.isCallExpression(node)) return
|
||||
const callee = node.expression
|
||||
const calleeName = ts.isIdentifier(callee) ? callee.text : ts.isPropertyAccessExpression(callee) ? callee.name?.text : ''
|
||||
if (calleeName !== 'useTaskProgressLoop') return
|
||||
const options = node.arguments[0]
|
||||
if (!options || !ts.isObjectLiteralExpression(options)) return
|
||||
const prop = options.properties.find((p: any) => p.name && (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) && p.name.text === 'isTerminal')
|
||||
|
||||
const cfg: ProgressLoopConfig = {
|
||||
file,
|
||||
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1 + lineOffset,
|
||||
passesIsTerminal: !!prop,
|
||||
isTerminalRef: null,
|
||||
isTerminalWords: [],
|
||||
delegatesToShared: false,
|
||||
}
|
||||
if (prop) {
|
||||
const init = (prop as any).initializer
|
||||
if (init && ts.isIdentifier(init)) cfg.isTerminalRef = init.text
|
||||
if (init && ts.isCallExpression(init) && ts.isIdentifier(init.expression)) cfg.isTerminalRef = init.expression.text
|
||||
const target = cfg.isTerminalRef ? decls.get(cfg.isTerminalRef) : null
|
||||
if (target) {
|
||||
const words = new Set<string>()
|
||||
const delegated = new Set<string>()
|
||||
walk(target, (inner: any) => {
|
||||
wordsOfExpressionContainer(inner, words)
|
||||
if (ts.isCallExpression(inner) && ts.isIdentifier(inner.expression) && sharedPredicateNames.includes(inner.expression.text)) delegated.add(inner.expression.text)
|
||||
})
|
||||
cfg.isTerminalWords = [...words]
|
||||
cfg.delegatesToShared = delegated.size > 0
|
||||
}
|
||||
}
|
||||
out.push(cfg)
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user