feat(ui): 保时捷语法复刻收尾(ADR-0008/0009/0011/0012/0013) #65
No files matched your search
@@ -173,6 +173,37 @@ const OPACITY_SCALE = new Set(["0", "5", "10", "15", "20", "25", "30", "35", "40
|
||||
/** 任意值色标(`bg-[#0B0A08]`)与命名色标(`bg-white`)后面跟裸数字 alpha 的形态 */
|
||||
const NON_SCALE_ALPHA_RE = /(?<![\w-])(?:bg|text|border|ring|divide|outline|fill|stroke|from|via|to|placeholder|accent|caret|decoration|shadow)-[^\s"'`/]*\/(\d{1,3})(?![\d.\w[-])/g;
|
||||
|
||||
/**
|
||||
* R21 死样式的**第三族**:自定义命名空间的类名写了,但 CSS 里从未定义过。
|
||||
*
|
||||
* 为什么必须存在(2026-11-03 审计发现、2026-11-25 才收口):
|
||||
* privacy / terms / not-found / error / SkeletonHero / standalone 六处写了
|
||||
* `container-wide`,而全仓从没有一条 `.container-wide` 规则 —— 于是这些页的内容
|
||||
* **贴左边缘、没有栅格**,而 type-check / lint / build / 单测 / axe 五道全绿。
|
||||
* 与 R20 是同一种病(「class 在源码里」≠「CSS 真的存在」),只是成因不同:
|
||||
* R20 是 Tailwind 拒收 scale 外的 alpha,R21 是这个名字根本没人定义。
|
||||
*
|
||||
* 判据刻意收窄到**本仓自有命名空间**(container- / section- / bain- / list- / prose
|
||||
* 之外的少数固定几个)—— Tailwind 的任意 utility 无穷多,全量枚举不可能;
|
||||
* 而这几族是本仓 globals.css 用 `@layer utilities` 手工定义的,数量有限、可枚举。
|
||||
* 枚举不到就判红 = 强制「要用自定义类名就必须先在 CSS 里定义它」。
|
||||
*/
|
||||
/**
|
||||
* 只收录**本仓 globals.css 用 @layer utilities 手工定义**的族,外加一个精确名。
|
||||
*
|
||||
* 刻意不收录的:
|
||||
* · `list-*` —— `list-none` / `list-disc` / `list-inside` 是 Tailwind 原生 utility,
|
||||
* 按前缀收会把它们全判成"未定义"(实测第一版就这样误报 3 条)。
|
||||
* 本仓自有的只有 `list-plain`,走精确名。
|
||||
* · `prose*` —— 属 `@tailwindcss/typography` 插件族。本仓**没有装该插件**,
|
||||
* 所以 privacy / terms 里的 `prose` / `prose-lg` 同样是死样式,但修它要么加依赖
|
||||
* 要么手写一套排版规则 —— 属设计裁定,故登记为已知缺口(见 qa-tracker §18),
|
||||
* 不塞进本闸(否则这条闸一上线就恒红,又会走上"把闸调瞎"那条路)。
|
||||
*/
|
||||
const CUSTOM_CLASS_PREFIXES = ["container-", "section-", "bain-", "editorial-", "signature-"];
|
||||
/** 精确名(不走前缀,避开与 Tailwind 原生族撞名) */
|
||||
const CUSTOM_CLASS_EXACT = new Set(["list-plain"]);
|
||||
|
||||
const countHits = (text, re) => (text.match(new RegExp(re.source, "g")) || []).length;
|
||||
|
||||
// ---- 源码遍历(只用 node 内置,避开第三方)----
|
||||
@@ -540,12 +571,48 @@ function checkGrammar(rawText, rawFiles, twConfig = "", opts = {}) {
|
||||
rule({ id: "R20", name: "裸 alpha 必须落在 opacity scale 内(防 class 在、CSS 不在)", denominator: r20Den, red: r20Red,
|
||||
note: `扫描 ${r20Den} 处裸 alpha · 合法集合 = 5 的倍数(0…100)` });
|
||||
|
||||
/**
|
||||
* R21 自定义命名空间类名必须在 CSS 里有定义(详见 module 层 CUSTOM_CLASS_PREFIXES)。
|
||||
* 扫描面 = 全 src:这类失效与页面性质无关,privacy/terms 踩过,ui/ 里同样会踩。
|
||||
*
|
||||
* 分母 = 命中的自定义命名空间 token 总数;红 = 其中在 CSS(已剥注释)里找不到
|
||||
* `.token` 定义的那些。CSS 侧同时接受 `.token {` 与 `.token,` / `.token:hover` 等变体,
|
||||
* 因此按「`.token` 后紧跟非标识符字符」判定,避免 `.container-x` 误命中 `.container-xyz`。
|
||||
*/
|
||||
const r21Red = [];
|
||||
let r21Den = 0;
|
||||
const cssForClasses = `${text}\n${twConfig}`;
|
||||
const seen = new Set();
|
||||
for (const [rel, src] of files) {
|
||||
if (!/\.(tsx?|jsx?)$/.test(rel)) continue;
|
||||
for (const m of src.matchAll(/className=(?:"([^"]*)"|\{`([^`]*)`\})/g)) {
|
||||
const raw = m[1] ?? m[2] ?? "";
|
||||
for (const token of raw.split(/\s+/)) {
|
||||
const isCustom = token
|
||||
&& (CUSTOM_CLASS_PREFIXES.some((p) => token.startsWith(p)) || CUSTOM_CLASS_EXACT.has(token));
|
||||
if (!isCustom) continue;
|
||||
const bare = token.replace(/:.*$/, "").replace(/\/.*$/, "");
|
||||
if (!bare) continue;
|
||||
r21Den += 1;
|
||||
const key = `${bare}`;
|
||||
const defRe = new RegExp(`\\.${key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\\w-])`);
|
||||
if (!defRe.test(cssForClasses)) {
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
r21Red.push(`${rel}:\`${bare}\` 在 CSS 里没有任何定义 —— 该规则不会生成,样式静默失效(R21 第三族死样式)`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
rule({ id: "R21", name: "自定义命名空间类名必须有 CSS 定义(防第三族死样式)", denominator: r21Den, red: r21Red,
|
||||
note: `扫描 ${r21Den} 处自定义类名 · 命名空间 ${CUSTOM_CLASS_PREFIXES.join("/")} + ${[...CUSTOM_CLASS_EXACT].join("/")}` });
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// ---- 自证(负例):喂一批明知违规的样本,不判红就宣布上面全部不作数 ----
|
||||
function selftest() {
|
||||
const bad = `:root {\n --radius-xs: 3px;\n --radius-sm: 6px;\n --radius-full: 0px;\n --shadow-brand: 0 6px 24px rgba(196,30,58,.22);\n --badge-radius: var(--radius-full);\n}\n.bain-card:hover { transform: translateY(-4px); }\n`;
|
||||
const bad = `:root {\n --radius-xs: 3px;\n --radius-sm: 6px;\n --radius-full: 0px;\n --shadow-brand: 0 6px 24px rgba(196,30,58,.22);\n --badge-radius: var(--radius-full);\n}\n.bain-card:hover { transform: translateY(-4px); }\n/* R21 的「已定义」一侧:夹具里必须真实存在一个自定义类名,否则只会测到"全判红" */\n.container-x { max-width: 1360px; }\n`;
|
||||
const files = new Map([
|
||||
["src/app/globals.css", bad],
|
||||
["src/components/ui/cta-button.tsx", 'className="rounded-full"'],
|
||||
@@ -553,13 +620,17 @@ function selftest() {
|
||||
["src/components/ui/button.tsx", "shadow-brand\n size: {\n sm: 'h-9 rounded-full px-3',\n default: 'h-11 rounded-full px-5',\n lg: 'h-12 rounded-cta px-6',\n xl: 'h-14 rounded-cta px-8',\n icon: 'h-11 w-11 rounded-cta',\n },"],
|
||||
["src/components/ui/badge.tsx", 'className="rounded-full"'],
|
||||
["src/app/(marketing)/x.tsx", 'className="shadow-brand-hover"'],
|
||||
// R21:自定义命名空间类名写了但 CSS 里没有定义(container-wide 那族的再现)
|
||||
["src/app/error.tsx", 'className="container-wide container-x"'],
|
||||
]);
|
||||
// 第三参数是 tailwind.config 文本:给一份「引用全部已声明、且带齐外部所有者豁免」的,
|
||||
// 避免 R11 的红因混进 R1–R9 的读数里(R11 有自己的两侧自证,见下)。
|
||||
const RADIX_REF = `keyframes: { refs: { ${EXTERNAL_OWNED.map((v) => `x: 'var(${v})'`).join(", ")} } }`;
|
||||
const r = checkGrammar(bad, files, `${RADIX_REF}\nboxShadow: { brand: 'var(--shadow-brand)' }`);
|
||||
const redIds = r.filter((x) => x.red.length > 0).map((x) => x.id);
|
||||
const expect = ["R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9"];
|
||||
// R21 在同一个夹具里:`container-wide` 必须判红,而同行的 `container-x`
|
||||
// (CSS 夹具里确实定义了 .container-x)不得被牵连 —— 否则闸会逼人删掉正确的类名。
|
||||
const expect = ["R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R21"];
|
||||
const missing = expect.filter((id) => !redIds.includes(id));
|
||||
console.log("自证(负例必须全部被抓红):");
|
||||
console.log(` 被抓红 ${redIds.join(",") || "(无)"}`);
|
||||
@@ -569,6 +640,18 @@ function selftest() {
|
||||
}
|
||||
console.log(` ✓ ${expect.length}/${expect.length} 条规则对已知违规均判红`);
|
||||
|
||||
// R21 双侧:只验"抓到未定义"会漏掉「把所有自定义类名一律报错」那种坏判据。
|
||||
const r21Red = r.find((x) => x.id === "R21").red;
|
||||
const r21CatchesUndefined = r21Red.some((m) => m.includes("container-wide"));
|
||||
const r21IgnoresDefined = !r21Red.some((m) => m.includes("container-x"));
|
||||
if (!r21CatchesUndefined || !r21IgnoresDefined) {
|
||||
console.log(
|
||||
` ✗ R21 双侧:未定义未抓到 ${!r21CatchesUndefined}、已定义被误报 ${!r21IgnoresDefined} ⇒ 该闸不可用`,
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
console.log(" ✓ R21 双侧:未定义的自定义类名判红、已定义的不误报");
|
||||
|
||||
// R11 单独两侧自证:僵尸令牌 = tailwind 引用了 CSS 没声明的变量。
|
||||
// 夹具里 --shadow-glow 未声明、--shadow-brand 已声明,所以必须只抓前者;
|
||||
// 若判据恒绿(抓不到)或恒红(连已声明的也报),都算仪器坏。
|
||||
|
||||
@@ -20,7 +20,10 @@ import path from 'node:path';
|
||||
// 扫描根可以有多个:只扫 src 会漏掉**门禁脚本与 e2e 对 src 的引用**
|
||||
// (scripts/utils/check-color-contrast.ts 就 import 了 src/lib/color-contrast.ts,
|
||||
// 只扫 src 时它被误判成死码)。入口仍只取 app/ 约定文件,其余根只贡献"谁在引"。
|
||||
const ROOTS = process.argv.length > 2 ? process.argv.slice(2) : ['src', 'scripts', 'e2e', 'config'];
|
||||
// `prisma/` 必须在内:`prisma/seed.ts` 是 `src/lib/seed-target.ts` 的**唯一**消费方
|
||||
// (2026-11-25 实测误报:漏掉这个根后 seed-target.ts 被判「仅测试/死链引用」,
|
||||
// 而它正是 seed 沙盒保护闸的实体 —— 按那个结论删掉,写错库的保护就没有了)。
|
||||
const ROOTS = process.argv.length > 2 ? process.argv.slice(2) : ['src', 'scripts', 'e2e', 'config', 'prisma'];
|
||||
const ROOT = 'src';
|
||||
const missing = ROOTS.filter((r) => !existsSync(r));
|
||||
if (missing.length) { console.error(`未能度量:根目录不存在 ${missing.join(', ')}`); process.exit(2); }
|
||||
|
||||
Reference in new issue
Block a user