diff --git a/packages/tools/cli/lib/visual-audit.mjs b/packages/tools/cli/lib/visual-audit.mjs index 5a42cdf..446bff6 100644 --- a/packages/tools/cli/lib/visual-audit.mjs +++ b/packages/tools/cli/lib/visual-audit.mjs @@ -30,6 +30,9 @@ function countInLine(line, key) { /*** 作用:按 git 历史倒序找最可能引入该类硬编码的提交(只查样式表最近 40 次改动) ***/ function suspectCommit(root, key) { + // 工作区先脏了就先报未提交改动:此时逐提交归因只会指向「上一次提交」,对排查没帮助 + const dirty = spawnSync('git', ['status', '--porcelain', '--', UI_CSS_REL], { cwd: root, encoding: 'utf8' }); + if (dirty.status === 0 && String(dirty.stdout).trim()) return { hash: '(工作区未提交的改动)', hit: null }; const log = spawnSync('git', ['log', '-n', '40', '--pretty=%h', '--', UI_CSS_REL], { cwd: root, encoding: 'utf8' }); if (log.status !== 0) return null; // 非 git 环境(打包分发)跳过归因 for (const hash of String(log.stdout).split('\n').map((s) => s.trim()).filter(Boolean)) { @@ -121,7 +124,11 @@ export function visualAudit(args, root) { const now = result.counts[meta.key] ?? 0; const was = baseline.counts[meta.key] ?? 0; const sus = suspectCommit(root, meta.key); - const who = sus ? `疑似由提交 ${sus.hash} 引入(该提交新增 ${sus.hit} 处)` : '未能定位引入提交(可能来自未提交改动)'; + const who = sus && sus.hit === null + ? `来自${sus.hash},提交前应先改回令牌` + : sus + ? `疑似由提交 ${sus.hash} 引入(该提交新增 ${sus.hit} 处)` + : '未能定位引入提交(可能来自未提交改动)'; console.error(`audit:visual: ${meta.label}(${meta.key})涨了 ${now - was} 处(基线 ${was} → 当前 ${now}),${meta.hint};${who}`); } console.error(`视觉基线失败:${grown.length} 类超过基线。禁止新增硬编码视觉字面量,改用 --ua-* 令牌`);