|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { walkFiles } from './files.js'; |
| 4 | +import { stripComments } from './imports.js'; |
| 5 | +import { loadSchema } from './manifest.js'; |
| 6 | +import { loadCatalogue } from './tokens.js'; |
| 7 | + |
| 8 | +/** Blanks url(...) contents and quoted strings, preserving length, so a semicolon or colon |
| 9 | + * inside a token's value (a data: URL, say) is never mistaken for a declaration or |
| 10 | + * property-name boundary. Only property names are checked, so values may be replaced. */ |
| 11 | +export function sanitizeDeclarations(text) { |
| 12 | + return text |
| 13 | + .replace(/url\(([^)]*)\)/g, (m, inner) => `url(${' '.repeat(inner.length)})`) |
| 14 | + .replace(/"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)*'/g, (m) => ' '.repeat(m.length)); |
| 15 | +} |
| 16 | + |
| 17 | +/** A theme is :root blocks of --yeti-* public tokens, nothing else. */ |
| 18 | +export function validateThemes(root) { |
| 19 | + const themesDir = path.join(root, 'src', 'themes'); |
| 20 | + const catalogueFile = path.join(root, 'src', 'tokens', 'tokens.json'); |
| 21 | + if (!fs.existsSync(themesDir) || !fs.existsSync(catalogueFile)) return []; |
| 22 | + const schema = loadSchema(path.join(root, 'schema', 'tokens.schema.json')); |
| 23 | + const { entries } = loadCatalogue(catalogueFile, schema); |
| 24 | + const publicNames = new Set(entries.filter((e) => e.public).map((e) => e.name)); |
| 25 | + const errors = []; |
| 26 | + for (const file of walkFiles(themesDir).filter((f) => f.endsWith('.css'))) { |
| 27 | + const text = stripComments(fs.readFileSync(file, 'utf8')); |
| 28 | + const stack = []; |
| 29 | + let selector = ''; |
| 30 | + let line = 1; |
| 31 | + let selectorLine = 1; |
| 32 | + let decls = ''; |
| 33 | + for (const ch of text) { |
| 34 | + if (ch === '{') { |
| 35 | + const sel = selector.trim(); |
| 36 | + if (/^@media\s*\(\s*prefers-color-scheme:\s*(light|dark)\s*\)$/.test(sel) && !(stack.length && stack.at(-1).kind === 'media')) { |
| 37 | + stack.push({ kind: 'media' }); |
| 38 | + } else if (sel === ':root' && (stack.length === 0 || stack.at(-1).kind === 'media')) { |
| 39 | + stack.push({ kind: 'root', line: selectorLine }); |
| 40 | + } else { |
| 41 | + errors.push({ file, line: selectorLine, message: `themes may only set --yeti-* tokens on :root (found "${sel}")` }); |
| 42 | + stack.push({ kind: 'other' }); |
| 43 | + } |
| 44 | + selector = ''; decls = ''; |
| 45 | + } else if (ch === '}') { |
| 46 | + const block = stack.pop(); |
| 47 | + if (block?.kind === 'root') { |
| 48 | + for (const d of sanitizeDeclarations(decls).split(';')) { |
| 49 | + const [prop] = d.split(':').map((s) => s.trim()); |
| 50 | + if (!prop) continue; |
| 51 | + if (!prop.startsWith('--yeti-')) errors.push({ file, line: block.line, message: `themes may only set --yeti-* tokens (found "${prop}")` }); |
| 52 | + else if (!publicNames.has(prop)) errors.push({ file, line: block.line, message: `theme sets "${prop}", which is not a public token` }); |
| 53 | + } |
| 54 | + } |
| 55 | + selector = ''; decls = ''; |
| 56 | + } else { |
| 57 | + if (stack.length && stack.at(-1).kind === 'root') decls += ch; else selector += ch; |
| 58 | + if (ch === '\n') { line++; if (!selector.trim()) selectorLine = line; } |
| 59 | + } |
| 60 | + } |
| 61 | + // A statement at-rule (@import …; or @layer x;) never opens a block, so the |
| 62 | + // char loop above never sees it: it just keeps accumulating in `selector` |
| 63 | + // until the file ends. Catch that leftover text here. |
| 64 | + if (selector.trim()) { |
| 65 | + errors.push({ file, line: selectorLine, message: `themes may only set --yeti-* tokens on :root (found "${selector.trim()}")` }); |
| 66 | + } |
| 67 | + } |
| 68 | + return errors; |
| 69 | +} |
0 commit comments