'use strict' // 组件级 token(L3)三方一致性守卫(v3.26.62,M6) // // 为什么需要:--iwb-set-* / --iwb-pop-* 原先手写在 style.css,文档有表格、代码有值, // 但没有任何断言 —— 实际漂过一次(--iwb-pop-w 从 328 被改到 360,文档晚了一步)。 // 现在值只在 scripts/gen-tokens.js 的 COMPONENT_SCOPED 里写一次,本测试守住三件事: // A. tokens.json 的 component. 与文档表格里的 token 名双向一致; // B. style.css 不再重复声明这些 token(否则单一源失效,改一处不生效); // C. design-tokens.css 里确实生成了这些声明(生成器没漏块)。 const assert = require('assert') const fs = require('fs') const path = require('path') const root = path.resolve(__dirname, '..') const tokens = JSON.parse(fs.readFileSync(path.join(root, 'dist', 'tokens.json'), 'utf8')) const css = fs.readFileSync(path.join(root, 'style.css'), 'utf8') const gen = fs.readFileSync(path.join(root, 'src', 'design-tokens.css'), 'utf8') const doc = fs.readFileSync(path.join(root, 'docs', 'standards', 'design-tokens.md'), 'utf8') const GROUPS = { // 容器级在 §3.11,控件级在 §3.12 —— 同属 component.settings,文档也分两节 settings: { prefix: '--iwb-set-', section: ['### 3.11', '### 3.12'] }, popover: { prefix: '--iwb-pop-', section: ['### 3.13'] } } function sectionBody (marker) { const i = doc.indexOf(marker) assert.ok(i >= 0, '文档缺少小节:' + marker) const j = doc.indexOf('### ', i + marker.length) return doc.slice(i, j > 0 ? j : doc.length) } let checked = 0 for (const [group, meta] of Object.entries(GROUPS)) { const g = (tokens.component || {})[group] assert.ok(g && typeof g === 'object', 'tokens.json 缺少 component.' + group + '(gen-tokens 的分组映射或 COMPONENT_SCOPED 漏了)') const codeNames = Object.keys(g).map((k) => meta.prefix + k) assert.ok(codeNames.length > 0, 'component.' + group + ' 为空') const body = [].concat(meta.section).map(sectionBody).join('\n') const docNames = [...new Set([...body.matchAll(/\|\s*`(--iwb-[a-z0-9-]+)`\s*\|/g)].map((m) => m[1]))] assert.ok(docNames.length > 0, [].concat(meta.section).join('+') + ' 表格里没解析到 token 名(表格格式变了?)') const missingInDoc = codeNames.filter((n) => !docNames.includes(n)) const missingInCode = docNames.filter((n) => !codeNames.includes(n)) assert.deepStrictEqual(missingInDoc, [], '代码有、文档没写:' + missingInDoc.join(', ')) assert.deepStrictEqual(missingInCode, [], '文档有、代码没定义:' + missingInCode.join(', ')) // B. style.css 不许再声明(单一源) const inStyle = [...css.matchAll(new RegExp('(' + meta.prefix + '[a-z0-9-]+)\\s*:', 'g'))].map((m) => m[1]) assert.deepStrictEqual(inStyle, [], 'style.css 仍在声明组件 token(应删掉,值只在 COMPONENT_SCOPED):' + inStyle.join(', ')) // C. 生成产物里有值 for (const n of codeNames) assert.ok(gen.includes(n + ':'), 'design-tokens.css 缺少生成项:' + n) checked += codeNames.length } console.log('design-tokens component guard: ' + checked + ' 个组件 token 三方一致(文档 ↔ tokens.json ↔ 生成产物)')