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
@@ -220,6 +220,39 @@ export const C = () => <motion.div transition={{ duration: 0.2, ease: [0.1, 0.2,
|
||||
});
|
||||
expect(result.violations).toEqual([]);
|
||||
});
|
||||
|
||||
it('回归:令牌定义紧跟「含 ASCII 冒号的多行注释」之后,仍必须被采集', () => {
|
||||
// 缺陷现场(2026-10-16):extractCssDeclarations 原先不剥注释,而值部分 `[^;{}]*` 跨行吃到下一个 `;`。
|
||||
// 于是注释里的 `CONTEXT.md:76` 起匹配后,把注释余文连同下一行 `--transition-hover: 150ms;`
|
||||
// 一起吞进 value 组 ⇒ 该令牌没被采集 ⇒ 闸对**已定义**的令牌报「悬空引用=死样式」的假红。
|
||||
// 两条断言互为对照:采集数必须等于实际定义数,且引用它不产生违规。
|
||||
const withColonComment = `
|
||||
:root {
|
||||
--transition-instant: 100ms;
|
||||
/* hover 档:动效四原则 CONTEXT.md:76 早就写了「hover 150ms」,但仓库从来没有这个档。
|
||||
补在此处闭合 C-1。 */
|
||||
--transition-hover: 150ms;
|
||||
--transition-fast: 180ms;
|
||||
--transition-normal: 280ms;
|
||||
--ease-ink: cubic-bezier(0.22, 1, 0.36, 1);
|
||||
}
|
||||
.card { transition: border-color var(--transition-hover) var(--ease-out), background var(--transition-hover) var(--ease-out); }
|
||||
`;
|
||||
const withoutComment = withColonComment
|
||||
.split('\n')
|
||||
.filter((l) => !/^\s*(\/\*|\*|.*\*\/)\s*/.test(l) && !/hover 档|补在此处/.test(l))
|
||||
.join('\n');
|
||||
|
||||
const a = scanFixture({ css: withColonComment });
|
||||
const b = scanFixture({ css: withoutComment });
|
||||
|
||||
// 4 = instant / hover / fast / normal。修复前这里只采到 3(hover 被注释吞掉)
|
||||
expect(a.counts.transitionTokens).toBe(4);
|
||||
expect(b.counts.transitionTokens).toBe(4);
|
||||
// 注释在不在,判定必须一致:不得因注释而多出「悬空引用」
|
||||
expect(a.countOf('css-transition-token')).toBe(0);
|
||||
expect(b.countOf('css-transition-token')).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('违规夹具:每条规则都必须能判红(防「常绿」)', () => {
|
||||
|
||||
@@ -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