'use strict' // 设计 token 棘轮 + 完整性守卫(M5-T5.2 / T5.3) // // 为什么是「棘轮」而不是「全清零」:M4 结束时 style.css 里还有 170 处写死字号、415 处写死间距, // 一次清零等于重写一遍样式表。棘轮把「数字不许涨」变成机械规则,迁移可以分版本做, // 而**新代码**从今天起不可能再增加存量(涨一个就红)。 // // 数字口径唯一来源是 scripts/token-audit.js --json —— 这里不重新实现一遍正则: // 两套口径一旦漂移,就会变成「守卫绿、审计红」这种最难查的分歧。 // // 三条断言: // A. 每一项计数 ≤ tests/fixtures/token-baseline.json(只降不升) // B. 引用了但从未定义的变量 = 0(`var(--iwb-radius-pill)` 这种拼错必须当场红, // 而不是等到浏览器里那条声明静默失效) // C. 没有 `var(--x, var(--x))` 自我兜底 —— 兜底值等于引用值,等于「失效时再失效一次」, // 既掩盖问题又没有默认值可用 const assert = require('assert') const fs = require('fs') const path = require('path') const { execFileSync } = require('child_process') const root = path.resolve(__dirname, '..') const baseline = JSON.parse(fs.readFileSync(path.join(root, 'tests', 'fixtures', 'token-baseline.json'), 'utf8')) const audit = JSON.parse(execFileSync(process.execPath, [path.join(root, 'scripts', 'token-audit.js'), '--json'], { cwd: root, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 })) const now = { undefinedRefs: audit.undefinedRefs.length, deadTokens: audit.deadTokens.length, hex: audit.hard['十六进制色值'], rgb: audit.hard['rgba()/rgb()'], fontSize: audit.hard['font-size 写死'], radius: audit.hard['border-radius 写死'], spacing: audit.hard['padding/margin/gap 写死'], zIndexDecl: audit.hard['z-index 声明'], fontSizeDistinct: audit.fontSizes.distinct.length, radiusDistinct: audit.radii.distinct.length, spacingDistinct: audit.spacings.distinct.length, zIndexDistinct: audit.zindex.distinct.length, shadowDistinct: audit.shadows.distinct } // ── A. 只降不升 ── // 基线里有的项必须都在(防手滑删项让某项「凭空消失」),当前也必须有同名项(防改名后静默跳过) assert.deepStrictEqual( Object.keys(now).sort(), Object.keys(baseline.counts).sort(), '棘轮口径与基线键不一致:审计脚本加了/改了指标却没更新基线(或反之)' ) const LABEL = { undefinedRefs: '引用未定义变量', deadTokens: '定义了没用的变量', hex: '十六进制色值', rgb: 'rgba()/rgb()', fontSize: 'font-size 写死', radius: 'border-radius 写死', spacing: 'padding/margin/gap 写死', zIndexDecl: 'z-index 声明', fontSizeDistinct: '字号取值种数', radiusDistinct: '圆角取值种数', spacingDistinct: '间距取值种数', zIndexDistinct: 'z-index 取值种数', shadowDistinct: '阴影写法种数' } const grew = [] for (const k of Object.keys(now)) { if (now[k] > baseline.counts[k]) grew.push(LABEL[k] + ':' + baseline.counts[k] + ' → ' + now[k] + '(+' + (now[k] - baseline.counts[k]) + ')') } assert.deepStrictEqual(grew, [], '以下计数涨了 —— 新增样式请用 token(--iwb-font-* / --iwb-radius-* / --iwb-space-* / --iwb-z-* / 语义色):\n ' + grew.join('\n ') + '\n确属必要的例外:用行内 /*iwb:raw-until=<版本>*/(会到期)或 /*iwb:content*/(画布内容色),不要直接把基线数字调大。') // ── B. 引用必须存在 ── assert.deepStrictEqual(audit.undefinedRefs, [], '有 var() 引用了从未定义的变量:' + audit.undefinedRefs.join(' ') + '(拼错的 token 名会让整条声明静默失效)') // ── C. 自我兜底 ── const cssFiles = ['style.css'].concat( fs.readdirSync(path.join(root, 'src')).filter((f) => f.endsWith('.css') && f !== 'design-tokens.css').map((f) => 'src/' + f) ) const selfFallback = [] for (const f of cssFiles) { const src = fs.readFileSync(path.join(root, f), 'utf8') src.split('\n').forEach((line, i) => { const m = /var\(\s*(--[a-z0-9-]+)\s*,\s*var\(\s*(--[a-z0-9-]+)/i.exec(line) if (m && m[1] === m[2]) selfFallback.push(f + ':' + (i + 1) + ' ' + m[1]) }) } assert.deepStrictEqual(selfFallback, [], 'var() 用自己做兜底(等于没有兜底):' + selfFallback.join(' | ')) const drop = Object.keys(now).filter((k) => now[k] < baseline.counts[k]) console.log('token-ratchet: ' + Object.keys(now).length + ' 项计数均未超过基线' + (drop.length ? '(可下调基线:' + drop.map((k) => LABEL[k] + ' ' + baseline.counts[k] + '→' + now[k]).join(',') + ')' : '')) console.log('token-ratchet: 引用未定义变量 0、自我兜底 0')