// Checks the UI and bridge contracts (see docs/plugin-architecture.md) // and writes ui-contract.json / bridge-contract.json for plugin authors. // // Rule "public means implemented": every stable part, state attribute, slot and // layer must be emitted by the host, and an experimental entry the host already // emits must be promoted to stable in the same change. import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const rootDir = path.resolve(__dirname, '..') const srcDir = path.join(rootDir, 'src') const contractSrc = fs.readFileSync(path.join(srcDir, 'styles', 'ui-contract.ts'), 'utf8') const bridgeSrc = fs.readFileSync(path.join(srcDir, 'lib', 'bridgeContract.ts'), 'utf8') const tokensCss = fs.readFileSync(path.join(srcDir, 'styles', 'tokens.css'), 'utf8') const appCss = fs.readFileSync(path.join(srcDir, 'styles', 'app.css'), 'utf8') function extractArray(src, name) { const re = new RegExp(`export const ${name} = \\[([\\s\\S]*?)\\] as const`) const m = src.match(re) if (!m) throw new Error(`Could not parse ${name}`) return [...m[1].matchAll(/'([^']+)'/g)].map((x) => x[1]) } function extractVersion(src, name) { const m = src.match(new RegExp(`${name}\\s*=\\s*'([^']+)'`)) if (!m) throw new Error(`Could not parse ${name}`) return m[1] } const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') const uiApi = extractVersion(contractSrc, 'UI_API_VERSION') const stable = { parts: extractArray(contractSrc, 'UI_CONTRACT_PARTS'), stateAttrs: extractArray(contractSrc, 'UI_CONTRACT_STATE_ATTRS'), itemStates: extractArray(contractSrc, 'UI_CONTRACT_ITEM_STATES'), slots: extractArray(contractSrc, 'UI_CONTRACT_SLOTS'), layers: extractArray(contractSrc, 'UI_CONTRACT_LAYERS'), layoutOptions: extractArray(contractSrc, 'UI_CONTRACT_LAYOUT_OPTIONS'), icons: extractArray(contractSrc, 'UI_CONTRACT_ICONS'), viewLocations: extractArray(contractSrc, 'UI_CONTRACT_VIEW_LOCATIONS'), sdkEvents: extractArray(contractSrc, 'UI_CONTRACT_SDK_EVENTS'), tokens: extractArray(contractSrc, 'UI_CONTRACT_TOKENS'), } const experimental = { parts: extractArray(contractSrc, 'UI_CONTRACT_PARTS_EXPERIMENTAL'), stateAttrs: extractArray(contractSrc, 'UI_CONTRACT_STATE_ATTRS_EXPERIMENTAL'), itemStates: extractArray(contractSrc, 'UI_CONTRACT_ITEM_STATES_EXPERIMENTAL'), slots: extractArray(contractSrc, 'UI_CONTRACT_SLOTS_EXPERIMENTAL'), layers: extractArray(contractSrc, 'UI_CONTRACT_LAYERS_EXPERIMENTAL'), layoutOptions: extractArray(contractSrc, 'UI_CONTRACT_LAYOUT_OPTIONS_EXPERIMENTAL'), icons: extractArray(contractSrc, 'UI_CONTRACT_ICONS_EXPERIMENTAL'), viewLocations: extractArray(contractSrc, 'UI_CONTRACT_VIEW_LOCATIONS_EXPERIMENTAL'), sdkEvents: extractArray(contractSrc, 'UI_CONTRACT_SDK_EVENTS_EXPERIMENTAL'), } const bridgeApi = extractVersion(bridgeSrc, 'BRIDGE_API_VERSION') const bridge = { methods: extractArray(bridgeSrc, 'BRIDGE_METHODS'), events: extractArray(bridgeSrc, 'BRIDGE_EVENTS'), experimentalMethods: extractArray(bridgeSrc, 'BRIDGE_METHODS_EXPERIMENTAL'), experimentalEvents: extractArray(bridgeSrc, 'BRIDGE_EVENTS_EXPERIMENTAL'), } // Host source the contract is emitted from (contract/SDK definitions excluded). function readSources(dir, exts) { let buf = '' for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name) if (entry.isDirectory()) buf += readSources(full, exts) else if (exts.some((e) => entry.name.endsWith(e)) && !/ui-contract\.ts$|bridgeContract\.ts$|\.gen\.ts$/.test(entry.name)) { buf += '\n' + fs.readFileSync(full, 'utf8') } } return buf } const allTsx = readSources(srcDir, ['.tsx']) const allCode = readSources(srcDir, ['.ts', '.tsx']) // Per-file sources, for checks that need two things in the same component. function sourceFiles(dir, out = []) { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name) if (entry.isDirectory()) sourceFiles(full, out) else if (entry.name.endsWith('.tsx')) out.push(fs.readFileSync(full, 'utf8')) } return out } const tsxFiles = sourceFiles(srcDir) const hasClass = (p) => new RegExp(`(? attr.replace(/^data-/, '').replace(/-([a-z])/g, (_, c) => c.toUpperCase()) const hasAttr = (a) => allCode.includes(a) || new RegExp(`dataset\\.${camel(a)}\\b`).test(allCode) const hasSlot = (s) => allTsx.includes(`data-rg-slot="${s}"`) const hasLayer = (l) => allTsx.includes(`data-rg-layer="${l}"`) const errors = [] const fail = (msg, list) => { if (list.length) errors.push(`${msg}: ${list.join(', ')}`) } fail('stable parts not emitted by any component', stable.parts.filter((p) => !hasClass(p))) fail('stable state attributes never set', stable.stateAttrs.filter((a) => !hasAttr(a))) fail('stable slots without a data-rg-slot anchor', stable.slots.filter((s) => !hasSlot(s))) fail('stable layers without a data-rg-layer element', stable.layers.filter((l) => !hasLayer(l))) fail('stable tokens missing in tokens.css', stable.tokens.filter((tk) => !tokensCss.includes(`${tk}:`))) // An item state counts as emitted when one component carries both the part and the attribute. fail('stable item states not emitted together with their part', stable.itemStates.filter((s) => { const [part, attr] = s.split(':') return !tsxFiles.some((src) => new RegExp(`(? { if (LAYOUT_DEFAULTS.includes(o)) return false const [opt, val] = o.split('=') return !LAYOUT_ATTR[opt] || !appCss.includes(`[${LAYOUT_ATTR[opt]}="${val}"]`) })) fail('layout options with an unknown option name', [...stable.layoutOptions, ...experimental.layoutOptions].filter((o) => !LAYOUT_ATTR[o.split('=')[0]])) // Every stable icon name is rendered through (or the status-shape map). const controlsSrc = fs.readFileSync(path.join(srcDir, 'ui', 'controls.tsx'), 'utf8') fail('stable icon names never rendered through HostIcon', stable.icons.filter((n) => !allTsx.includes(`HostIcon name="${n}"`) && !(n.startsWith('vm.state.') && controlsSrc.includes(`'${n}'`)))) // Every stable SDK event has an emitter. fail('stable SDK events never emitted', stable.sdkEvents.filter((e) => !allCode.includes(`emitThemeEvent('${e}'`))) fail('experimental parts already emitted (promote them to UI_CONTRACT_PARTS)', experimental.parts.filter(hasClass)) fail('experimental slots already anchored (promote them to UI_CONTRACT_SLOTS)', experimental.slots.filter(hasSlot)) fail('experimental layers already rendered (promote them to UI_CONTRACT_LAYERS)', experimental.layers.filter(hasLayer)) const overlap = (a, b) => a.filter((x) => b.includes(x)) fail('entries listed as both stable and experimental (parts)', overlap(stable.parts, experimental.parts)) fail('entries listed as both stable and experimental (state attributes)', overlap(stable.stateAttrs, experimental.stateAttrs)) fail('entries listed as both stable and experimental (slots)', overlap(stable.slots, experimental.slots)) fail('entries listed as both stable and experimental (layers)', overlap(stable.layers, experimental.layers)) fail('entries listed as both stable and experimental (item states)', overlap(stable.itemStates, experimental.itemStates)) fail('entries listed as both stable and experimental (layout options)', overlap(stable.layoutOptions, experimental.layoutOptions)) fail('entries listed as both stable and experimental (icons)', overlap(stable.icons, experimental.icons)) fail('entries listed as both stable and experimental (view locations)', overlap(stable.viewLocations, experimental.viewLocations)) fail('entries listed as both stable and experimental (SDK events)', overlap(stable.sdkEvents, experimental.sdkEvents)) fail('bridge methods listed as both stable and experimental', overlap(bridge.methods, bridge.experimentalMethods)) fail('bridge events listed as both stable and experimental', overlap(bridge.events, bridge.experimentalEvents)) const knownParts = [...stable.parts, ...experimental.parts] fail('item states on unknown parts', [...stable.itemStates, ...experimental.itemStates].filter((s) => !knownParts.includes(s.split(':')[0]))) for (const [name, list] of Object.entries({ ...stable, ...experimental, ...bridge })) { const dupes = list.filter((x, i) => list.indexOf(x) !== i) fail(`duplicate entries in ${name}`, dupes) } if (errors.length > 0) { for (const e of errors) console.error(`[ui-contract] ERROR: ${e}`) process.exit(1) } const write = (file, data) => { const text = JSON.stringify(data, null, 2) + '\n' fs.writeFileSync(path.join(rootDir, file), text, 'utf8') const distDir = path.join(rootDir, 'dist') if (fs.existsSync(distDir)) fs.writeFileSync(path.join(distDir, file), text, 'utf8') } write('ui-contract.json', { uiApi, ...stable, experimental }) write('bridge-contract.json', { bridgeApi, methods: bridge.methods, events: bridge.events, experimental: { methods: bridge.experimentalMethods, events: bridge.experimentalEvents }, }) console.log( `[ui-contract] OK (uiApi=${uiApi}, parts=${stable.parts.length}+${experimental.parts.length}x, ` + `tokens=${stable.tokens.length}, slots=${stable.slots.length}+${experimental.slots.length}x, layers=${stable.layers.length}, ` + `icons=${stable.icons.length}, sdkEvents=${stable.sdkEvents.length}, ` + `bridgeApi=${bridgeApi}, methods=${bridge.methods.length}+${bridge.experimentalMethods.length}x)`, )