1215 lines
71 KiB
JavaScript
1215 lines
71 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 类名体检:抓「写了但生成不出规则」的死类名。
|
||
*
|
||
* 为什么要它:Tailwind v4 改过一批名字(break-anywhere → wrap-anywhere、
|
||
* font-inherit 直接没了),写错不报错,只是静悄悄没有样式,肉眼很难发现。
|
||
*
|
||
* 做法:把项目真正的入口 CSS 喂给 Tailwind 编译器,逐个候选问它
|
||
* 「这个词你能生成规则吗」。产物里有对应类选择器 = 有效,没有 = 死类名。
|
||
* 判定用的是构建时同一套配置(@theme 语义色、自定义断点、@utility),不会误报。
|
||
*
|
||
* 第二道检查(2026-11 图标重设计加的):图标只许从 components/icons 取。
|
||
* 全站唯一允许 `from '@ant-design/icons'` 的地方是那张登记表——阶段 25 起它是一个目录、
|
||
* 两张表(core.ts 首屏 / extra.ts 懒页面)加一个桶与一件公共件,放行按**目录**不按文件名。
|
||
* 为什么必须钉住:页面一旦直接 import 字形名(AppstoreOutlined),
|
||
* 「这个位置到底是网格视图还是资产库」就又只有读代码的人才知道,
|
||
* 换图标也又变成全站搜索替换——登记表当天就废掉。
|
||
*
|
||
* 第三道检查(2026-11 按钮重设计加的):裸 <button> 只许出现在「自定义表面族」里。
|
||
* 判据不是「长得特殊」,而是几何:控件的宽高由所在的那块媒体/容器决定、
|
||
* antd 三档(24/32/40)套不进去的,才是自定义表面(走带键、角标、遮罩、胶囊);
|
||
* 只要是一颗独立矩形按钮,就必须是 antd <Button>,否则它不吃 theme token,
|
||
* 换主题、改 controlHeight、加 disabled 语义时全都得手工再对一遍。
|
||
* 白名单文件必须仍然写着 nex-* 标记类——标记类是给 CSS 和截图复核用的抓手,
|
||
* 一个都不写了说明这一族已经被 antd 取代,条目就该从白名单里删掉。
|
||
*
|
||
* 第四道检查(2026-11 按钮重设计加的):浮层底部(Drawer 的 actions / Modal 的 footer)
|
||
* 是一个「动作条」,不是一堆各写各的按钮。它只有一条口径,且必须能被机器验出来:
|
||
* 从左到右 = 辅助动作 → 取消 → 主行动(和 antd Modal 自带页脚同序),整条右对齐。
|
||
* 为什么把顺序也钉进脚本:这条是**语义**约定而不是样式约定,CSS 里没处可查;
|
||
* 一旦某个抽屉把「保存」写在「取消」左边,全站抽屉就开始互相不一致,
|
||
* 而这种漂移肉眼极难发现——只能靠脚本逐块读 DOM 顺序。
|
||
*
|
||
* 用法:node scripts/ui/classaudit.mjs [src 目录]
|
||
*/
|
||
import fs from 'node:fs'
|
||
import path from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
||
const FRONTEND = path.resolve(HERE, '../../frontend')
|
||
const { compile } = await import(path.join(FRONTEND, 'node_modules/tailwindcss/dist/lib.mjs'))
|
||
|
||
const SRC = path.resolve(process.argv[2] || path.join(FRONTEND, 'src'))
|
||
const ENTRY = path.join(FRONTEND, 'src/styles/index.css')
|
||
|
||
/** antd 自己的类、我们的体检标记、自有组件类:都不是 Tailwind 的活 */
|
||
const FOREIGN = /^(ant|nex|nf)-/
|
||
|
||
/**
|
||
* 剥掉一行里的注释,并返回「这一行结束时是否仍在块注释里」。
|
||
*
|
||
* 症状(2026-11 第 4 道闸上线后冒出来的假阳性):块注释的**续行**不以 `*` 开头,
|
||
* 于是「… .ant-segmented-item-label 的 overflow:hidden 裁掉半个字」收尾的那一行
|
||
* 被当成源码用词,`overflow:hidden` 报成死类名——可它根本不是类名,是中文注释里的
|
||
* 一个 CSS 属性。旧判据只看行首,天然处理不了跨行注释。
|
||
* 做法:真的跟着块注释开合标记走状态;同一行内的整段抹掉,
|
||
* 行首 // 与行尾 ` // `(代码里不会出现裸双斜杠)一并抹掉。
|
||
*/
|
||
function stripComments(line, wasInBlock) {
|
||
let inBlock = wasInBlock()
|
||
let q = null // 引号内的 /* 与 // 不是注释;字符串本身要留着(className 就在引号里)
|
||
let out = ''
|
||
for (let i = 0; i < line.length; i++) {
|
||
const ch = line[i]
|
||
const two = line.slice(i, i + 2)
|
||
if (inBlock) {
|
||
if (two === '*/') { inBlock = false; i++ }
|
||
continue
|
||
}
|
||
if (q) {
|
||
out += ch
|
||
if (ch === '\\') { if (i + 1 < line.length) { out += line[i + 1]; i++ } continue }
|
||
if (ch === q) q = null
|
||
continue
|
||
}
|
||
if (ch === '"' || ch === "'" || ch === '`') { q = ch; out += ch; continue }
|
||
if (two === '/*') { inBlock = true; i++; continue }
|
||
if (two === '//') break
|
||
out += ch
|
||
}
|
||
return [out, inBlock]
|
||
}
|
||
|
||
function candidates() {
|
||
const out = new Map()
|
||
const walk = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walk(p); continue }
|
||
if (!/\.(tsx|jsx)$/.test(ent.name)) continue
|
||
let inBlock = false // 块注释状态要跨行:只看行首会漏掉续行(见 stripComments)
|
||
inBlock = false
|
||
fs.readFileSync(p, 'utf8').split('\n').forEach((raw, i) => {
|
||
const [line, block] = stripComments(raw, () => inBlock)
|
||
inBlock = block
|
||
if (!line.trim()) return
|
||
for (const tok of line.split(/[\s'"`]+/)) {
|
||
if (tok.length < 3 || /-$/.test(tok) || FOREIGN.test(tok)) continue
|
||
if (/[一-龥]/.test(tok)) continue
|
||
if (!/^[a-z]/.test(tok)) continue
|
||
if (!/^[\w:[\]().,%#*/=&>+~-]+$/.test(tok)) continue
|
||
for (const ch of '[]()') {
|
||
const open = ch === '[' || ch === '('
|
||
const n = (tok.split(ch).length - 1)
|
||
const mate = open ? (ch === '[' ? ']' : ')') : (ch === ']' ? '[' : '(')
|
||
if (n !== tok.split(mate).length - 1) return
|
||
}
|
||
const at = `${path.relative(FRONTEND, p)}:${i + 1}`
|
||
if (!out.has(tok)) out.set(tok, [])
|
||
out.get(tok).push(at)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
walk(SRC)
|
||
return out
|
||
}
|
||
|
||
function generated(css) {
|
||
const set = new Set()
|
||
for (const m of css.matchAll(/\.((?:\\.|[-\w])+)/g)) set.add(m[1].replace(/\\(.)/g, '$1'))
|
||
return set
|
||
}
|
||
|
||
const compiler = await compile(fs.readFileSync(ENTRY, 'utf8'), {
|
||
base: path.dirname(ENTRY),
|
||
async loadStylesheet(id, base) {
|
||
let p = id.startsWith('.') ? path.resolve(base, id) : path.resolve(FRONTEND, 'node_modules', id)
|
||
if (fs.existsSync(p) && fs.statSync(p).isDirectory()) p = path.join(p, 'index.css')
|
||
return { path: p, base: path.dirname(p), content: fs.readFileSync(p, 'utf8') }
|
||
},
|
||
async loadModule(p, base) { return { module: null, base } },
|
||
})
|
||
|
||
const cands = candidates()
|
||
const all = generated(compiler.build([...cands.keys()]))
|
||
|
||
/** 首段(grid / text / max-h …)在产物里出现过的,才认为「本该是工具类」 */
|
||
const roots = new Set()
|
||
for (const name of all) {
|
||
const core = name.split(':').pop().replace(/\[[^\]]*\].*$/, '')
|
||
roots.add(core.split('-')[0])
|
||
}
|
||
|
||
/** 文案 / props 值 / antd Menu 的 group key —— 是英语单词,不是类名 */
|
||
const PROSE = new Set([
|
||
'align', 'break', 'duration', 'flex-end', 'gap', 'items', 'left', 'list', 'max',
|
||
'max-height', 'min', 'min-width', 'not', 'object', 'order', 'pointer', 'right',
|
||
'row', 'text', 'top', 'group-global', 'group-project',
|
||
/* 2026-11 布局层改用 antd Flex 后多出来的三个 props 值 / CSS 取值:
|
||
`wrap` 是 <Flex wrap> 这个布尔属性本身、`space-between` 是 justify="space-between"、
|
||
`not-allowed` 是 style 里的 cursor、`flex-start` 是 <Flex align="flex-start">。
|
||
Tailwind 里对应的事各有正名
|
||
(flex-wrap / justify-between / cursor-not-allowed),这三个词当类名写出来必然是笔误,
|
||
但作为 props 值天天出现,逐行切词分不清引号内外,只能在这里点名。 */
|
||
'wrap', 'space-between', 'not-allowed', 'flex-start',
|
||
])
|
||
|
||
const dead = []
|
||
for (const [tok, where] of cands) {
|
||
if (all.has(tok)) continue
|
||
const core = tok.split(':').pop()
|
||
if (/^\d+$/.test(core) || /^[\w-]+\[[a-z0-9]\]$/.test(core)) continue // 模板字符串里的 key 片段
|
||
const root = core.replace(/\[[^\]]*\]/g, '').split('-').filter(Boolean)[0]
|
||
if (!root || !roots.has(root) || PROSE.has(core)) continue
|
||
dead.push([tok, where])
|
||
}
|
||
|
||
dead.sort((a, b) => a[0].localeCompare(b[0]))
|
||
for (const [tok, where] of dead) console.log(`DEAD ${tok.padEnd(26)} ${where.join(' ')}`)
|
||
console.log(`\n${dead.length} 个死类名 / ${cands.size} 个候选`)
|
||
|
||
/* ── 第 2 条:图标出口唯一 ───────────────────────────────────────────────
|
||
登记表**目录**是唯一可以 import 字形名的地方;其余文件一律写语义别名
|
||
(IconGrid / IconWarn / IconTimeline),要新图形先在登记表登记一行。
|
||
为什么必须钉住:页面一旦直接 import 字形名(AppstoreOutlined),
|
||
「这个位置到底是网格视图还是资产库」就又只有读代码的人才知道,
|
||
换图标也又变成全站搜索替换——登记表当天就废掉。
|
||
|
||
2026-11-09 阶段 25:这张登记表从「一个文件」变成**一个目录**(两级表)——
|
||
· `core.ts` 首屏外壳用得上的 54 只,由 `index.ts` 桶对外;
|
||
· `extra.ts` 只有懒加载页面用得上的 24 只,调用方深 import `icons/extra`;
|
||
· `_kit.ts` `decorative()` 与两个图标类型,**不导出任何图形**;
|
||
· `index.ts` 桶,只 re-export core(见下面第 2 条·附三:它不许碰 extra)。
|
||
所以放行范围跟着从「一个文件」改成「这个目录」:两张表各自 import 自己要用的字形,
|
||
`_kit.ts` 里那条 `import type { ProjectOutlined }` 只是取它的属性类型,
|
||
`import type` 编译期整条抹掉、不上产物(这也是它不算绕过口径的原因:
|
||
它渲染不出任何东西,字形名也就漏不进辅助技术树)。
|
||
扫描前先剥注释:登记表文件头里有一句散文写着
|
||
`import { XOutlined } from '@ant-design/icons'`,不剥就是注释自己被当成一次绕过
|
||
(§13.11 已把「import 正则先剥注释」立成通则)。 */
|
||
const REG_DIR = path.join(SRC, 'components/icons')
|
||
/** 读一个源文件并剥掉全部注释(保留行号:返回按行切开的数组,注释位置留空行)。 */
|
||
function codeLines(file) {
|
||
let inBlock = false
|
||
return fs.readFileSync(file, 'utf8').split('\n').map((raw) => {
|
||
const [line, block] = stripComments(raw, () => inBlock)
|
||
inBlock = block
|
||
return line
|
||
})
|
||
}
|
||
const inRegistryDir = (p) => p === REG_DIR || p.startsWith(REG_DIR + path.sep)
|
||
const stray = []
|
||
const walkIcons = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkIcons(p); continue }
|
||
if (!/\.(tsx|ts)$/.test(ent.name) || inRegistryDir(p)) continue
|
||
codeLines(p).forEach((line, i) => {
|
||
if (/from '@ant-design\/icons'/.test(line)) stray.push(`${path.relative(FRONTEND, p)}:${i + 1}: ${line.trim()}`)
|
||
})
|
||
}
|
||
}
|
||
walkIcons(SRC)
|
||
for (const hit of stray) console.log(`ICON ${hit}`)
|
||
console.log(`${stray.length} 处绕过登记表目录的图标 import(只许 ${path.relative(FRONTEND, REG_DIR)}/ 里的两张表)`)
|
||
/* ── 第 2 条·附:登记表自身的辅助技术口径(2026-11-08 · S-2)───────────────
|
||
上面那条只管「别人不许 import 字形」,管不到登记表自己怎么写出口。
|
||
`decorative()` 把「图标默认是装饰」写死在唯一的出口上(登记表顶部规则 5);
|
||
绕过它的三种写法都会让英文字形名重新混进可读名称——「upload 导入脚本」
|
||
「down 导出」就是这么来的(取证:reports/ui_evidence/nex-s2/ax-BEFORE.json):
|
||
a) 裸 re-export: export { UploadOutlined } from '@ant-design/icons'
|
||
b) 直接赋值: export const IconUpload = UploadOutlined
|
||
c) 换名再导出: export { UploadOutlined as IconUpload }
|
||
对账逐条匹配固定形状 `export const IconX = decorative(Glyph, 'IconX')`:
|
||
加图标的人只会复制旁边一行,不会先读注释,所以形状不对就红。
|
||
反例由 scripts/ui/gateprobe.py 注入验证(改一条注释不会把这条闸改成恒绿)。 */
|
||
/** 登记表的全部出口所在文件:目录里除桶与公共件之外的那几张表。
|
||
写成「扫目录」而不是写死 `['core.ts','extra.ts']`,是为了让这条判据对
|
||
单文件旧布局和两级新布局**同样有效**——gateprobe ⑰ 的夹具只写一份 index.ts,
|
||
它必须继续量得出东西(放行但采不到 = 空转,这是本项目栽过的跟头)。 */
|
||
const TABLES = fs.existsSync(REG_DIR)
|
||
? fs.readdirSync(REG_DIR).filter((f) => /\.ts$/.test(f)).map((f) => path.join(REG_DIR, f))
|
||
: []
|
||
const REG_NAME = path.relative(FRONTEND, REG_DIR) + '/'
|
||
const regSrc = TABLES.map((f) => codeLines(f).join('\n')).join('\n')
|
||
const regHits = []
|
||
for (const m of regSrc.matchAll(/^export\s*\{([^}]*)\}/gm)) {
|
||
if (/\bIcon[A-Z]\w*\b|Outlined|Filled|TwoTone/.test(m[1]))
|
||
regHits.push(`${REG_NAME}: 图标走了 export {${m[1].trim().slice(0, 54)}}`
|
||
+ ` —— 不吃 decorative(),字形名会重新进辅助技术树`)
|
||
}
|
||
const regIcons = [...regSrc.matchAll(/^export const (Icon\w+)\s*=\s*([^\n]*)$/gm)]
|
||
for (const [, name, rhs] of regIcons) {
|
||
if (!new RegExp(`^decorative\\(\\w+\\s*,\\s*'${name}'\\)$`).test(rhs.trim()))
|
||
regHits.push(`${REG_NAME}: ${name} 的出口不是 decorative(Glyph, '${name}')(现在是 ${rhs.trim().slice(0, 46)})`
|
||
+ ` —— 图标默认必须 aria-hidden,需要图标自己承担语义时显式写 aria-label`)
|
||
}
|
||
if (!regIcons.length) regHits.push(`${REG_NAME}: 一条 decorative() 图标出口都没匹配到 —— 登记表结构变了,这道闸要跟着改`)
|
||
for (const hit of regHits) console.log(`ICONA ${hit}`)
|
||
console.log(`${regHits.length} 处图标出口未走 decorative()(登记表 ${REG_NAME} 共 ${regIcons.length} 条出口 / 目录外 ${stray.length} 处 import)`)
|
||
/* ── 第 2 条·附二:图标型控件的名字从哪儿来(2026-11-09 · S-2 复扫)──────────
|
||
decorative() 把字形名从辅助技术树里撤掉之后,「一颗只画图标的按钮」剩下的命名
|
||
通道只有两条:调用方自己写的 aria-label / title,或者整颗没有名字。
|
||
浏览器口径的闸门(semanticcheck 的 NAME / GLYPH)量不到这一族的大本营:抽屉、
|
||
模态、灯箱、录音面板里的图标钮要**先点开浮层**才在 DOM 里,而常驻探针跑的是
|
||
15 条路由的关闭态(阶段 10 的普查 census-icons.py 同样只走关闭态)。
|
||
两个实测数(2026-11-09 · 阶段 17,口径可复现,别再抄口头数):
|
||
· 源码面:全站 **46 颗**自关闭的图标型控件,分布在 18 个文件(Storyboard 7 /
|
||
Library 6 / Timeline 6 / AssetLibrary 5 / MediaLightbox 4 / UserAdmin 4 …),
|
||
43 颗同时写了 aria-label + title、2 颗只有 aria-label、1 颗只有 title,
|
||
缺名字的 **0 颗** ⇒ 这条闸今天立案 0 处,它是**立案起点**而不是缺陷清单;
|
||
· 浏览器面:reports/ui_evidence/nex-p17/s2overlay.py 把 8 个浮层逐个点开
|
||
(概况抽屉 / 删除确认模态 / 平台动态 / 430 导航抽屉 / 新建账号 / 编辑母版 /
|
||
素材灯箱 / 分镜检查器),明暗各量一遍:**每个主题 16 颗**图标型控件
|
||
是关闭态探针根本量不到的(最多一处 = 分镜检查器 8 颗),命名缺陷 0 颗。
|
||
⇒ 「15 条路由关闭态全绿」离「全站图标控件都有名字」还差 16 颗的可见面,
|
||
而没有任何东西挡得住下一个人在新抽屉里顺手加一颗没名字的 ✕。
|
||
判据敢用静态,靠的是一个结构事实:**自闭合的 JSX 元素不可能有 children**,
|
||
所以「这颗控件没有文字」不需要渲染就能确定;有 icon 槽又没有 aria-label / title,
|
||
就等于把命名托付给了一只默认 aria-hidden 的图标 = 托付给空气。
|
||
三条子判据:
|
||
① `<Button|LinkButton|button|a … icon={<Icon…/>} … />` 自关闭 + 无 aria-label
|
||
且无 title ⇒ 立案(宿主只有图标,却没有名字);
|
||
② `<Icon… aria-label="字面量">` 的字面量里没有汉字 ⇒ 立案。图标自己承担语义
|
||
是 decorative() 的第二条通道,写进去的就是 AT 念到的全部;今天 4 处出口
|
||
全部是中文(「更多页签」「清空搜索」+ SecretInput 的两态 hint),
|
||
一旦有人图省事写回 `aria-label="upload"`,S-2 就原地复活。
|
||
③ **antd 把 onClick 克隆到图标本体上的那些槽位**(closeIcon / clearIcon /
|
||
removeIcon / prevIcon / nextIcon / sortIcon / filterIcon / expandIcon /
|
||
menuItemSelectedIcon / dropIcon)里的 `<Icon…/>` 必须自带非空中文 aria-label。
|
||
为什么 ① 管不到它们:① 认的是「宿主只画图标」,而这些槽位的宿主(Tag、Select、
|
||
Table 表头……)**本来就有文字或有自己的名字**,图标是宿主里另一颗独立操作
|
||
——胶囊 `人物·俊杰` 有名字,那颗 ✕ 仍然要自己的名字。
|
||
为什么不看渲染结果:静态就能定死。`antd/es/tag/index.js:81-93` 的
|
||
`closeIconRender` + `rc-util/replaceElement` 把 `onClick` 与
|
||
`ant-tag-close-icon` **克隆到我们那颗图标节点上**,antd 不再包一层壳
|
||
⇒ 图标自己就是那颗可点的东西;而 `decorative()` 默认 `aria-hidden`
|
||
⇒ 不写名字 = 这条操作在辅助技术树里不存在。
|
||
「antd 会自己兜一个名字」不算数:`_util/hooks/useClosable.js` 从 5.29 起
|
||
才用 `locale.global.close`(现网实测念「关闭」)兜这一族,5.20 没有这一段,
|
||
而且 `clearIcon` 那一族 antd **从来不兜**——把命名托付给依赖库的版本实现细节,
|
||
升级那天静默失效,量都量不到。
|
||
这一族现网的真实调用面(grep 实测 2026-11-09):`Ctlbar.tsx:338`(胶囊 ✕,
|
||
本轮补「移除」)、`Input.tsx:183`(`clearIcon` 已带「清空搜索」),其余两处
|
||
只在注释里(注释已剥掉,不参与匹配)⇒ 真文件必须静音。
|
||
④ **图标当 children 的图标钮**(2026-11-10 · 阶段 32 · S-2 复扫补的第四支):
|
||
`<Button … onClick={f}><IconClose /></Button>` 这种写法 ① 看不见——它既不自关闭、
|
||
也没有 icon 槽,① 的两条前提一条都不成立,可结论一模一样:`decorative()` 一隐藏,
|
||
这颗控件在辅助技术树里没有名字。DOM 侧的 NAME 闸认得它,但常驻探针跑的是
|
||
15 条路由的**关闭态**,浮层里那一半(抽屉 / 模态 / 灯箱 / 录音面板)结构上量不到,
|
||
所以这一族仍须钉在源码上。
|
||
判据保守到只咬一种形状:**非自关闭 + 开标签没有 aria-label / aria-labelledby / title
|
||
+ children 里出现的每一个开标签都是登记表图标 + 没有任何裸文字 + 没有任何「可能出字」的
|
||
表达式**。表达式由 `exprIconOnly()` 判定,只认四种形状:整段就是一只 `<Icon… />`、
|
||
外面套圆括号、`cond ? 图标 : 图标`、`cond && 图标`(假值渲染成空,不出字);
|
||
`cond` 那一段随便写,两个**分支**必须逐条递归过这条判据。
|
||
children 里有别的元素(`<span>` 包的文字)、表达式里有字符串字面量或 `{row.label}`
|
||
这类认得出的标识符、或是函数调用 ⇒ 跳过:「看不出有没有名字」不是「没有名字」,
|
||
误报的代价和漏报一样是这条闸被关掉。
|
||
顺带打印覆盖率行 `ICNN-COVER childIcon=N named=M missing=K`(N = 全站「children 只有
|
||
图标」的交互标签颗数,M = 其中报了名的):这一族闸门的失败方式从来不是响错,
|
||
而是安静地什么都没看见(§15.1 通则),N=0 就说明采集形状变了、闸该跟着改而不是继续绿。
|
||
反例由 scripts/ui/gateprobe.py ㉓ 注入验证(改一条注释不会把这条闸改成恒绿)。 */
|
||
const ICON_NAMELESS = []
|
||
const CHILD_ICON_MISSING = []
|
||
let CHILD_ICON_TOTAL = 0, CHILD_ICON_NAMED = 0
|
||
/** 从 `<Tag` 的开括号扫到配对的 `>`,返回开标签原文与自关闭标记。
|
||
JSX 表达式里的 > 在 `{}` 内,字符串/模板字面量整段跳过。 */
|
||
function jsxOpenTag(src, from) {
|
||
let depth = 0, q = null
|
||
for (let i = from; i < src.length; i++) {
|
||
const c = src[i]
|
||
if (q) {
|
||
if (c === '\\') { i++; continue }
|
||
if (c === q) q = null
|
||
continue
|
||
}
|
||
if (c === '"' || c === "'" || c === '`') { q = c; continue }
|
||
if (c === '{') { depth++; continue }
|
||
if (c === '}') { depth--; continue }
|
||
if (c === '>' && depth === 0) return { head: src.slice(from, i + 1), end: i }
|
||
}
|
||
return null
|
||
}
|
||
/** 从 src[i](必须是 `{`)扫到配对的那个 `}`,返回它的下标;找不到返回 -1。
|
||
字符串与模板字面量整段跳过——里面的括号不算数。给 ③ 取 `xxxIcon={…}` 的原文用。 */
|
||
function braceAt(src, i) {
|
||
let depth = 0, q = null
|
||
for (let j = i; j < src.length; j++) {
|
||
const c = src[j]
|
||
if (q) {
|
||
if (c === '\\') { j++; continue }
|
||
if (c === q) q = null
|
||
continue
|
||
}
|
||
if (c === '"' || c === "'" || c === '`') { q = c; continue }
|
||
if (c === '{') depth++
|
||
else if (c === '}' && --depth === 0) return j
|
||
}
|
||
return -1
|
||
}
|
||
/** 从 `<X` 取到与之配对的 `</X>`:返回开标签原文 head 与 children 原文。
|
||
自关闭时 children 是空串;扫不到配对(模板残缺 / 解析不了)返回 null,调用方按「不判」处理。 */
|
||
function jsxElement(src, from) {
|
||
const t = jsxOpenTag(src, from)
|
||
if (!t) return null
|
||
const nm = /^<\s*([A-Za-z][\w.]*)/.exec(src.slice(from))
|
||
if (!nm) return null
|
||
const name = nm[1]
|
||
if (t.head.replace(/\s+$/, '').endsWith('/>')) return { head: t.head, children: '', selfClosing: true, end: t.end + 1 }
|
||
const tagRe = new RegExp('<\\s*/?\\s*' + name.replace('.', '\\.') + '(?=[\\s/>])', 'g')
|
||
tagRe.lastIndex = t.end + 1
|
||
let m, depth = 0
|
||
while ((m = tagRe.exec(src))) {
|
||
const closing = /^<\s*\//.test(m[0])
|
||
if (closing) {
|
||
if (depth === 0) {
|
||
const gt = src.indexOf('>', m.index) // 闭合标签没有属性,第一个 > 就是它结尾
|
||
return { head: t.head, children: src.slice(t.end + 1, m.index), selfClosing: false,
|
||
end: gt < 0 ? m.index : gt + 1 }
|
||
}
|
||
depth--
|
||
continue
|
||
}
|
||
const inner = jsxOpenTag(src, m.index)
|
||
if (inner && inner.head.replace(/\s+$/, '').endsWith('/>')) continue // 自关闭的同名标签不进栈
|
||
depth++
|
||
}
|
||
return null
|
||
}
|
||
/** 拆一段 JSX children:返回开标签名、`{}` 表达式原文、去掉标签与表达式之后的裸文字。
|
||
扫描靠 jsxOpenTag / braceAt,所以 `onClick={() => a > b}` 那种表达式里的 `>` 不会截断标签。 */
|
||
function kidsShape(src) {
|
||
const tags = [], exprs = []
|
||
let text = '', i = 0
|
||
while (i < src.length) {
|
||
const c = src[i]
|
||
if (c === '<') {
|
||
const t = jsxOpenTag(src, i)
|
||
if (!t) { i++; continue }
|
||
const nm = /^<\s*(\/?)([A-Za-z][\w.]*)/.exec(src.slice(i))
|
||
if (nm && !nm[1]) tags.push(nm[2]) // 闭合标签不算「内容」
|
||
i = t.end + 1
|
||
continue
|
||
}
|
||
if (c === '{') {
|
||
const e = braceAt(src, i)
|
||
if (e < 0) break
|
||
const body = src.slice(i + 1, e).trim()
|
||
if (body) exprs.push(body)
|
||
i = e + 1
|
||
continue
|
||
}
|
||
if (!/\s/.test(c)) text += c
|
||
i++
|
||
}
|
||
return { tags, exprs, text: text.replace(/&[a-z]+;/g, '').trim() }
|
||
}
|
||
/** 找一段表达式里**顶层**的 `? : && ||` 切点:圆括号 / 方括号 / 花括号 / 字符串 /
|
||
模板字面量内部都不算,整段 JSX 元素直接跳过——属性值里的 `?`、`:` 与箭头函数的 `=>`
|
||
都不是外层运算符(`onClick={() => a ? b : c}` 曾被当成三元拆过一遍)。 */
|
||
function topCuts(s) {
|
||
const cuts = []
|
||
let depth = 0, q = null
|
||
for (let i = 0; i < s.length; i++) {
|
||
const c = s[i]
|
||
if (q) { if (c === '\\') i++; else if (c === q) q = null; continue }
|
||
if (c === '"' || c === "'" || c === '`') { q = c; continue }
|
||
if (c === '(' || c === '[' || c === '{') { depth++; continue }
|
||
if (c === ')' || c === ']' || c === '}') { depth--; continue }
|
||
if (c === '<') {
|
||
const el = jsxElement(s, i)
|
||
if (el && el.end > i) { i = el.end - 1; continue }
|
||
const t = jsxOpenTag(s, i) // 解析不了的残缺 JSX 也要跳过,别把里面的字符当运算符
|
||
if (t) i = t.end
|
||
continue
|
||
}
|
||
if (depth) continue
|
||
if (c === '?' || c === ':') { cuts.push({ i, kind: c }); continue }
|
||
if (c === '&' && s[i + 1] === '&') { cuts.push({ i, kind: '&&' }); i++; continue }
|
||
if (c === '|' && s[i + 1] === '|') { cuts.push({ i, kind: '||' }); i++; continue }
|
||
}
|
||
return cuts
|
||
}
|
||
/** 这段表达式的返回值会不会「出字」——④ 的表达式守卫,保守到只放行四种形状:
|
||
· 整段就是一只登记表图标 `<Icon… />`(且自己没有 children);
|
||
· 外面套一层或多层**成对**圆括号(`(on ? <IconA /> : <IconB />)`);
|
||
· `cond ? 分支 : 分支`,两个分支各自递归过这条判据,cond 随便写;
|
||
· `cond && 图标`(假值渲染成空,不出字);`a || 图标` 则两侧都要过。
|
||
其余一律 false:字符串字面量、`{row.label}` 这种标识符、函数调用、包了 `<span>`——
|
||
这些是「看不出会不会出字」,④ 放过那颗控件不猜(放过 = 不计数,不是判它合规)。 */
|
||
function exprIconOnly(raw) {
|
||
let s = String(raw || '').trim()
|
||
for (;;) { // 只剥「与末尾配对」的外层圆括号
|
||
if (!s.startsWith('(')) break
|
||
let d = 0, q = null, closed = -1
|
||
for (let i = 0; i < s.length; i++) {
|
||
const c = s[i]
|
||
if (q) { if (c === '\\') i++; else if (c === q) q = null; continue }
|
||
if (c === '"' || c === "'" || c === '`') { q = c; continue }
|
||
if (c === '(') d++
|
||
else if (c === ')' && --d === 0) { closed = i; break }
|
||
}
|
||
if (closed !== s.length - 1) break
|
||
s = s.slice(1, -1).trim()
|
||
}
|
||
if (!s) return false
|
||
const cuts = topCuts(s)
|
||
const tern = cuts.find((c) => c.kind === '?')
|
||
const colon = tern && cuts.find((c) => c.kind === ':' && c.i > tern.i)
|
||
if (tern && colon) {
|
||
return exprIconOnly(s.slice(tern.i + 1, colon.i)) && exprIconOnly(s.slice(colon.i + 1))
|
||
}
|
||
const and = cuts.find((c) => c.kind === '&&')
|
||
if (and) return exprIconOnly(s.slice(and.i + 2))
|
||
const or = cuts.find((c) => c.kind === '||')
|
||
if (or) return exprIconOnly(s.slice(0, or.i)) && exprIconOnly(s.slice(or.i + 2))
|
||
if (!/^<Icon[A-Za-z0-9]*[\s/>]/.test(s)) return false
|
||
const el = jsxElement(s, 0)
|
||
return !!el && String(el.children).trim() === ''
|
||
}
|
||
/** 取一个开标签里的 `aria-label` 字面量:`="文"`、`={"文"}` 两种写法都认;
|
||
写成表达式(`aria-label={n}`)认不出 → 返回 null,由调用方按「没有名字」处理。 */
|
||
function ariaLit(head) {
|
||
const m = head.match(/\baria-label\s*=\s*(?:\{\s*)?(["'])((?:[^\\]|\\.)*?)\1(?:\s*\})?/)
|
||
return m ? m[2] : null
|
||
}
|
||
const lineOf = (code, idx) => code.slice(0, idx).split('\n').length
|
||
const tsxFiles = []
|
||
{
|
||
const collect = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const q = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { collect(q); continue }
|
||
if (/\.(tsx|jsx)$/.test(ent.name)) tsxFiles.push({ rel: path.relative(SRC, q), abs: q })
|
||
}
|
||
}
|
||
collect(SRC)
|
||
}
|
||
tsxFiles.sort()
|
||
for (const { rel, abs } of tsxFiles) {
|
||
const raw = fs.readFileSync(abs, 'utf8')
|
||
let inBlock = false
|
||
const code = raw.split('\n').map((l) => {
|
||
const [c, b] = stripComments(l, () => inBlock)
|
||
inBlock = b
|
||
return c
|
||
}).join('\n')
|
||
/* ① 只有图标、又没有名字的控件 */
|
||
for (const m of code.matchAll(/<(Button|LinkButton|button|a)([\s>])/g)) {
|
||
const tag = jsxOpenTag(code, m.index)
|
||
if (!tag || !tag.head.replace(/\s+$/, '').endsWith('/>')) continue
|
||
if (!/icon=\{[^}]*<Icon[A-Za-z]*\s*\/?>/.test(tag.head)) continue
|
||
if (/\baria-label\s*=|\btitle\s*=/.test(tag.head)) continue
|
||
ICON_NAMELESS.push(`${rel}:${lineOf(code, m.index)} — <${m[1]}> 只画图标却既无 aria-label 也无 title`
|
||
+ `(图标默认 aria-hidden,这颗控件对辅助技术没有名字):${tag.head.replace(/\s+/g, ' ').trim().slice(0, 76)}`)
|
||
}
|
||
/* ② 图标自己报名,报名内容必须是自然语言 */
|
||
for (const m of code.matchAll(/<Icon[A-Za-z]*\b/g)) {
|
||
const tag = jsxOpenTag(code, m.index)
|
||
if (!tag) continue
|
||
const lit = tag.head.match(/\baria-label\s*=\s*(["'])((?:[^\\]|\\.)*?)\1/
|
||
|| tag.head.match(/\baria-label\s*=\s*\{\s*(["'])((?:[^\\]|\\.)*?)\1\s*\}/))
|
||
if (!lit) continue
|
||
const val = lit[2]
|
||
if (!val) {
|
||
ICON_NAMELESS.push(`${rel}:${lineOf(code, m.index)} — <${m[0].slice(1)}> 的 aria-label 是空串(图标自称有语义却什么都没写)`)
|
||
} else if (!/[一-龥]/.test(val)) {
|
||
ICON_NAMELESS.push(`${rel}:${lineOf(code, m.index)} — <${m[0].slice(1)} aria-label="${val.slice(0, 24)}"> 没有中文`
|
||
+ `:这只图标自己承担语义,写什么读屏就念什么,字形码(upload / eye-invisible)不许当名字`)
|
||
}
|
||
}
|
||
/* ③ antd 把 onClick 克隆到图标本体上的槽位:那颗图标就是那颗操作 */
|
||
for (const m of code.matchAll(/\b(?:closeIcon|clearIcon|removeIcon|prevIcon|nextIcon|sortIcon|filterIcon|expandIcon|menuItemSelectedIcon|dropIcon)\s*[=:]\s*/g)) {
|
||
const at = m.index + m[0].length
|
||
let seg
|
||
if (code[at] === '{') { const close = braceAt(code, at); seg = close < 0 ? '' : code.slice(at + 1, close) }
|
||
else { const nl = code.indexOf('\n', at); seg = code.slice(at, nl < 0 ? code.length : nl) }
|
||
if (!seg.includes('<Icon')) continue
|
||
for (const im of seg.matchAll(/<Icon[A-Za-z]*\b/g)) {
|
||
const tag = jsxOpenTag(seg, im.index)
|
||
if (!tag) continue
|
||
const val = ariaLit(tag.head)
|
||
const at2 = `${rel}:${lineOf(code, m.index)}`
|
||
if (val === null) {
|
||
ICON_NAMELESS.push(`${at2} — ${m[0].replace(/\s+/g, ' ').trim()} 里的 <${im[0].slice(1)}> 没写 aria-label`
|
||
+ `:antd 把 onClick 克隆在这颗图标上,它就是那颗操作,而图标默认 aria-hidden`
|
||
+ ` ⇒ 辅助技术树里没有这条操作(antd 顺手兜的「关闭」是 5.29 起的实现细节,不算名字):`
|
||
+ `${tag.head.replace(/\s+/g, ' ').trim().slice(0, 76)}`)
|
||
} else if (!/[\u4e00-\u9fa5]/.test(val)) {
|
||
ICON_NAMELESS.push(`${at2} — ${m[0].replace(/\s+/g, ' ').trim()} 里的 <${im[0].slice(1)} aria-label="${val.slice(0, 24)}"> 没有中文`
|
||
+ `:这颗图标自己就是那颗操作,写什么读屏就念什么,字形码不许当名字`)
|
||
}
|
||
}
|
||
}
|
||
|
||
/* ④ 图标当 children 的图标钮(2026-11-10 · 阶段 32 · S-2 复扫)*/
|
||
for (const m of code.matchAll(/<(Button|LinkButton|button|a)([\s>])/g)) {
|
||
const el = jsxElement(code, m.index)
|
||
if (!el || el.selfClosing || !el.children.trim()) continue
|
||
const kids = kidsShape(el.children)
|
||
if (kids.text) continue // children 里有裸文字 ⇒ 有名字来源
|
||
if (kids.tags.some((t) => !/^Icon[A-Za-z0-9]*$/.test(t))) continue // 混了别的标签 ⇒ 可能出字
|
||
if (!kids.exprs.every(exprIconOnly)) continue // 表达式可能出字 ⇒ 不判
|
||
if (!kids.tags.length && !kids.exprs.length) continue // 只有空白 / 注释:不属这一族
|
||
CHILD_ICON_TOTAL++
|
||
const at = `${rel}:${lineOf(code, m.index)}`
|
||
if (/\baria-label\s*=|\baria-labelledby\s*=|\btitle\s*=/.test(el.head)) { CHILD_ICON_NAMED++; continue }
|
||
CHILD_ICON_MISSING.push(`${at} — <${m[1]}> 的 children 只有图标却既无 aria-label 也无 title`
|
||
+ `(图标默认 aria-hidden,这颗控件对辅助技术没有名字):`
|
||
+ `${el.head.replace(/\s+/g, ' ').trim().slice(0, 76)}`)
|
||
}
|
||
}
|
||
|
||
for (const hit of ICON_NAMELESS) console.log(`ICNN ${hit}`)
|
||
for (const hit of CHILD_ICON_MISSING) console.log(`ICNN ${hit}`)
|
||
console.log(`${ICON_NAMELESS.length + CHILD_ICON_MISSING.length} 处图标型控件缺自然语言名称(第 2 条·附二:① 自关闭 + icon 槽 + 无 aria-label/title;`
|
||
+ `② 图标自带 aria-label 必须是中文;③ closeIcon/clearIcon 这类「onClick 克隆到图标上」的槽位必须自带中文名字;`
|
||
+ `④ children 只有图标且无 aria-label/title)`)
|
||
// 覆盖率行:不是缺陷,是「这支闸今天到底看见了多少颗」的凭据(N=0 = 采集形状变了,闸在空转)
|
||
console.log(`ICNN-COVER childIcon=${CHILD_ICON_TOTAL} named=${CHILD_ICON_NAMED} missing=${CHILD_ICON_MISSING.length}`)
|
||
|
||
/* ── 第 2 条·附三:两级表的边界(2026-11-09 · 阶段 25 · Bundle)───────────────
|
||
拆表换的是**首屏字节**,同一条 `scripts/ui/chunkinfo.mjs` 前后配对实测
|
||
(`reports/ui_evidence/nex-p25/bundle-landing.log`):
|
||
首屏 raw 1 513.38 → 1 492.44 kB / gzip 460.51 → 455.77 kB(−4.74,−1.03 %)/
|
||
brotli 377.02 → 373.46 kB;全部产物 gzip 593.70 → 594.69 kB(+0.99,新块边界的 interop 成本);
|
||
第三方大块 `WarningOutlined-*` 1 217.16 → 1 195.84 kB(十进制 kB,与 `chunk-budget.json` 同一把尺),
|
||
sourcemap 归因:里面 111 只字形 → 87 只,请出去的正是 extra 那 24 只,
|
||
react / @babel/runtime / rc-util 逐模块字节一字不差。
|
||
这笔收益**没有任何编译器会替你守着**,两条静默回退路径都在 typecheck 下活得很好:
|
||
a) 桶里补一句 `export * from './extra'`(看着最无害、IDE 还会自动补全成它)——
|
||
外壳立刻又能按名字引用到那 24 只,`kit` 分组按目录 `src/components/**` 收拢,
|
||
整张 extra.ts 连字形一起焊回首屏。实测这一条 `export *` 值 Δgzip −0.08 kB,
|
||
拆表当场白做(`bundle-icons-split.log` 的 D1)。
|
||
b) 首屏外壳里的某个组件(`app/` · `components/ui/` · `lib/` …)为了一个新按钮
|
||
深 import `icons/extra` —— extra 又回首屏,页面照样绿。
|
||
⇒ 判据三条:① 桶不许 re-export `./extra`;② 从 `main.tsx` 出发**只走静态 import**
|
||
(撞到 `import()` 动态边界就停,与打包器的切块口径一致)可达的文件里,
|
||
谁都不许引用 `icons/extra`;③ 同一个语义名不许同时躺在两张表里
|
||
(同名双出口时「页面拿到的到底是哪一只」取决于打包顺序,肉眼查不出来)。
|
||
另外两个数是「这条闸采到了东西」的证据,不是缺陷:外壳静态闭包的模块数、
|
||
extra 里没有任何调用方的死出口数(今天 0 只——24 只全有真实调用方,逐条 grep 过)。
|
||
反例由 scripts/ui/gateprobe.py ㉖ 注入验证:临时树里把桶改成 `export * from './extra'`、
|
||
把某个外壳文件改成深 import extra,两种都必须点名;整棵树只有 core 时必须静音。 */
|
||
const SPLIT_DEFECTS = []
|
||
{
|
||
const CORE = path.join(REG_DIR, 'core.ts')
|
||
const EXTRA = path.join(REG_DIR, 'extra.ts')
|
||
const BUCKET = path.join(REG_DIR, 'index.ts')
|
||
if (fs.existsSync(CORE) && fs.existsSync(EXTRA)) {
|
||
/** 临时树里跑的时候 `path.relative(FRONTEND, …)` 会吐出一长串 ../..,
|
||
看不见是哪个文件;越界就退回按 src 目录算(gateprobe ㉖ 的点名行靠这个才可读)。 */
|
||
const rel = (f) => { const r = path.relative(FRONTEND, f); return r.startsWith('..') ? path.relative(SRC, f) : r }
|
||
const code = (f) => codeLines(f).join('\n')
|
||
const namesOf = (f) => [...code(f).matchAll(/^export const (Icon\w+)/gm)].map((m) => m[1])
|
||
const coreNames = new Set(namesOf(CORE))
|
||
const extraNames = new Set(namesOf(EXTRA))
|
||
/* ① 桶 */
|
||
if (fs.existsSync(BUCKET) && /from\s+'\.\/extra'/.test(code(BUCKET)))
|
||
SPLIT_DEFECTS.push(`${rel(BUCKET)}: 桶 re-export 了 ./extra —— 那 ${extraNames.size} 只字形会整块焊回首屏`
|
||
+ `(实测这一条 export * 值 Δgzip −0.08 kB,等于白拆);懒页面专属出口请在调用方写全路径 '…/icons/extra'`)
|
||
/* ③ 同名双出口 */
|
||
for (const n of extraNames) if (coreNames.has(n))
|
||
SPLIT_DEFECTS.push(`${n} 同时在 core.ts 与 extra.ts 里登记 —— 桶与深路径会给出两只不同的出口`)
|
||
/* ② 外壳静态闭包 */
|
||
const RESOLVE_EXT = ['', '.ts', '.tsx', '/index.ts', '/index.tsx']
|
||
const RE_STATIC = /(?:^|[\s;}])(?:import|export)\s+(?:[\w*{},\s$]*?\s+from\s*)?["']([^"']+)["']/g
|
||
const resolveSpec = (spec, from) => {
|
||
let base = null
|
||
if (spec.startsWith('@/')) base = path.join(SRC, spec.slice(2))
|
||
else if (spec.startsWith('.')) base = path.resolve(path.dirname(from), spec)
|
||
else return null // 第三方包不是我们的模块,闭包到此为止
|
||
for (const e of RESOLVE_EXT) { const q = base + e; if (fs.existsSync(q) && fs.statSync(q).isFile()) return q }
|
||
return null // CSS / 资源 / 解析不到:不是图标表,跳过
|
||
}
|
||
const MAIN = path.join(SRC, 'main.tsx')
|
||
const closure = new Set()
|
||
if (fs.existsSync(MAIN)) {
|
||
const queue = [MAIN]
|
||
while (queue.length) {
|
||
const f = queue.pop()
|
||
if (closure.has(f)) continue
|
||
closure.add(f)
|
||
const text = code(f)
|
||
// 只沿**静态** import 走;`import()` 是切块边界,对面那一坨不在首屏里,不进闭包
|
||
for (const m of text.matchAll(RE_STATIC)) { const r = resolveSpec(m[1], f); if (r && !closure.has(r)) queue.push(r) }
|
||
}
|
||
for (const f of closure) {
|
||
for (const m of code(f).matchAll(RE_STATIC)) {
|
||
if (resolveSpec(m[1], f) === EXTRA)
|
||
SPLIT_DEFECTS.push(`${rel(f)} 在**首屏静态闭包**里引用 icons/extra —— extra 的字形会跟着回到首屏`
|
||
+ `(收益 −4.74 kB gzip 当场作废)。要么这只图形本来就属于外壳(挪进 core.ts 并同步这里),`
|
||
+ `要么调用方本身该是懒加载的`)
|
||
}
|
||
}
|
||
}
|
||
/* 证据计数:extra 里没人用的出口 */
|
||
const outside = []
|
||
const walkAll = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const q = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkAll(q); continue }
|
||
if (/\.(tsx|ts)$/.test(ent.name) && !inRegistryDir(q)) outside.push(code(q))
|
||
}
|
||
}
|
||
walkAll(SRC)
|
||
const allCode = outside.join('\n')
|
||
const deadExtra = [...extraNames].filter((n) => !new RegExp(`\\b${n}\\b`).test(allCode))
|
||
for (const n of deadExtra) SPLIT_DEFECTS.push(`extra.ts 的 ${n} 没有任何调用方 —— 删掉它,别留着当摆设`)
|
||
for (const hit of SPLIT_DEFECTS) console.log(`ICONS ${hit}`)
|
||
console.log(`${SPLIT_DEFECTS.length} 处两级表边界问题(第 2 条·附三:core ${coreNames.size} 只 / extra ${extraNames.size} 只 ·`
|
||
+ ` 首屏静态闭包 ${closure.size} 个模块 · extra 死出口 ${deadExtra.length} 只)`)
|
||
} else {
|
||
console.log(`两级表结构不完整(缺 ${fs.existsSync(CORE) ? 'extra.ts' : 'core.ts'})—— 第 2 条·附三这条闸今天没东西可量,请确认是布局变了还是文件被误删`)
|
||
}
|
||
}
|
||
|
||
/* ── 第 3 条:裸 <button> 收口到自定义表面族 ──────────────────────────────
|
||
每条都要写清「为什么不是 antd Button」;新增文件先进这张表,别先写裸按钮。
|
||
Controls.tsx(阶段 6 已拆成 Ctlbar.tsx + Fields.tsx)曾在表里:Chip / TagChip 后来全部改由 antd `Tag` /
|
||
`Tag.CheckableTag` / `Typography.Link` 承担,裸 <button> 命中数归零,条目已删。 */
|
||
const CUSTOM_SURFACES = {
|
||
'src/components/ui/Overlay.tsx': 'Scrim:整屏遮罩,点空白关闭;不是按钮,是命中区',
|
||
'src/components/media/MediaThumb.tsx': '版本角标 + 删除叉:压在封面上的徽标,尺寸由封面决定',
|
||
'src/components/media/AudioPlay.tsx': '走带键:三档直径 28/22/20 由音频行高决定,antd 圆钮最矮 24',
|
||
'src/components/media/MediaLightbox.tsx': '灯箱左右翻页:压在媒体上的 40px 玻璃圆钮 + 悬停位移',
|
||
'src/components/media/VersionPanel.tsx': '版本封面缩略:图块本身是热区,光标是 zoom-in',
|
||
'src/pages/project/Storyboard.tsx': '注释里解释「卡片为什么不是 button」——命中数为 0 时删条目',
|
||
/* 2026-11-10 · 阶段 35 · T1-N4:「显示密码」那颗从 <span role=img tabindex=-1> 换成
|
||
真 <button>(Tab 到得了 + `aria-pressed` 报明文/密文状态)。为什么不是 kit <Button>:
|
||
antd `Input.Password` 用 `cloneElement(icon, { className: 'ant-input-password-icon', … })`
|
||
把 className **整个覆盖**(`antd/es/input/Password.js:64-83`)⇒ 自定义类留不住,
|
||
归零只能靠内联;换 antd Button 会带进自己的 32px 盒与悬停底,输入框后缀就变形了。
|
||
下面那条 `nex-*` 标记检查在本文件里由取证路径 `nex-p35/vo/eye-geom-*.json` 满足
|
||
(清注释时注意:真被撞掉就把标记补到 `.ant-input-password-icon` 的局部样式上)。 */
|
||
'src/components/forms/SecretInput.tsx': '显示密码开关:antd 后缀槽里的零皮肤命中区,className 会被 antd cloneElement 覆盖',
|
||
}
|
||
|
||
/** 宿主元素不是字面量 <button> 的文件:Tile 用 <As>,As 可以是 button。
|
||
它们同样要带 nex-* 标记,但不参与「字面量命中」对账。 */
|
||
const HOST_ELEMENT = {
|
||
'src/components/ui/Tile.tsx': 'Tile:整块热区(瓷贴/导航行/账号胶囊),宿主标签由 onClick 决定',
|
||
}
|
||
|
||
const bare = []
|
||
const hits = new Map()
|
||
const walkButtons = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkButtons(p); continue }
|
||
if (!/\.tsx$/.test(ent.name)) continue
|
||
const rel = path.relative(FRONTEND, p)
|
||
const lines = fs.readFileSync(p, 'utf8').split('\n')
|
||
lines.forEach((line, i) => {
|
||
if (/^\s*(\*|\/\/|\/\*|#)/.test(line)) return // 注释里提到 button 不算
|
||
if (!/<button(\s|>|$)/.test(line)) return
|
||
if (/<\/button>|^\s*return <button/.test(line) || true) {
|
||
if (!hits.has(rel)) hits.set(rel, [])
|
||
hits.get(rel).push(i + 1)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
walkButtons(SRC)
|
||
|
||
const ALLOWED = { ...CUSTOM_SURFACES, ...HOST_ELEMENT }
|
||
for (const [rel, rows] of [...hits].sort()) {
|
||
if (!ALLOWED[rel]) {
|
||
bare.push(`${rel}:${rows.join(',')} — 裸 <button> 不在自定义表面族白名单里,换 <Button>`)
|
||
continue
|
||
}
|
||
const src = fs.readFileSync(path.join(FRONTEND, rel), 'utf8')
|
||
if (!/\bnex-[a-z]/.test(src)) {
|
||
bare.push(`${rel} — 白名单里的文件一个 nex-* 标记类都没有了,要么补标记要么从白名单删除`)
|
||
}
|
||
}
|
||
/* 白名单要诚实:条目命中数为 0 就说明这一族已经被 antd 取代,打出来提醒删除,
|
||
但不算失败——留一天的空条目比漏一颗裸按钮危害小。 */
|
||
for (const rel of Object.keys(HOST_ELEMENT)) {
|
||
if (CUSTOM_SURFACES[rel]) bare.push(`${rel} — 同一个文件既在白名单又在宿主元素表,删一个`)
|
||
}
|
||
for (const hit of bare) console.log(`BTN ${hit}`)
|
||
const stale = Object.keys(CUSTOM_SURFACES).filter((k) => !hits.has(k))
|
||
console.log(`${bare.length} 处未收口的裸 <button>(白名单 ${Object.keys(CUSTOM_SURFACES).length} 个文件 / 实际命中 ${hits.size} 个)`
|
||
+ (stale.length ? `\n 可清理的白名单条目:${stale.join(' ')}` : ''))
|
||
|
||
/* ── 第 4 条:浮层底部动作条 ────────────────────────────────────────────
|
||
口径的权威解释在 components/ui/Overlay.tsx 的 DRAWER_ACTIONS 上方;
|
||
这里只负责让违反口径的代码无法合入。 */
|
||
|
||
/** 从 src[open](必须是 '{')找到配对的 '}',返回下标;找不到返回 -1。
|
||
字符串/模板字面量整段跳过,JSX 表达式里的 } 用深度计数处理。 */
|
||
function braceEnd(src, open) {
|
||
let depth = 0
|
||
for (let i = open; i < src.length; i++) {
|
||
const c = src[i]
|
||
if (c === '"' || c === "'" || c === '`') {
|
||
const q = c
|
||
i++
|
||
while (i < src.length) {
|
||
if (src[i] === '\\') { i += 2; continue }
|
||
if (src[i] === q) break
|
||
i++
|
||
}
|
||
continue
|
||
}
|
||
if (c === '{') depth++
|
||
else if (c === '}' && --depth === 0) return i
|
||
}
|
||
return -1
|
||
}
|
||
|
||
/** 取 <Button …> 的开标签全文(含 '>')。深度 > 0 时不认 '>',
|
||
所以 onClick={() => x} 里的箭头不会被误当成开标签结束。 */
|
||
function openTag(src, start) {
|
||
let depth = 0
|
||
for (let i = start + 1; i < src.length; i++) {
|
||
const c = src[i]
|
||
if (c === '"' || c === "'" || c === '`') {
|
||
const q = c
|
||
i++
|
||
while (i < src.length) {
|
||
if (src[i] === '\\') { i += 2; continue }
|
||
if (src[i] === q) break
|
||
i++
|
||
}
|
||
continue
|
||
}
|
||
if (c === '{') depth++
|
||
else if (c === '}') depth--
|
||
else if (c === '>' && depth === 0) return src.slice(start, i + 1)
|
||
}
|
||
return ''
|
||
}
|
||
|
||
/** 动作条的例外:整条只有一个动作、或根本不是动作条(只是把按钮当装饰塞进标题)。
|
||
每条都要写理由,别用它绕过口径。 */
|
||
const FOOTER_EXEMPT = new Set([])
|
||
|
||
/**
|
||
* 第 4 条量的是**浮层底部**(规范 §10),不是所有叫 actions / footer 的 prop。
|
||
*
|
||
* 症状:`\b(actions|footer)=\{` 这个名字碰撞把 `PageHead actions`(页头那排开关)
|
||
* 和 `Toolbar actions`(勾选后浮出的批量条)也吞了进来,报出
|
||
* 「最后一颗必须是主行动」——而页头最后一颗本来就该是「收起概况」这种
|
||
* 视图开关,主行动在 `PanelHead right`。批量条的「批量删除」是**未确认的
|
||
* 行内删除**(§10:那种才用 `type="text" danger`),也不该被判成权重反了。
|
||
* 做法:先定位 prop 属于哪个元素,只有 `<Drawer actions>` / `<Modal footer>`
|
||
* (含 kit 内部转发的 `<AntDrawer>` / `<AntModal>`)才是底部动作条。
|
||
* 实测全站 25 处:Drawer 6 + Modal 5 + AntModal 1 = 12 处受管,
|
||
* PageHead 5 + Toolbar 7 归 §7 的控件栏口径管,不在这里查。
|
||
*/
|
||
const FOOTER_HOSTS = new Set(['Drawer', 'Modal', 'AntDrawer', 'AntModal'])
|
||
|
||
/** 找 `at` 这个位置被哪个 JSX 元素包着(取起点最近的那个开标签)。 */
|
||
function hostOf(src, tags, at) {
|
||
let host = ''
|
||
let best = -1
|
||
for (const t of tags) if (t.start < at && t.end > at && t.start > best) { host = t.name; best = t.start }
|
||
return host
|
||
}
|
||
|
||
const footerViolations = []
|
||
const walkFooters = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkFooters(p); continue }
|
||
if (!/\.tsx$/.test(ent.name)) continue
|
||
const rel = path.relative(FRONTEND, p)
|
||
const src = fs.readFileSync(p, 'utf8')
|
||
const openTags = [...src.matchAll(/<(Drawer|Modal|AntDrawer|AntModal)(?=[\s/>])/g)]
|
||
.map((t) => ({ name: t[1], start: t.index, end: t.index + openTag(src, t.index).length }))
|
||
.filter((t) => t.end > t.start)
|
||
for (const m of src.matchAll(/\b(actions|footer)=\{/g)) {
|
||
if (!FOOTER_HOSTS.has(hostOf(src, openTags, m.index))) continue
|
||
const open = m.index + m[0].length - 1
|
||
const close = braceEnd(src, open)
|
||
if (close < 0) continue
|
||
const block = src.slice(open, close)
|
||
const tags = []
|
||
for (const b of block.matchAll(/<Button(?=[\s/>])/g)) {
|
||
const at = open + b.index
|
||
tags.push({ raw: openTag(src, at), line: src.slice(0, at).split('\n').length })
|
||
}
|
||
if (tags.length < 2) continue // 单动作不违反「取消在前」
|
||
if (FOOTER_EXEMPT.has(`${rel}:${m[1]}`)) continue
|
||
const at = (n) => `${rel}:${tags[0].line} (${m[1]}) ${n}`
|
||
const sizeHit = tags.find((t) => /\bsize="/.test(t.raw))
|
||
if (sizeHit) footerViolations.push(at('① 浮层底部一律 md 档,别写 size'))
|
||
const primaries = tags.filter((t) => /type="primary"/.test(t.raw))
|
||
if (primaries.length > 1) footerViolations.push(at(`② 一条底部最多一颗 type="primary",当前 ${primaries.length} 颗`))
|
||
for (const t of tags.filter((x) => /\bdanger\b/.test(x.raw))) {
|
||
if (!/type="primary"/.test(t.raw)) {
|
||
footerViolations.push(`${rel}:${t.line} (${m[1]}) ③ 破坏性动作用 type="primary" danger`
|
||
+ '——裸描边按钮比「取消」还轻,确认权重就反了')
|
||
}
|
||
}
|
||
const last = tags[tags.length - 1]
|
||
if (!/type="primary"/.test(last.raw)) {
|
||
footerViolations.push(`${rel}:${last.line} (${m[1]}) ④ 最后一颗必须是主行动 type="primary"`
|
||
+ '(DOM 顺序 = 视觉顺序:辅助 → 取消 → 主行动)')
|
||
}
|
||
}
|
||
}
|
||
}
|
||
walkFooters(SRC)
|
||
for (const hit of footerViolations) console.log(`FOOT ${hit}`)
|
||
console.log(`${footerViolations.length} 处不符合浮层底部动作口径(只查 Drawer actions / Modal footer:PageHead · Toolbar 的 actions 属 §7 控件栏)`)
|
||
|
||
/* ── 第 5 条:`import 'antd'` 只许出现在尺子、外壳与 kit 里(R8)────────────
|
||
页面一旦直接 import 组件库,主题与档位就再也收不口:换 antd 大版本、调 token、
|
||
甚至只是想让「按钮三档高度」跟着系统走,都得把 20 多个页面重扫一遍。
|
||
白名单只有三处:theme/(尺子本体)· app/(外壳没有第二份实现,直接铺 Layout/Menu)·
|
||
components/{ui,forms}(kit 唯一封装层)。
|
||
以前这条只写在文档里,没人拦——补成闸。
|
||
⚠ 2026-11 第十三轮删掉了第四处 `stores/notice.ts`:它原先是 antd `message` 的唯一出口,
|
||
吐司改成自绘队列(stores/notice.ts + components/ui/Toast.tsx)以后它不再 import antd,
|
||
留着这条等于给「以后谁想在 store 里 import 组件库」开后门。 */
|
||
const ANTD_OK = (rel) => /^(theme|app)\//.test(rel) || /^components\/(ui|forms)\//.test(rel)
|
||
const antdStray = []
|
||
const walkAntd = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkAntd(p); continue }
|
||
if (!/\.(tsx|ts)$/.test(ent.name)) continue
|
||
const rel = path.relative(FRONTEND, p).replace(/^src\//, '')
|
||
if (ANTD_OK(rel)) continue
|
||
fs.readFileSync(p, 'utf8').split('\n').forEach((line, i) => {
|
||
if (/from 'antd'/.test(line)) antdStray.push(`${rel}:${i + 1}: ${line.trim()}`)
|
||
})
|
||
}
|
||
}
|
||
walkAntd(SRC)
|
||
for (const hit of antdStray) console.log(`ANTD ${hit}`)
|
||
console.log(`${antdStray.length} 处越界的 import 'antd'(R8:只许 theme/ · app/ · components/{ui,forms})`)
|
||
|
||
/* ── 第 6 条:反馈四级表面各有唯一实现(R19)────────────────────────────
|
||
症状(2026-11 第十三轮 · 用户原话「整体修改系统中的所以提示窗、完成后的反馈tips等。
|
||
不要使用默认组件」):同一句反馈有过五种长相——浏览器原生的 `window.confirm`
|
||
灰条(字体、按钮、层级全是操作系统的)、antd 顶部那行 `message.success` 小灰字、
|
||
`Modal.confirm` 的默认问话框、`notification` 的右上大卡、页面自拼的
|
||
`border + warning-bg`。读者分不清刚才那句话是不是系统说的,破坏性确认
|
||
还经常被当成浏览器自己弹的广告直接关掉。
|
||
做法:四级表面各留唯一出口——行内 `ErrorText`/`LineHint`(贴在控件旁)
|
||
→ `Notice` 提示条(贴在内容块上方)→ `notify*`(全局吐司,右下角)
|
||
→ `confirmAction`(破坏性操作确认框)。其余写法一律判违规。
|
||
为什么用正则不用 AST:这些都是**函数名 / 组件名**层面的口径,
|
||
正则命中的就是要禁的那几个名字,AST 反而要多写一层解析;
|
||
漏报的代价是全站又长出第五套皮肤,误报的代价只是往白名单加一行。
|
||
白名单是实现本体:吐司 / 确认框 / 提示条三个组件 + 两个 store
|
||
(提示条 `Notice` 自阶段 6 起住在 `components/ui/State.tsx`,白名单同步跟上)。
|
||
2026-11-07(阶段 5):正则原本只匹配字面 `window.confirm(`,浏览器里裸调用
|
||
`confirm(...)` 是**同一个**原生对话框,Library.tsx 因此漏过 5 处。
|
||
放宽后的口径 = 「前面不是 . / $ / 词字符,可带 window. 前缀」:
|
||
/(?<![\w.$])(?:window\.)?(?:confirm|alert)\s*\(/
|
||
十一例自查(改的时候别再退回旧写法):
|
||
命中 5 —— window.confirm( / confirm( / !confirm(` / window.alert( / alert(
|
||
放行 6 —— confirmAction( / Modal.confirm( / lib.confirm( / notconfirm( / myalert( / x.window.confirm(
|
||
(`(?<![\w.$])` 放在**可选前缀之前**:写成 `(?:\bwindow\.)?(?<![\w.$])` 会让
|
||
前缀吃掉 `window.` 之后回头看那个点,把 window.confirm( 整个漏掉——阶段 5 先踩过一次。) */
|
||
const FB_IMPL = /^components\/ui\/(Toast|Confirm|State|Panel|Button|tone)\.tsx$|^stores\/(notice|confirm)\.ts$/
|
||
const FB_RULES = [
|
||
[/(?<![\w.$])(?:window\.)?(?:confirm|alert)\s*\(/, '浏览器原生 confirm / alert(含省略 window. 的裸调用)'],
|
||
[/\bModal\.confirm\s*\(/, 'antd Modal.confirm 默认问话框'],
|
||
[/\bmessage\.(success|error|warning|info|loading|open|destroy)\s*\(/, 'antd message 顶部小灰字'],
|
||
[/\bnotification\.(open|success|error|warning|info)\s*\(/, 'antd notification 右上大卡'],
|
||
[/<Alert(?=[\s/>])/, '裸 antd Alert(语义提示只有一个名字:Notice)']
|
||
]
|
||
const fbHits = []
|
||
const walkFb = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkFb(p); continue }
|
||
if (!/\.(tsx|ts)$/.test(ent.name)) continue
|
||
const rel = path.relative(FRONTEND, p).replace(/^src\//, '')
|
||
if (FB_IMPL.test(rel)) continue
|
||
fs.readFileSync(p, 'utf8').split('\n').forEach((line, i) => {
|
||
const code = line.replace(/\/\/.*$/, '')
|
||
for (const [re, why] of FB_RULES) {
|
||
if (re.test(code)) fbHits.push(`${rel}:${i + 1}: ${why} → ${code.trim().slice(0, 72)}`)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
walkFb(SRC)
|
||
for (const hit of fbHits) console.log(`FEED ${hit}`)
|
||
console.log(`${fbHits.length} 处绕开反馈四级表面(R19:ErrorText/LineHint → Notice → notify* → confirmAction)`)
|
||
|
||
/* ── 第 7 条:门牌与卡头标题不挂前置图形(2026-11 第十四轮)───────────────
|
||
症状(用户原话「不要图标」,附项目总控台那颗品牌 N 的门牌截图):页头标题左边
|
||
那块主色方块(反查菜单表拿到的图形)说的是左栏高亮项已经说过的一句话,
|
||
同屏还有统计卡色块、卡头品牌 N 三颗同形状方块互相抢焦点;
|
||
它还把标题整体右推 42px,门牌与正文的左缘因此对不上。
|
||
做法:`PageHead` 的 icon / 反查机制、`.nex-pagehead-mark` 整层删除;
|
||
`<Mark>` 只剩登录页那颗品牌方牌。闸门钉住三件事:
|
||
① `<PageHead>` 不许再写 `icon=`;② 谁都不许再写 `nex-pagehead-mark` 类名;
|
||
③ `<Mark>` 只许出现在 pages/Login.tsx——一旦第二处开始用它当「标题装饰」,
|
||
这一轮删掉的东西就会从那个文件长回来。 */
|
||
const MARK_OK = new Set(['pages/Login.tsx'])
|
||
const headHits = []
|
||
const walkHead = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkHead(p); continue }
|
||
if (!/\.tsx$/.test(ent.name)) continue
|
||
const rel = path.relative(FRONTEND, p).replace(/^src\//, '')
|
||
const src = fs.readFileSync(p, 'utf8')
|
||
src.split('\n').forEach((line, i) => {
|
||
if (/nex-pagehead-mark/.test(line) && !/^\s*[*/]/.test(line)) {
|
||
headHits.push(`${rel}:${i + 1}: 门牌前置图形的类名又回来了(第十四轮已删这一层)`)
|
||
}
|
||
if (/<Mark(?=[\s>])/.test(line) && !MARK_OK.has(rel) && !/^\s*[*/]/.test(line)) {
|
||
headHits.push(`${rel}:${i + 1}: <Mark> 只许登录页当品牌方牌用,标题前不再挂方块图形`)
|
||
}
|
||
})
|
||
for (const m of src.matchAll(/<PageHead(?=[\s/>])/g)) {
|
||
// 只看 PageHead **自己那一层**的 prop:actions={<Button icon={…}/>} 里的 icon=
|
||
// 属于里面那颗按钮(R15 要求按钮带图标),不算门牌的图形——所以要跟着花括号深度走。
|
||
const tag = openTag(src, m.index)
|
||
let depth = 0
|
||
let own = ''
|
||
for (const ch of tag) {
|
||
if (ch === '{') depth++
|
||
else if (ch === '}') depth--
|
||
else if (depth === 0) own += ch
|
||
}
|
||
if (/\bicon=/.test(own)) {
|
||
headHits.push(`${rel}:${src.slice(0, m.index).split('\n').length}: <PageHead icon=> 已废弃(门牌不挂图形)`)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
walkHead(SRC)
|
||
for (const hit of headHits) console.log(`HEAD ${hit}`)
|
||
console.log(`${headHits.length} 处标题前置图形(第十四轮:门牌 = 一级标题 + 右端主操作,没有图标)`)
|
||
|
||
|
||
/* ── 第 8 条:标签的 className 只许写排布类,不许写皮肤类(2026-11-07 · D16 修复)──
|
||
症状:kit 的 `Tag` 形参收了 className 却没并进 cn(),页面写 `<Tag className="ml-2">`
|
||
从 2026-11 起一直是空转(缺陷 D16,`docs/NEX-对照与例外.md` §6)。修好之后
|
||
新问题换了形状:**能不能点亮取决于属性有没有被 `.nex-tag` 独占**——
|
||
`index.css:55` 的 `@import "./app.css"` 不带 layer,未层声明高于任何 @layer,
|
||
所以 display / background / color / border-radius / padding-inline / font-size /
|
||
line-height / min-height / gap / max-width / white-space / overflow /
|
||
margin-inline-end 这十三族写上去照样是空转(明暗两档逐属性实测,
|
||
`docs/UI规范.md` §1.1.1 那张表)。
|
||
做法:把这十三族钉成禁写词。命中就是「页面以为自己在改皮肤,其实什么都没发生」,
|
||
该改 tone(accent / mono 等登记过的色调)或者回到壳里改令牌,不是再套一层 !important。
|
||
为什么只扫 kit 标签:`components/ui/Ctlbar.tsx` 里的 `Tag` 是 antd 本尊(它自己写
|
||
`nex-tag-chip` 皮肤类),不吃这条规矩;只有从 components/ui 取 Tag/Status 的文件
|
||
才是这次修的那个壳。 */
|
||
const SKIN_TOKEN = [
|
||
[/^hidden$/, 'display(壳是 inline-flex)'],
|
||
[/^(?:max|min)-[\w-]+:hidden$/, '响应式 display(同上,要藏在 wrapper 上)'],
|
||
[/^bg-/, 'background(§2.1:标签只有一档中性底)'],
|
||
[/^text-/, 'color / font-size(皮肤与字号都由壳定)'],
|
||
[/^rounded/, 'border-radius'],
|
||
[/^(?:p|px|pe)-/, 'padding-inline(壳用 --tag-px 两档下发)'],
|
||
[/^leading-/, 'line-height'],
|
||
[/^min-h-/, 'min-height(壳用 --tag-h 两档下发)'],
|
||
[/^gap-/, 'gap(壳用 --tag-gap)'],
|
||
[/^max-w-/, 'max-width(壳定 100%,防长标签捅出容器)'],
|
||
[/^whitespace-/, 'white-space(壳 nowrap)'],
|
||
[/^overflow-/, 'overflow(壳 hidden 配合内部 ellipsis)'],
|
||
[/^(?:mr|me)-/, 'margin-inline-end(壳归零,成排间距归 TagGroup 管)']
|
||
]
|
||
const tagSkinHits = []
|
||
const walkTagSkin = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkTagSkin(p); continue }
|
||
if (!/\.tsx$/.test(ent.name)) continue
|
||
const rel = path.relative(FRONTEND, p).replace(/^src\//, '')
|
||
const raw = fs.readFileSync(p, 'utf8')
|
||
// 只认真正用 kit 壳的文件:import 行里从 components/ui 取了 Tag 或 Status
|
||
const usesKit = raw.split('\n').some((l) =>
|
||
/^import\s/.test(l) && /from '[^']*components\/ui/.test(l) && /\b(Tag|Status)\b/.test(l))
|
||
if (!usesKit) continue
|
||
const src = raw.replace(/\/\*[\s\S]*?\*\//g, '') // 注释里写 <Tag className="bg-…"> 是解释,不是违规
|
||
for (const m of src.matchAll(/<(Tag|Status)(?=[\s/>])/g)) {
|
||
const tag = openTag(src, m.index)
|
||
let depth = 0, own = ''
|
||
for (const ch of tag) {
|
||
if (ch === '{') depth++
|
||
else if (ch === '}') depth--
|
||
else if (depth === 0) own += ch
|
||
}
|
||
const attr = own.match(/\bclassName=\{?"([^"]*)"|\bclassName=\{(.*?)\}\}/s)
|
||
if (!attr) continue
|
||
const bag = (attr[1] || attr[2] || '')
|
||
.replace(/\b(?:cn|clsx)\(/g, ' ')
|
||
.match(/['"`]([^'"`]*)['"`]/g)
|
||
const tokens = (bag || []).join(' ').replace(/['"`]/g, ' ').split(/\s+/).filter(Boolean)
|
||
const line = src.slice(0, m.index).split('\n').length
|
||
for (const t of tokens) {
|
||
const bare = t.replace(/^[!]/, '').replace(/^\w+:/, '') // 变体前缀与 important 后缀都改变「写的是哪个属性」
|
||
for (const [re, why] of SKIN_TOKEN) {
|
||
if (re.test(bare) || re.test(t)) {
|
||
tagSkinHits.push(`${rel}:${line}: <${m[1]} className 写了 ${t}> —— ${why},`.replace(/\s+$/, '') +
|
||
`未层壳压不住(UI规范 §1.1.1);要显眼用 tone,要隐藏套 span`)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
walkTagSkin(SRC)
|
||
for (const hit of tagSkinHits) console.log(`TAGSKIN ${hit}`)
|
||
console.log(`${tagSkinHits.length} 处标签 className 写皮肤类(第 8 闸:display/底/色/圆角/内距/字号…由 .nex-tag 独占,写上去是空转)`)
|
||
|
||
|
||
/* ── 第 9 条:断点只许用「登记过的名字」(2026-11-07 · N16 断点治理)────────────
|
||
症状:阶段 7 把页面里 5 把裸 `max-[1240px]` 改名成 `--breakpoint-*` 命名变体之后,
|
||
「登记」这件事只剩两份人写的表(`styles/index.css` 的 @theme、`UI规范.md` §4
|
||
的登记表)和一句 `grep -rn "max-\[" frontend/src`。没有任何机器闸:
|
||
下一轮谁再写一个 `max-[900px]:flex`,编译得过、像素也对,只有人眼能发现;
|
||
反过来往 @theme 加一档却忘了写进 §4、或者忘了同步 `visualcheck.py` 的 `BP`,
|
||
`wantDisplay` 会对这一档**静默返回 null**——断言悄悄不跑了,比报错更糟。
|
||
做法:尺子的唯一真相是 `@theme` 里的 `--breakpoint-*`,其余四处(页面用词 / UI规范 §4
|
||
登记表 / 探针 BP 表 / 尺子自己的使用面)全部由这条闸向它对账:
|
||
① 页面与样式里不许再出现裸任意断点 `max-[…]` / `min-[…]`;
|
||
② 用到的变体名必须在 @theme 里存在(写错名字 = 这一档根本不会生成规则);
|
||
③ 登记断点(非主阶梯那几把)必须**只有一个使用者**、必须被 §4 表收录、
|
||
且不能是「登记了却没人用」的死尺子(UI规范 §4 三条口径逐条机器化);
|
||
④ 每一档都要出现在 `visualcheck.py` 的 `BP` 与 `BPORDER` 里,且 BPORDER
|
||
必须与「按 px 从大到小」一致(顺序错了 wantDisplay 会挑错赢家);
|
||
⑤ CSS 侧 `@media (max-width: Npx)` 的 N 必须是登记过的值——闸门同样管得住 .css,
|
||
否则页面归了名、样式表里再开一批私档就白干。
|
||
为什么不在这里禁「主阶梯档位用得对不对」:那是 R9 的语义判断(该不该在这一档换排布),
|
||
机器量不出来;这条闸只管「名字存不存在、有没有登记、有没有人用、探针认不认」。 */
|
||
const THEME_FILE = path.join(FRONTEND, 'src/styles/index.css')
|
||
const SPEC_DOC = path.resolve(HERE, '../../docs/UI规范.md')
|
||
const VISUAL_PROBE = path.join(HERE, 'visualcheck.py')
|
||
/** 主阶梯 = antd Grid 的档位(`@theme` 与 antd 同源);其余一律算「登记断点」 */
|
||
const MAIN_LADDER = new Set(['sm', 'md', 'lg', 'xl', '2xl'])
|
||
/** `max-width:` / `min-height:` 这类是 CSS 属性,不是断点变体(正则不吃上下文,只能点名放过) */
|
||
const NOT_A_BP = new Set(['width', 'height', 'inline', 'block', 'inline-size', 'block-size',
|
||
'w', 'h', 'min', 'max', '-content', 'vw', 'vh', 'unit'])
|
||
|
||
const themeCss = fs.readFileSync(THEME_FILE, 'utf8')
|
||
const bpTable = new Map() // 名字 → px:全站断点的唯一真相
|
||
for (const m of themeCss.matchAll(/--breakpoint-([a-z0-9-]+)\s*:\s*([0-9.]+)px/g)) {
|
||
bpTable.set(m[1], Number(m[2]))
|
||
}
|
||
const registered = [...bpTable.keys()].filter((n) => !MAIN_LADDER.has(n))
|
||
const bpValues = new Set([...bpTable.values()].map(String))
|
||
|
||
const specDoc = fs.existsSync(SPEC_DOC) ? fs.readFileSync(SPEC_DOC, 'utf8') : ''
|
||
const visualSrc = fs.existsSync(VISUAL_PROBE) ? fs.readFileSync(VISUAL_PROBE, 'utf8') : ''
|
||
const probeOrder = ((visualSrc.match(/const BPORDER = \[([^\]]*)\]/) || [, ''])[1]
|
||
.match(/'[^']*'|"[^"]*"/g) || []).map((x) => x.slice(1, -1))
|
||
const probeBP = new Map()
|
||
for (const m of ((visualSrc.match(/const BP = \{([^}]*)\}/s) || [, ''])[1]
|
||
.matchAll(/'([a-z0-9-]+)'\s*:\s*([0-9.]+)|(?<![^\s,{])"([a-z0-9-]+)"\s*:\s*([0-9.]+)|([a-z][a-z0-9-]*)\s*:\s*([0-9.]+)/g))) {
|
||
probeBP.set(m[1] || m[3] || m[5], Number(m[2] || m[4] || m[6]))
|
||
}
|
||
|
||
const bpHits = []
|
||
const bpUsers = new Map(registered.map((n) => [n, new Map()])) // 名字 → 文件 → 处数
|
||
const walkBP = (dir) => {
|
||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const p = path.join(dir, ent.name)
|
||
if (ent.isDirectory()) { walkBP(p); continue }
|
||
if (!/\.(tsx|ts|css)$/.test(ent.name)) continue
|
||
const rel = path.relative(FRONTEND, p)
|
||
let inBlock = false
|
||
fs.readFileSync(p, 'utf8').split('\n').forEach((raw, i) => {
|
||
const [line, block] = stripComments(raw, () => inBlock)
|
||
inBlock = block
|
||
if (!line.trim()) return
|
||
const at = `${rel}:${i + 1}`
|
||
/* ① 裸任意断点 */
|
||
for (const m of line.matchAll(/\b(?:max|min)-\[/g)) {
|
||
bpHits.push(`${at}: 写了裸任意断点 ${line.slice(m.index).split(/[\s'"`)/]+/)[0]}`
|
||
+ ` —— 主阶梯放不下就按 UI规范 §4 登记成 --breakpoint-*,别在页面里造私档`)
|
||
}
|
||
/* ② 变体名必须在 @theme 里 */
|
||
for (const m of line.matchAll(/\b(?:max|min)-([a-z0-9][a-z0-9-]*):/g)) {
|
||
const name = m[1]
|
||
if (NOT_A_BP.has(name)) continue
|
||
if (!bpTable.has(name)) {
|
||
bpHits.push(`${at}: 用了没登记的断点名 ${m[0]}`
|
||
+ ` —— styles/index.css 的 --breakpoint-${name} 不存在,这一档压根不生成规则`)
|
||
continue
|
||
}
|
||
/* ③ 登记断点的使用者 */
|
||
if (!MAIN_LADDER.has(name)) {
|
||
const per = bpUsers.get(name)
|
||
per.set(rel, (per.get(rel) || 0) + 1)
|
||
}
|
||
}
|
||
/* ⑤ CSS 侧 @media 的像素值 */
|
||
if (/\.css$/.test(ent.name)) {
|
||
for (const m of line.matchAll(/@media[^{]*\((?:max|min)-width:\s*([0-9.]+)px\)/g)) {
|
||
if (!bpValues.has(m[1])) {
|
||
bpHits.push(`${at}: @media 用了没登记的值 ${m[1]}px`
|
||
+ ` —— 样式表里的私档同样算私档(UI规范 §4)`)
|
||
}
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
walkBP(SRC)
|
||
/* ③ 三条登记口径:有人用、只有一个使用者、被 §4 表收录 */
|
||
for (const [name, per] of bpUsers) {
|
||
const px = bpTable.get(name)
|
||
if (!per.size) {
|
||
bpHits.push(`styles/index.css: 断点 ${name}(${px}px) 登记了却 0 个使用者`
|
||
+ ` —— 删档要连着它唯一的使用者一起删(§4 口径 ③)`)
|
||
}
|
||
if (per.size > 1) {
|
||
bpHits.push(`断点 ${name}(${px}px) 有 ${per.size} 个使用者:`
|
||
+ [...per.keys()].join(' / ')
|
||
+ ` —— 一把尺子只服务一个表面(§4 口径 ①),要共用先把它升级成主阶梯档位`)
|
||
}
|
||
if (specDoc && !new RegExp('^\\|\\s*`' + name + '`\\s*\\|', 'm').test(specDoc)) {
|
||
bpHits.push(`docs/UI规范.md §4: 登记表里没有 ${name}(${px}px)`
|
||
+ ` —— @theme 加一档必须同时留下「相邻档位为什么不行」的实测行`)
|
||
}
|
||
}
|
||
/* ④ 探针的断点表必须与 @theme 同名同值同序 */
|
||
for (const [name, px] of bpTable) {
|
||
if (!probeBP.has(name)) bpHits.push(`scripts/ui/visualcheck.py: BP 表里没有 ${name}`
|
||
+ ` —— wantDisplay 会对这一档静默返回 null,断言等于没跑`)
|
||
if (visualSrc && !probeOrder.includes(name)) bpHits.push(`scripts/ui/visualcheck.py: BPORDER 里没有 ${name}`
|
||
+ ` —— 少了名字 max-*/min-* 的赢家会算错`)
|
||
else if (probeBP.has(name) && probeBP.get(name) !== px) {
|
||
bpHits.push(`scripts/ui/visualcheck.py: BP.${name}=${probeBP.get(name)} 与 styles/index.css 的 ${px} 不一致`
|
||
+ ` —— 探针按旧尺子量,页面按新尺子排`)
|
||
}
|
||
}
|
||
if (probeOrder.length) {
|
||
const sorted = [...bpTable.keys()].sort((a, b) => bpTable.get(b) - bpTable.get(a))
|
||
if (probeOrder.join(',') !== sorted.join(',')) {
|
||
bpHits.push(`scripts/ui/visualcheck.py: BPORDER 顺序应为 ${sorted.join(', ')}`
|
||
+ `(按 px 从大到小),现在是 ${probeOrder.join(', ')}`)
|
||
}
|
||
}
|
||
for (const hit of bpHits) console.log(`BP ${hit}`)
|
||
console.log(`${bpHits.length} 处断点越界(第 9 闸:${bpTable.size} 档在册 / ${registered.length} 把登记尺子`
|
||
+ ` / 裸 max-[…] 与未登记名与文档-探针失同步)`)
|
||
|
||
|
||
/* ── 收尾:一个族一张账,退出码由这张表算出来 ─────────────────────────────
|
||
为什么要把表打出来:退出码是布尔的,「第 2 条·附三」刚落地那天就漏在这张表外面——
|
||
它照常打印 `ICONS …` 与「N 处两级表边界问题」,却进不了 process.exit,
|
||
于是真仓库里有人把桶改回 `export * from './extra'`(首屏收益 −4.74 kB gzip 当场作废)
|
||
时,闸门数得清清楚楚、退出码却仍是 0,链子上看是「全绿」。
|
||
现在族的名单与退出码同源,`FAMILY` 这一行也给 gateprobe ㉖ 一个能区分
|
||
「采到了」与「响了」的机器可读标记(临时树里第 9 闸永远会响 5 下——它量的是全局登记表,
|
||
所以退出码本身在夹具里不具区分度,只能靠这一行)。 */
|
||
const FAMILIES = [
|
||
['死类名', dead], ['图标外部import', stray], ['图标出口', regHits], ['图标型控件缺名', ICON_NAMELESS],
|
||
['两级表', SPLIT_DEFECTS], ['裸button', bare], ['footer', footerViolations], ['antd越界', antdStray],
|
||
['反馈四级', fbHits], ['门牌图形', headHits], ['标签皮肤类', tagSkinHits], ['断点越界', bpHits],
|
||
]
|
||
console.log(`FAMILY ${FAMILIES.map(([n, a]) => `${n}=${a.length}`).join(' ')}`
|
||
+ ` —— 退出码 = 任一族非零;这条行是 gateprobe ㉖ 的机器可读凭据`)
|
||
process.exit(FAMILIES.reduce((s, [, a]) => s + a.length, 0) ? 1 : 0)
|
||
|