fix(gates): check:motion 的 CSS 解析器不剥注释,会把紧随其后的令牌整条吞掉
extractCssDeclarations 的声明正则值部分是 `[^;{}]*`,天然跨行、一路吃到下一个 `;`。
它原先不剥注释,于是当一条令牌定义前面有一段含 **ASCII 冒号**的块注释时(例如注释里写
`CONTEXT.md:76`),正则会先从注释里的 `md:` 起匹配,把「注释余文 + 紧随其后的整条
`--transition-hover: 150ms;`」一起吞进 value 捕获组 —— 该声明根本没被采集。
后果不是少个数,是**假红**:R3 随后对着一个明明已定义的令牌报
「引用了未定义的 --transition-hover 令牌(编译后不生效)」。假红比假绿更贵,
它教会人无视这道闸。
发现方式:本批给 globals.css 加 --transition-hover 档时,闸报 `--transition-* 令牌 6`
而 `:root` 里实有 7 条,且判定分母对不上 —— 逐条核对才定位到解析器而非 CSS 写错。
- 新增 stripCssComments:整段替换为**等量换行**,故 lineOf 报出的行号仍指向原文件那一行
(行号错位 = 判据不可行动,等同半个假绿)
- 配套回归用例是双向的:断言采集数 == 实际定义数(4),且注释在/不在时判定必须一致
- 已实测该用例能失败:摘掉修复 → Expected 4 / Received 3;恢复 → 20 passed
门禁:check:motion:test 20/20(原 19 + 新增 1);type-check EXIT=0;
全量 jest 143 套件 / 1769 例通过。
This commit is contained in:
1 parent
da9a5cb650
commit
d346301405
3 files changed
+52
-201
No files matched your search
@@ -235,15 +235,33 @@ export interface CssDeclaration {
|
||||
isCustomProperty: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 剥 CSS 注释,但**保留换行条数**,使 lineOf 算出的行号仍指向原文件那一行。
|
||||
*
|
||||
* 为什么必须在扫描之前剥(2026-10-16 真实踩到,不是假想):
|
||||
* 下面的声明正则值部分 `[^;{}]*` 天然跨行、一路吃到下一个 `;`。若某条令牌定义前面有一段
|
||||
* 含 **ASCII 冒号**的块注释(例如注释里写 `CONTEXT.md:76`),正则会先从注释里的 `md:` 起匹配,
|
||||
* 把「注释余文 + 紧随其后的整条 `--transition-hover: 150ms;`」一起吞进 value 组 ——
|
||||
* 该声明于是**根本没被采集**,闸转而报「引用了未定义的 --transition-hover(死样式)」,
|
||||
* 是一条对着已存在令牌的**假红**。假红比假绿更贵:它教会人无视这道闸。
|
||||
* 现场:globals.css 的 :root 实有 7 个 `--transition-*`,本闸只采到 6 个,即此因。
|
||||
*/
|
||||
export function stripCssComments(text: string): string {
|
||||
return text.replace(/\/\*[\s\S]*?\*\//g, (m) => "\n".repeat((m.match(/\n/g) ?? []).length));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 CSS 文本里抽出「属性: 值」声明。
|
||||
* `[^;{}]*` 天然跨行,故 globals.css:314 那种多行值也能拿到完整串。
|
||||
* 自定义属性(`--transition-*` / `--ease-*` / `--card-transition`)与普通声明都会拿到,
|
||||
* 由调用方按 prop 名分流;`--card-transition` 这类「组件级过渡令牌」也在射程内,
|
||||
* 因为它同样是编译进产物的 CSS 值。
|
||||
* ⚠ 扫描前先过 stripCssComments,否则注释里的 ASCII 冒号会吞掉紧随其后的声明。
|
||||
*/
|
||||
export function extractCssDeclarations(text: string): CssDeclaration[] {
|
||||
export function extractCssDeclarations(rawText: string): CssDeclaration[] {
|
||||
const declarations: CssDeclaration[] = [];
|
||||
// 扫描前先剥注释:见 stripCssComments 里记的假红事故
|
||||
const text = stripCssComments(rawText);
|
||||
const lineOf = makeLineResolver(text);
|
||||
const pattern = /(--[\w-]+|[a-z][\w-]*)\s*:\s*([^;{}]*)/gi;
|
||||
let match: RegExpExecArray | null = pattern.exec(text);
|
||||
|
||||
Reference in new issue
Block a user