mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-03 21:49:46 +08:00
fix(design-system): DS-10 间距绑定目标须存在于 DTCG 源
审查指出:前缀正确但目标不存在的条目(space-typo)仍能通过 非空校验,docs-only 审计退出 0,且 p-[var(--space-typo)] 会被 标成 spacing-source-reference——单个拼写错误就能伪造生成来源。 校验改为逐条核对 space-* 绑定目标在 packages/design-tokens/src/semantic/foundations.json 的 semantic.foundations 下真实存在,不存在则退出 2 并指名缺失项; DTCG 源缺失/损坏同样退出 2。该文件纳入审计 scriptHashes 输入, 回归测试补伪造目标与 DTCG 缺失断言。 Signed-off-by: Kirozeng <kirozeng@gmail.com>
This commit is contained in:
@@ -205,8 +205,11 @@ test('worktree includes staged, unstaged and untracked source; commit mode exclu
|
||||
fs.copyFileSync(path.join(root, 'scripts', rel), target);
|
||||
}
|
||||
const bindings = 'packages/design-tokens/src/desktop-bindings.json';
|
||||
const dtcg = 'packages/design-tokens/src/semantic/foundations.json';
|
||||
fs.mkdirSync(path.dirname(path.join(temp, bindings)), {recursive:true});
|
||||
fs.copyFileSync(path.join(root, bindings), path.join(temp, bindings));
|
||||
fs.mkdirSync(path.dirname(path.join(temp, dtcg)), {recursive:true});
|
||||
fs.copyFileSync(path.join(root, dtcg), path.join(temp, dtcg));
|
||||
const cli = (...args) => spawnSync(process.execPath, ['scripts/hardcoded-color-audit.mjs', ...args], {cwd:temp,encoding:'utf8'});
|
||||
const failed = cli('--base-ref',commit,'--worktree','--json');
|
||||
assert.equal(failed.status,1,failed.stderr);
|
||||
@@ -239,13 +242,23 @@ test('worktree includes staged, unstaged and untracked source; commit mode exclu
|
||||
assert.equal(cli('--base-ref',commit,'--worktree','--report').status,2);
|
||||
fs.writeFileSync(path.join(temp,bindings),'{"foundations":{"css":{"text":"semantic.foundations.text-14"}}}');
|
||||
assert.equal(cli('--base-ref',commit,'--worktree','--report').status,2);
|
||||
// A prefix-correct but non-existent target must not invent a real source:
|
||||
// the binding has to resolve against the DTCG generation source.
|
||||
const poisoned = JSON.parse(fs.readFileSync(path.join(root, bindings), 'utf8'));
|
||||
poisoned.foundations.css['space-typo'] = 'semantic.foundations.space-typo';
|
||||
fs.writeFileSync(path.join(temp,bindings), JSON.stringify(poisoned));
|
||||
assert.equal(cli('--base-ref',commit,'--worktree','--report').status,2);
|
||||
fs.copyFileSync(path.join(root, bindings), path.join(temp, bindings));
|
||||
fs.rmSync(path.join(temp,dtcg));
|
||||
assert.equal(cli('--base-ref',commit,'--worktree','--report').status,2);
|
||||
fs.copyFileSync(path.join(root, dtcg), path.join(temp, dtcg));
|
||||
fs.appendFileSync(path.join(temp,file), '\nconst spacing = "gap-x-[var(--space-4)]";\n');
|
||||
const reported = cli('--base-ref',commit,'--worktree','--report','--json');
|
||||
assert.equal(reported.status,0,reported.stderr);
|
||||
const report = JSON.parse(reported.stdout);
|
||||
assert.equal(report.findings.find(f=>f.rule==='role-spacing')?.classification,'spacing-source-reference');
|
||||
assert.match(report.scriptHashes[bindings],/^[a-f0-9]{64}$/);
|
||||
assert.match(report.scriptHashes[dtcg],/^[a-f0-9]{64}$/);
|
||||
fs.writeFileSync(path.join(temp,bindings),'{bad json');
|
||||
assert.equal(cli('--base-ref',commit,'--worktree','--report').status,2);
|
||||
fs.rmSync(path.join(temp,bindings));
|
||||
|
||||
@@ -185,7 +185,8 @@ export function audit({ root = ROOT, baseRef = 'origin/main', headRef = 'HEAD',
|
||||
}
|
||||
const scripts = ['scripts/hardcoded-color-audit.mjs', 'scripts/shared/hardcoded-color-match.mjs',
|
||||
'scripts/shared/design-layer-report.mjs', 'scripts/hardcoded-color-exemptions.json',
|
||||
'packages/design-tokens/src/desktop-bindings.json'];
|
||||
'packages/design-tokens/src/desktop-bindings.json',
|
||||
'packages/design-tokens/src/semantic/foundations.json'];
|
||||
const scriptHashes = Object.fromEntries(scripts.map(f => [f, hash(fs.readFileSync(path.join(f === 'scripts/hardcoded-color-exemptions.json' ? root : ROOT, f)))]));
|
||||
const colors = findings.filter(f => f.rule === 'bare-color');
|
||||
return { schemaVersion: 1, base, head, candidate: worktree ? 'worktree (staged + unstaged + untracked)' : 'commit',
|
||||
|
||||
@@ -10,13 +10,23 @@ export function getSpacingVariables() {
|
||||
if (!foundations?.css || typeof foundations.css !== 'object' || Array.isArray(foundations.css)) {
|
||||
throw new Error('Invalid desktop-bindings.json; expected foundations.css as a non-array mapping');
|
||||
}
|
||||
spacingVariables = new Set(Object.entries(foundations.css)
|
||||
const bindings = Object.entries(foundations.css)
|
||||
// Includes component spacing (space-input-lg), not only Tailwind's scale.
|
||||
.filter(([, id]) => id.startsWith('semantic.foundations.space-'))
|
||||
.map(([name]) => `--${name}`));
|
||||
if (!spacingVariables.size) {
|
||||
.filter(([, id]) => id.startsWith('semantic.foundations.space-'));
|
||||
if (!bindings.length) {
|
||||
throw new Error('Invalid desktop-bindings.json; foundations.css has no semantic.foundations.space-* entries');
|
||||
}
|
||||
// The prefix alone must not invent sources: every binding target has to
|
||||
// exist as a token in the DTCG generation source the binding is built from.
|
||||
const dtcg = JSON.parse(readFileSync(new URL('../../packages/design-tokens/src/semantic/foundations.json', import.meta.url), 'utf8'))?.semantic?.foundations;
|
||||
if (!dtcg || typeof dtcg !== 'object' || Array.isArray(dtcg)) {
|
||||
throw new Error('Invalid semantic/foundations.json; expected semantic.foundations DTCG tokens');
|
||||
}
|
||||
const unbound = bindings.filter(([, id]) => !dtcg[id.slice('semantic.foundations.'.length)]);
|
||||
if (unbound.length) {
|
||||
throw new Error(`Invalid desktop-bindings.json; spacing bindings without a DTCG token: ${unbound.map(([name]) => `--${name}`).join(', ')}`);
|
||||
}
|
||||
spacingVariables = new Set(bindings.map(([name]) => `--${name}`));
|
||||
}
|
||||
return spacingVariables;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user