|
| 1 | +/** |
| 2 | + * Styling Preflight — single-call tool that combines component API discovery, |
| 3 | + * CSS reference resolution, and validation into one response. |
| 4 | + * |
| 5 | + * Agents call this ONCE with their CSS (and optional HTML) to get: |
| 6 | + * 1. The component's full style API surface (parts, tokens, slots) |
| 7 | + * 2. Resolution of every ::part() and token reference (valid vs hallucinated) |
| 8 | + * 3. Validation issues (shadow DOM, theme, specificity) |
| 9 | + * 4. A correct CSS snippet to use as reference |
| 10 | + * 5. A pass/fail verdict |
| 11 | + * |
| 12 | + * This eliminates the "forgot to check the API first" failure mode. |
| 13 | + */ |
| 14 | + |
| 15 | +import type { ComponentMetadata } from './cem.js'; |
| 16 | +import { resolveCssApi, type CssApiResolution } from './css-api-resolver.js'; |
| 17 | +import { checkShadowDomUsage } from './shadow-dom-checker.js'; |
| 18 | +import { checkThemeCompatibility } from './theme-checker.js'; |
| 19 | +import { buildCssSnippet } from './styling-diagnostics.js'; |
| 20 | + |
| 21 | +// ─── Types ─────────────────────────────────────────────────────────────────── |
| 22 | + |
| 23 | +export interface PreflightInput { |
| 24 | + css: string; |
| 25 | + html?: string; |
| 26 | + meta: ComponentMetadata; |
| 27 | +} |
| 28 | + |
| 29 | +export interface PreflightIssue { |
| 30 | + severity: 'error' | 'warning' | 'info'; |
| 31 | + category: string; |
| 32 | + message: string; |
| 33 | + line?: number; |
| 34 | + suggestion?: string; |
| 35 | +} |
| 36 | + |
| 37 | +export interface PreflightComponentApi { |
| 38 | + tagName: string; |
| 39 | + description: string; |
| 40 | + parts: string[]; |
| 41 | + tokens: string[]; |
| 42 | + slots: string[]; |
| 43 | + hasStyleApi: boolean; |
| 44 | +} |
| 45 | + |
| 46 | +export interface PreflightResult { |
| 47 | + componentApi: PreflightComponentApi; |
| 48 | + resolution: CssApiResolution; |
| 49 | + issues: PreflightIssue[]; |
| 50 | + correctSnippet: string; |
| 51 | + verdict: string; |
| 52 | +} |
| 53 | + |
| 54 | +// ─── Main Entry Point ─────────────────────────────────────────────────────── |
| 55 | + |
| 56 | +export function runStylingPreflight(input: PreflightInput): PreflightResult { |
| 57 | + const { css, html, meta } = input; |
| 58 | + const issues: PreflightIssue[] = []; |
| 59 | + |
| 60 | + // 1. Resolve CSS references against CEM |
| 61 | + const resolution = resolveCssApi(css, meta, html); |
| 62 | + |
| 63 | + // 2. Run shadow DOM validation (if CSS is non-empty) |
| 64 | + if (css.trim()) { |
| 65 | + try { |
| 66 | + const shadowResult = checkShadowDomUsage(css, meta.tagName, meta); |
| 67 | + for (const issue of shadowResult.issues) { |
| 68 | + issues.push({ |
| 69 | + severity: issue.severity === 'error' ? 'error' : 'warning', |
| 70 | + category: 'shadowDom', |
| 71 | + message: issue.message, |
| 72 | + line: issue.line, |
| 73 | + suggestion: issue.suggestion, |
| 74 | + }); |
| 75 | + } |
| 76 | + } catch { |
| 77 | + // Shadow DOM check failed — skip |
| 78 | + } |
| 79 | + |
| 80 | + // 3. Run theme compatibility check |
| 81 | + try { |
| 82 | + const themeResult = checkThemeCompatibility(css); |
| 83 | + for (const issue of themeResult.issues) { |
| 84 | + issues.push({ |
| 85 | + severity: 'warning', |
| 86 | + category: 'themeCompat', |
| 87 | + message: issue.message, |
| 88 | + line: issue.line, |
| 89 | + }); |
| 90 | + } |
| 91 | + } catch { |
| 92 | + // Theme check failed — skip |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // 4. Build the component API summary |
| 97 | + const componentApi: PreflightComponentApi = { |
| 98 | + tagName: meta.tagName, |
| 99 | + description: meta.description, |
| 100 | + parts: meta.cssParts.map((p) => p.name), |
| 101 | + tokens: meta.cssProperties.map((p) => p.name), |
| 102 | + slots: meta.slots.map((s) => (s.name === '' ? '(default)' : s.name)), |
| 103 | + hasStyleApi: meta.cssParts.length > 0 || meta.cssProperties.length > 0 || meta.slots.length > 0, |
| 104 | + }; |
| 105 | + |
| 106 | + // 5. Generate correct CSS snippet |
| 107 | + const correctSnippet = buildCssSnippet(meta); |
| 108 | + |
| 109 | + // 6. Build verdict |
| 110 | + const verdict = buildVerdict(resolution, issues); |
| 111 | + |
| 112 | + return { |
| 113 | + componentApi, |
| 114 | + resolution, |
| 115 | + issues, |
| 116 | + correctSnippet, |
| 117 | + verdict, |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | +// ─── Verdict Builder ──────────────────────────────────────────────────────── |
| 122 | + |
| 123 | +function buildVerdict(resolution: CssApiResolution, issues: PreflightIssue[]): string { |
| 124 | + const errors = issues.filter((i) => i.severity === 'error').length; |
| 125 | + const warnings = issues.filter((i) => i.severity === 'warning').length; |
| 126 | + const invalidRefs = resolution.invalidCount; |
| 127 | + |
| 128 | + if (errors === 0 && warnings === 0 && invalidRefs === 0) { |
| 129 | + return 'pass — CSS references are valid and no anti-patterns detected.'; |
| 130 | + } |
| 131 | + |
| 132 | + const parts: string[] = []; |
| 133 | + if (invalidRefs > 0) { |
| 134 | + parts.push( |
| 135 | + `${invalidRefs} invalid CSS reference${invalidRefs > 1 ? 's' : ''} (hallucinated part/token/slot names)`, |
| 136 | + ); |
| 137 | + } |
| 138 | + if (errors > 0) { |
| 139 | + parts.push(`${errors} error${errors > 1 ? 's' : ''} (will break at runtime)`); |
| 140 | + } |
| 141 | + if (warnings > 0) { |
| 142 | + parts.push(`${warnings} warning${warnings > 1 ? 's' : ''} (theme/dark mode risk)`); |
| 143 | + } |
| 144 | + |
| 145 | + return 'fail — ' + parts.join(', ') + '.'; |
| 146 | +} |
0 commit comments