|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * @ai-context 豁免表「散文数字」对拍探针(批 7 T11 新造;C10.6 第 6 项那处散文的机器兜底)。 |
| 4 | + * ① `app_commands.rs` 登记行正文的「N 条」(generate_handler! 实条目数)—— `--write` 只刷数字列 ⇒ 零门禁; |
| 5 | + * ② `App.tsx` 登记行的行数 —— 另有 `--full` 的 (e) 判据兜底,本探针作对照。判据 = 散文值 == 真源实测值。 |
| 6 | + * 口径:行数 = countLines()(文件**全部**行数含空行;末尾换行不额外算一行),与 `scripts/line-limits.mjs` 同源。 |
| 7 | + * ⚠️ 本脚本**不是门禁**:不入八闸 / 不入 CI / 不入 husky,只能**手工跑**(批 7 §C11.8)。批 8 的候选闸。 |
| 8 | + * 退出码:0 = 全一致 · 1 = 有不一致 · 2 = 解析失败(登记行/字段缺失,或读不到门禁输出)。 |
| 9 | + */ |
| 10 | +import { execFileSync } from 'node:child_process'; |
| 11 | +import { readFileSync } from 'node:fs'; |
| 12 | +import { dirname, join } from 'node:path'; |
| 13 | +import { fileURLToPath } from 'node:url'; |
| 14 | + |
| 15 | +const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..'); |
| 16 | +const TABLE = 'docs/standards/line-limit-exemptions.md'; |
| 17 | +const REGISTRY = 'scripts/check-command-registry.mjs'; |
| 18 | +const CMD_ROW = 'app/src-tauri/src/app_commands.rs'; |
| 19 | +const APP_TSX = 'app/src/App.tsx'; |
| 20 | + |
| 21 | +const USAGE = `用法:node scripts/check-exemption-prose.mjs [--self-test] |
| 22 | + 无参数 对拍:豁免表「N 条」↔ 命令注册门禁「定义 N」;App.tsx 登记行数 ↔ countLines 实测 |
| 23 | + --self-test 四例内存夹具(一致 / 条数错值 / 行数错值 / 缺行)自证判据有牙 · --help 本帮助 |
| 24 | +⚠️ 本脚本不是门禁:不入八闸 / 不入 CI / 不入 husky,只能手工跑(批 7 §C11.8)。批 8 的候选闸。`; |
| 25 | + |
| 26 | +const countLinesText = (s) => s.split('\n').length - (s.endsWith('\n') ? 1 : 0); |
| 27 | +const countLines = (abs) => countLinesText(readFileSync(abs, 'utf8')); |
| 28 | + |
| 29 | +/** 纯判定:真跑与 --self-test 共用同一实现(避免"自测走另一条代码路径") */ |
| 30 | +function evaluate({ text, registryOut, appLines }) { |
| 31 | + const lines = text.split('\n'); |
| 32 | + const row = (p) => { |
| 33 | + const i = lines.findIndex((l) => l.startsWith(`| ${p} |`)); |
| 34 | + return i < 0 ? null : { no: i + 1, text: lines[i] }; |
| 35 | + }; |
| 36 | + const cmd = row(CMD_ROW), app = row(APP_TSX); |
| 37 | + const gate = /定义\s+(\d+)\s/.exec(`${registryOut} `); |
| 38 | + const parse = []; |
| 39 | + if (!cmd) parse.push(`找不到登记行:| ${CMD_ROW} |`); |
| 40 | + if (!app) parse.push(`找不到登记行:| ${APP_TSX} |`); |
| 41 | + if (!gate) parse.push(`读不到门禁「定义 N」:${registryOut.trim().slice(0, 50)}`); |
| 42 | + const pm = cmd && (/\*\*(\d+)\s*条\*\*/.exec(cmd.text) ?? /(\d+)\s*条/.exec(cmd.text)); |
| 43 | + const dm = app && /^\|[^|]*\|\s*(\d+)\s*\|/.exec(app.text); |
| 44 | + if (cmd && !pm) parse.push(`:${cmd.no} 读不到「N 条」`); |
| 45 | + if (app && !dm) parse.push(`:${app.no} 读不到登记行数`); |
| 46 | + if (parse.length) return { parse, problems: [] }; |
| 47 | + const prose = Number(pm[1]), declared = Number(dm[1]), gateN = Number(gate[1]); |
| 48 | + return { parse, problems: [ |
| 49 | + { ok: prose === gateN, where: `:${cmd.no}`, label: `散文「${pm[0]}」${prose === gateN ? '==' : '!='} 门禁「定义 ${gateN}」`, hint: '本行的「N 条」须与 generate_handler! 实条目数同批更新(C10.6 第 6 项)' }, |
| 50 | + { ok: declared === appLines, where: `:${app.no}`, label: `App.tsx 登记 ${declared} ${declared === appLines ? '==' : '!='} countLines ${appLines}`, hint: '登记行数须与实测同批更新(该半另有 line-limits --full 的 (e) 判据兜底)' }, |
| 51 | + ] }; |
| 52 | +} |
| 53 | + |
| 54 | +function realRun() { |
| 55 | + let out; |
| 56 | + try { |
| 57 | + out = execFileSync(process.execPath, [join(ROOT, REGISTRY)], { cwd: ROOT, encoding: 'utf8' }); |
| 58 | + } catch (err) { |
| 59 | + out = `${err.stdout ?? ''}`; // 门禁自身红灯(exit 1)仍带可用读数 |
| 60 | + if (!/定义\s+\d+/.test(out)) { console.error(`❌ 无法执行 ${REGISTRY}:${err.message}`); return 2; } |
| 61 | + } |
| 62 | + const r = evaluate({ text: readFileSync(join(ROOT, TABLE), 'utf8'), registryOut: out, appLines: countLines(join(ROOT, APP_TSX)) }); |
| 63 | + if (r.parse.length) { |
| 64 | + console.log('❌ 解析失败(exit 2):'); |
| 65 | + for (const p of r.parse) console.log(` · ${p}`); |
| 66 | + return 2; |
| 67 | + } |
| 68 | + let bad = 0; |
| 69 | + for (const c of r.problems) { |
| 70 | + console.log(`${c.ok ? '✅' : '❌'} ${c.label}(${TABLE}${c.where})`); |
| 71 | + if (!c.ok) { bad++; console.log(` ⇒ ${c.hint}`); } |
| 72 | + } |
| 73 | + console.log(bad ? `❌ 散文与真源不一致:${bad} 处` : '✅ 散文与真源一致(本脚本非门禁、只能手工跑;批 8 候选闸)'); |
| 74 | + return bad ? 1 : 0; |
| 75 | +} |
| 76 | + |
| 77 | +const fixture = (prose, declared) => [`| 文件 | 行数 | 豁免理由 | 拆分计划 |`, '|---|---|---|---|', |
| 78 | + `| ${APP_TSX} | ${declared} | 夹具行 | 若再增长:拆分 |`, |
| 79 | + `| ${CMD_ROW} | 476 | 夹具:**${prose} 条** 注册点 | 不拆(数据文件) |`, ''].join('\n'); |
| 80 | +const GATE = (n) => `✅ 命令注册一致:定义 ${n} / 注册 ${n} / 重复 0`; |
| 81 | + |
| 82 | +/** `--self-test`:夹具自带数值(不读仓库现状 ⇒ T19 把 310 改成 311 后仍成立) */ |
| 83 | +function runSelfTest() { |
| 84 | + const fails = []; |
| 85 | + const expect = (label, cond) => { console.log(` ${cond ? '✓' : '✗'} ${label}`); if (!cond) fails.push(label); }; |
| 86 | + const go = (text, gate, appLines) => evaluate({ text, registryOut: GATE(gate), appLines }); |
| 87 | + const clean = go(fixture(310, 549), 310, 549); |
| 88 | + expect('夹具 1/4 一致(310==310 · 549==549)⇒ 0 问题 / 0 解析失败', clean.parse.length === 0 && clean.problems.every((p) => p.ok)); |
| 89 | + const bad1 = go(fixture(313, 549), 310, 549); |
| 90 | + expect('夹具 2/4 手工错值「313 条」对门禁 310 ⇒ 报不等', bad1.problems.some((p) => !p.ok && p.label.includes('313') && p.label.includes('310'))); |
| 91 | + const bad2 = go(fixture(310, 548), 310, 549); |
| 92 | + expect('夹具 3/4 手工错值登记 548 对实测 549 ⇒ 报不等', bad2.problems.some((p) => !p.ok && p.label.includes('548') && p.label.includes('549'))); |
| 93 | + const missing = go('没有那张表\n', 310, 549); |
| 94 | + expect('夹具 4/4 缺登记行 ⇒ 判解析失败(exit 2 路径)', missing.parse.filter((p) => p.includes('找不到登记行')).length === 2); |
| 95 | + expect('countLines 口径自证:末尾带 / 不带换行都算 2 行', countLinesText('a\nb\n') === 2 && countLinesText('a\nb') === 2); |
| 96 | + if (fails.length) { console.log(`❌ --self-test 失败:${fails.length} 项断言未通过`); return 1; } |
| 97 | + console.log('✅ --self-test 通过:4 例夹具 + countLines 口径自证全部判定正确'); |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +function main(argv) { |
| 102 | + for (const a of argv) { |
| 103 | + if (a === '--help' || a === '-h') { console.log(USAGE); return 0; } |
| 104 | + if (a === '--self-test') return runSelfTest(); |
| 105 | + console.error(`未知参数:${a}\n${USAGE}`); |
| 106 | + return 2; |
| 107 | + } |
| 108 | + return realRun(); |
| 109 | +} |
| 110 | + |
| 111 | +try { process.exitCode = main(process.argv.slice(2)); } |
| 112 | +catch (err) { console.error(`❌ 探针无法执行:${err.message}`); process.exitCode = 2; } |
0 commit comments