@getchomp/mcp 0.0.0-stage → 0.2.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -2
- package/dist/conventionsReference.d.ts +84 -0
- package/dist/conventionsReference.js +313 -0
- package/dist/designSystem/astScanner.d.ts +153 -0
- package/dist/designSystem/astScanner.js +2014 -0
- package/dist/designSystem/baseline.d.ts +65 -0
- package/dist/designSystem/baseline.js +136 -0
- package/dist/designSystem/color.d.ts +35 -0
- package/dist/designSystem/color.js +265 -0
- package/dist/designSystem/config.d.ts +183 -0
- package/dist/designSystem/config.js +469 -0
- package/dist/designSystem/contrast.d.ts +55 -0
- package/dist/designSystem/contrast.js +377 -0
- package/dist/designSystem/cssParser.d.ts +63 -0
- package/dist/designSystem/cssParser.js +469 -0
- package/dist/designSystem/merges.d.ts +57 -0
- package/dist/designSystem/merges.js +94 -0
- package/dist/designSystem/preprocessors.d.ts +46 -0
- package/dist/designSystem/preprocessors.js +1108 -0
- package/dist/designSystem/presentation.d.ts +45 -0
- package/dist/designSystem/presentation.js +796 -0
- package/dist/designSystem/styleVectors.d.ts +27 -0
- package/dist/designSystem/styleVectors.js +484 -0
- package/dist/designSystem/tokens.d.ts +39 -0
- package/dist/designSystem/tokens.js +409 -0
- package/dist/designSystem/topology.d.ts +64 -0
- package/dist/designSystem/topology.js +254 -0
- package/dist/designSystem/types.d.ts +857 -0
- package/dist/designSystem/types.js +113 -0
- package/dist/designSystem/typography.d.ts +25 -0
- package/dist/designSystem/typography.js +244 -0
- package/dist/designSystem/uiShapes.d.ts +74 -0
- package/dist/designSystem/uiShapes.js +285 -0
- package/dist/designSystem/valueClusters.d.ts +76 -0
- package/dist/designSystem/valueClusters.js +281 -0
- package/dist/designSystemPlugin.d.ts +321 -0
- package/dist/designSystemPlugin.js +4730 -0
- package/dist/enrichNodesPlugin.d.ts +160 -0
- package/dist/enrichNodesPlugin.js +957 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +1298 -0
- package/dist/intelligence.d.ts +502 -0
- package/dist/intelligence.js +3058 -0
- package/dist/pluginRunner.d.ts +364 -0
- package/dist/pluginRunner.js +1096 -0
- package/dist/plugins/design-system/index.d.ts +14 -0
- package/dist/plugins/design-system/index.js +241 -0
- package/dist/plugins/enrich-nodes/index.d.ts +388 -0
- package/dist/plugins/enrich-nodes/index.js +697 -0
- package/dist/plugins/index.d.ts +15 -0
- package/dist/plugins/index.js +20 -0
- package/dist/plugins/local-shims/index.d.ts +121 -0
- package/dist/plugins/local-shims/index.js +237 -0
- package/dist/shimsPlugin.d.ts +134 -0
- package/dist/shimsPlugin.js +1392 -0
- package/package.json +38 -3
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/contrast.ts
|
|
3
|
+
*
|
|
4
|
+
* WCAG 2.1 Color & Theme Contrast Engine (`dark` vs `light`, or single-theme `root`),
|
|
5
|
+
* discovering foreground/background pairs from config, CSS rules, naming conventions,
|
|
6
|
+
* and JSX Tailwind classes (with `@theme --text-*` font-size guard).
|
|
7
|
+
*/
|
|
8
|
+
import { compositeOver, computeRelativeLuminance, computeWcagContrastRatio, parseCssColorString, rgbaToHex, } from "./color.js";
|
|
9
|
+
export { computeRelativeLuminance, computeWcagContrastRatio, parseCssColorString, };
|
|
10
|
+
export const CANVAS_TOKEN_PROBES = [
|
|
11
|
+
"--background",
|
|
12
|
+
"--color-background",
|
|
13
|
+
"--bg",
|
|
14
|
+
"--surface-canvas",
|
|
15
|
+
"--surface",
|
|
16
|
+
"--color-bg",
|
|
17
|
+
];
|
|
18
|
+
export const VAR_EXPR_RE = /var\(\s*(--[a-zA-Z0-9_-]+)\s*(?:,\s*((?:[^()]|\([^()]*\))*))?\)/g;
|
|
19
|
+
/** Resolves `var(--x)` chains textually; returns `null` when a token has no value and no fallback. */
|
|
20
|
+
export function substituteVars(value, themeMap, rootMap, bridgeMap = new Map(), depth = 0) {
|
|
21
|
+
if (depth > 8)
|
|
22
|
+
return null;
|
|
23
|
+
let failed = false;
|
|
24
|
+
const out = value.replace(VAR_EXPR_RE, (_m, name, fallback) => {
|
|
25
|
+
const decl = themeMap.get(name) ?? rootMap.get(name) ?? bridgeMap.get(name);
|
|
26
|
+
if (decl) {
|
|
27
|
+
const resolved = substituteVars(decl.value, themeMap, rootMap, bridgeMap, depth + 1);
|
|
28
|
+
if (resolved !== null)
|
|
29
|
+
return resolved;
|
|
30
|
+
}
|
|
31
|
+
if (fallback !== undefined && fallback.trim() !== "") {
|
|
32
|
+
const fb = substituteVars(fallback.trim(), themeMap, rootMap, bridgeMap, depth + 1);
|
|
33
|
+
if (fb !== null)
|
|
34
|
+
return fb;
|
|
35
|
+
}
|
|
36
|
+
failed = true;
|
|
37
|
+
return "";
|
|
38
|
+
});
|
|
39
|
+
return failed ? null : out.trim();
|
|
40
|
+
}
|
|
41
|
+
const BARE_HSL_RE = /^-?\d*\.?\d+(?:deg)?\s+-?\d*\.?\d+%\s+-?\d*\.?\d+%(?:\s*\/\s*[\d.%]+)?$/;
|
|
42
|
+
/** shadcn-style bare HSL triplets (`--background: 0 0% 100%`) are colors consumed as `hsl(var(--background))`. */
|
|
43
|
+
export function normalizeBareHsl(value) {
|
|
44
|
+
return BARE_HSL_RE.test(value.trim()) ? `hsl(${value.trim()})` : value;
|
|
45
|
+
}
|
|
46
|
+
export const TW_ALPHA_RE = /^--alpha\(\s*(.+)\s*\/\s*([\d.]+)(%?)\s*\)$/i;
|
|
47
|
+
export function parseColorWithAlphaSyntax(value) {
|
|
48
|
+
if (!value)
|
|
49
|
+
return null;
|
|
50
|
+
const alphaMatch = TW_ALPHA_RE.exec(value);
|
|
51
|
+
if (alphaMatch) {
|
|
52
|
+
const inner = parseCssColorString(normalizeBareHsl(alphaMatch[1]));
|
|
53
|
+
const rawFactor = Number(alphaMatch[2]);
|
|
54
|
+
if (!inner || !Number.isFinite(rawFactor))
|
|
55
|
+
return null;
|
|
56
|
+
const factor = Math.max(0, Math.min(1, alphaMatch[3] === "%" ? rawFactor / 100 : rawFactor));
|
|
57
|
+
return { ...inner, a: inner.a * factor };
|
|
58
|
+
}
|
|
59
|
+
return parseCssColorString(normalizeBareHsl(value));
|
|
60
|
+
}
|
|
61
|
+
export function resolveThemeTokenColor(tokenName, themeMap, rootMap, bridgeMap = new Map()) {
|
|
62
|
+
if (!themeMap.has(tokenName) && !rootMap.has(tokenName) && !bridgeMap.has(tokenName)) {
|
|
63
|
+
return { ok: false, reason: "undefined" };
|
|
64
|
+
}
|
|
65
|
+
const decl = (themeMap.get(tokenName) ?? rootMap.get(tokenName) ?? bridgeMap.get(tokenName));
|
|
66
|
+
const substituted = substituteVars(decl.value, themeMap, rootMap, bridgeMap);
|
|
67
|
+
if (substituted === null) {
|
|
68
|
+
return { ok: false, reason: "unsupported", detail: `unresolvable var() in "${decl.value}"` };
|
|
69
|
+
}
|
|
70
|
+
const alphaMatch = TW_ALPHA_RE.exec(substituted);
|
|
71
|
+
if (alphaMatch) {
|
|
72
|
+
const innerColor = parseCssColorString(normalizeBareHsl(alphaMatch[1]));
|
|
73
|
+
const rawFactor = Number(alphaMatch[2]);
|
|
74
|
+
if (!innerColor || !Number.isFinite(rawFactor)) {
|
|
75
|
+
return { ok: false, reason: "unsupported", detail: `"${substituted.slice(0, 60)}"` };
|
|
76
|
+
}
|
|
77
|
+
const factor = Math.max(0, Math.min(1, alphaMatch[3] === "%" ? rawFactor / 100 : rawFactor));
|
|
78
|
+
return { ok: true, color: { ...innerColor, a: innerColor.a * factor } };
|
|
79
|
+
}
|
|
80
|
+
const color = parseCssColorString(normalizeBareHsl(substituted));
|
|
81
|
+
if (!color) {
|
|
82
|
+
return { ok: false, reason: "unsupported", detail: `"${substituted.slice(0, 60)}"` };
|
|
83
|
+
}
|
|
84
|
+
return { ok: true, color };
|
|
85
|
+
}
|
|
86
|
+
const BARE_VAR_REF_RE = /^var\(\s*(--[a-zA-Z0-9_-]+)\s*\)$/;
|
|
87
|
+
export function evaluateContrast(tokenDeclarations, styleRules, options = {}) {
|
|
88
|
+
const minRatio = options.minRatio ?? 4.5;
|
|
89
|
+
const rootMap = new Map();
|
|
90
|
+
const darkMap = new Map();
|
|
91
|
+
const lightMap = new Map();
|
|
92
|
+
const bridgeMap = new Map();
|
|
93
|
+
for (const decl of tokenDeclarations) {
|
|
94
|
+
if (decl.scope === "scoped")
|
|
95
|
+
continue;
|
|
96
|
+
if (decl.themeContext === "root")
|
|
97
|
+
rootMap.set(decl.name, decl);
|
|
98
|
+
else if (decl.themeContext === "dark")
|
|
99
|
+
darkMap.set(decl.name, decl);
|
|
100
|
+
else if (decl.themeContext === "light")
|
|
101
|
+
lightMap.set(decl.name, decl);
|
|
102
|
+
else if (decl.themeContext === "theme-bridge")
|
|
103
|
+
bridgeMap.set(decl.name, decl);
|
|
104
|
+
}
|
|
105
|
+
const definedNames = new Set([
|
|
106
|
+
...rootMap.keys(),
|
|
107
|
+
...darkMap.keys(),
|
|
108
|
+
...lightMap.keys(),
|
|
109
|
+
]);
|
|
110
|
+
const allKnownTokenNames = new Set([
|
|
111
|
+
...definedNames,
|
|
112
|
+
...bridgeMap.keys(),
|
|
113
|
+
]);
|
|
114
|
+
const knownLowerToCanonical = new Map();
|
|
115
|
+
for (const n of allKnownTokenNames) {
|
|
116
|
+
if (!knownLowerToCanonical.has(n.toLowerCase())) {
|
|
117
|
+
knownLowerToCanonical.set(n.toLowerCase(), n);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const bridgeLowerNames = new Set();
|
|
121
|
+
for (const n of bridgeMap.keys())
|
|
122
|
+
bridgeLowerNames.add(n.toLowerCase());
|
|
123
|
+
// Tailwind v4 `@theme` `--text-<slug>` declares font-size utilities (`text-hud-2xs`), not colors.
|
|
124
|
+
const isNonColorBridgeTextSlug = (slug) => bridgeLowerNames.has(`--text-${slug.toLowerCase()}`) &&
|
|
125
|
+
!bridgeLowerNames.has(`--text-color-${slug.toLowerCase()}`) &&
|
|
126
|
+
!bridgeLowerNames.has(`--color-${slug.toLowerCase()}`);
|
|
127
|
+
const canonicalizeBridgeToken = (tokenName) => {
|
|
128
|
+
if (definedNames.has(tokenName))
|
|
129
|
+
return tokenName;
|
|
130
|
+
const bridgeDecl = bridgeMap.get(tokenName);
|
|
131
|
+
if (!bridgeDecl || !tokenName.toLowerCase().startsWith("--color-"))
|
|
132
|
+
return tokenName;
|
|
133
|
+
const m = BARE_VAR_REF_RE.exec(bridgeDecl.value.trim());
|
|
134
|
+
if (!m)
|
|
135
|
+
return tokenName;
|
|
136
|
+
const target = knownLowerToCanonical.get(m[1].toLowerCase()) ?? m[1];
|
|
137
|
+
const stem = tokenName.slice("--color-".length);
|
|
138
|
+
if (definedNames.has(target) && target.toLowerCase() === `--${stem.toLowerCase()}`) {
|
|
139
|
+
return target;
|
|
140
|
+
}
|
|
141
|
+
return tokenName;
|
|
142
|
+
};
|
|
143
|
+
const pairs = [];
|
|
144
|
+
const seen = new Set();
|
|
145
|
+
const addPair = (fg, bg, label, discovery, pairMin, pairId) => {
|
|
146
|
+
const key = `${fg}::${bg}`;
|
|
147
|
+
if (seen.has(key))
|
|
148
|
+
return;
|
|
149
|
+
seen.add(key);
|
|
150
|
+
pairs.push({
|
|
151
|
+
pairId: pairId ?? `${discovery}:${fg}-on-${bg}`,
|
|
152
|
+
fg,
|
|
153
|
+
bg,
|
|
154
|
+
label,
|
|
155
|
+
minRatio: pairMin ?? minRatio,
|
|
156
|
+
discovery,
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
for (const p of options.pairs ?? []) {
|
|
160
|
+
addPair(p.fg, p.bg, p.label ?? `${p.fg} on ${p.bg}`, "config", p.minRatio, `config:${p.fg}-on-${p.bg}`);
|
|
161
|
+
}
|
|
162
|
+
if (options.discoverFromRules !== false) {
|
|
163
|
+
for (const rule of styleRules) {
|
|
164
|
+
let fgVar;
|
|
165
|
+
let bgVar;
|
|
166
|
+
for (const d of rule.declarations) {
|
|
167
|
+
if (d.prop === "color") {
|
|
168
|
+
const m = d.value.match(/^var\(\s*(--[a-zA-Z0-9_-]+)\s*\)$/);
|
|
169
|
+
if (m)
|
|
170
|
+
fgVar = m[1];
|
|
171
|
+
}
|
|
172
|
+
else if (d.prop === "background" || d.prop === "background-color") {
|
|
173
|
+
const m = d.value.match(/^var\(\s*(--[a-zA-Z0-9_-]+)\s*\)$/);
|
|
174
|
+
if (m)
|
|
175
|
+
bgVar = m[1];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (fgVar && bgVar) {
|
|
179
|
+
addPair(fgVar, bgVar, `CSS rule '${rule.selector}' (${fgVar} on ${bgVar})`, "rule", undefined, `rule:${rule.selector}:${fgVar}-on-${bgVar}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (options.discoverFromNaming !== false) {
|
|
184
|
+
for (const name of [...allKnownTokenNames].sort()) {
|
|
185
|
+
const lower = name.toLowerCase();
|
|
186
|
+
let fgCandidate;
|
|
187
|
+
let bgCandidates = [];
|
|
188
|
+
const suffix = lower.match(/^(--.+)-(?:foreground|fg|contrast)$/);
|
|
189
|
+
const onMatch = !suffix ? lower.match(/^(.*-)on-(.+)$/) : null;
|
|
190
|
+
const surfaceMatch = !suffix && !onMatch ? lower.match(/^(--.+)-surface$/) : null;
|
|
191
|
+
if (lower === "--foreground") {
|
|
192
|
+
fgCandidate = name;
|
|
193
|
+
bgCandidates = ["--background"];
|
|
194
|
+
}
|
|
195
|
+
else if (suffix) {
|
|
196
|
+
fgCandidate = name;
|
|
197
|
+
bgCandidates = [suffix[1]];
|
|
198
|
+
}
|
|
199
|
+
else if (onMatch) {
|
|
200
|
+
fgCandidate = name;
|
|
201
|
+
bgCandidates = [
|
|
202
|
+
`${onMatch[1]}${onMatch[2]}`,
|
|
203
|
+
...(onMatch[1] === "--" ? [`--color-${onMatch[2]}`] : []),
|
|
204
|
+
];
|
|
205
|
+
}
|
|
206
|
+
else if (surfaceMatch) {
|
|
207
|
+
const fgMatch = knownLowerToCanonical.get(surfaceMatch[1]);
|
|
208
|
+
if (fgMatch) {
|
|
209
|
+
const canonFg = canonicalizeBridgeToken(fgMatch);
|
|
210
|
+
const canonBg = canonicalizeBridgeToken(name);
|
|
211
|
+
if (canonFg !== canonBg) {
|
|
212
|
+
addPair(canonFg, canonBg, `${canonFg} on ${canonBg} (naming convention)`, "naming");
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (!fgCandidate)
|
|
218
|
+
continue;
|
|
219
|
+
for (const candidateBg of bgCandidates) {
|
|
220
|
+
const resolvedBg = knownLowerToCanonical.get(candidateBg.toLowerCase());
|
|
221
|
+
if (resolvedBg) {
|
|
222
|
+
const canonFg = canonicalizeBridgeToken(fgCandidate);
|
|
223
|
+
const canonBg = canonicalizeBridgeToken(resolvedBg);
|
|
224
|
+
if (canonFg !== canonBg) {
|
|
225
|
+
addPair(canonFg, canonBg, `${canonFg} on ${canonBg} (naming convention)`, "naming");
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const hasDark = darkMap.size > 0;
|
|
233
|
+
const hasLight = lightMap.size > 0;
|
|
234
|
+
const themes = [
|
|
235
|
+
hasLight ? { key: "light", map: lightMap } : { key: "root", map: rootMap },
|
|
236
|
+
...(hasDark ? [{ key: "dark", map: darkMap }] : []),
|
|
237
|
+
];
|
|
238
|
+
const unresolvedTailwindCandidates = new Map();
|
|
239
|
+
if (options.discoverFromTailwindClasses !== false && options.tailwindClassPairs?.length) {
|
|
240
|
+
const resolveTwToken = (slug, role) => {
|
|
241
|
+
const candidates = bridgeMap.size > 0
|
|
242
|
+
? role === "fg"
|
|
243
|
+
? [`--text-color-${slug}`, `--color-${slug}`]
|
|
244
|
+
: [`--background-color-${slug}`, `--color-${slug}`]
|
|
245
|
+
: [`--color-${slug}`, `--${slug}`];
|
|
246
|
+
for (const c of candidates) {
|
|
247
|
+
const found = knownLowerToCanonical.get(c.toLowerCase());
|
|
248
|
+
if (found)
|
|
249
|
+
return found;
|
|
250
|
+
}
|
|
251
|
+
return undefined;
|
|
252
|
+
};
|
|
253
|
+
for (const tw of options.tailwindClassPairs) {
|
|
254
|
+
if (isNonColorBridgeTextSlug(tw.fgSlug))
|
|
255
|
+
continue;
|
|
256
|
+
const rawFg = resolveTwToken(tw.fgSlug, "fg");
|
|
257
|
+
const rawBg = resolveTwToken(tw.bgSlug, "bg");
|
|
258
|
+
if (rawFg && rawBg) {
|
|
259
|
+
const canonFg = canonicalizeBridgeToken(rawFg);
|
|
260
|
+
const canonBg = canonicalizeBridgeToken(rawBg);
|
|
261
|
+
if (canonFg !== canonBg) {
|
|
262
|
+
addPair(canonFg, canonBg, `JSX classes 'text-${tw.fgSlug} bg-${tw.bgSlug}' (${canonFg} on ${canonBg})`, "tailwind-jsx", undefined, `tailwind:${canonFg}-on-${canonBg}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else if (rawFg || rawBg) {
|
|
266
|
+
const canonFg = rawFg ? canonicalizeBridgeToken(rawFg) : `--color-${tw.fgSlug}`;
|
|
267
|
+
const canonBg = rawBg ? canonicalizeBridgeToken(rawBg) : `--color-${tw.bgSlug}`;
|
|
268
|
+
const bareKey = `--${tw.fgSlug}::--${tw.bgSlug}`;
|
|
269
|
+
const directKey = `${canonFg}::${canonBg}`;
|
|
270
|
+
if (!seen.has(bareKey) && !seen.has(directKey)) {
|
|
271
|
+
const pairId = `tailwind:text-${tw.fgSlug}-on-bg-${tw.bgSlug}`;
|
|
272
|
+
if (!unresolvedTailwindCandidates.has(pairId)) {
|
|
273
|
+
unresolvedTailwindCandidates.set(pairId, {
|
|
274
|
+
pairId,
|
|
275
|
+
foregroundToken: canonFg,
|
|
276
|
+
backgroundToken: canonBg,
|
|
277
|
+
theme: hasLight ? "light" : "root",
|
|
278
|
+
reason: rawFg ? "background-unresolved" : "foreground-unresolved",
|
|
279
|
+
detail: `Tailwind utility '${rawFg ? `bg-${tw.bgSlug}` : `text-${tw.fgSlug}`}' has no matching token in ${bridgeMap.size > 0 ? "@theme" : "stylesheets"}`,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
const probe = [options.canvasToken, ...CANVAS_TOKEN_PROBES].filter((t) => Boolean(t));
|
|
287
|
+
const canvasFor = (themeMap) => {
|
|
288
|
+
for (const t of probe) {
|
|
289
|
+
const r = resolveThemeTokenColor(t, themeMap, rootMap, bridgeMap);
|
|
290
|
+
if (r.ok && r.color.a >= 0.999)
|
|
291
|
+
return r.color;
|
|
292
|
+
}
|
|
293
|
+
return null;
|
|
294
|
+
};
|
|
295
|
+
const audits = [];
|
|
296
|
+
const unresolved = [...unresolvedTailwindCandidates.values()];
|
|
297
|
+
for (const pair of pairs) {
|
|
298
|
+
const fgKnown = allKnownTokenNames.has(pair.fg);
|
|
299
|
+
const bgKnown = allKnownTokenNames.has(pair.bg);
|
|
300
|
+
if (pair.discovery !== "config" && (!fgKnown || !bgKnown))
|
|
301
|
+
continue;
|
|
302
|
+
const results = {};
|
|
303
|
+
let anyEvaluated = false;
|
|
304
|
+
for (const theme of themes) {
|
|
305
|
+
const fgRes = resolveThemeTokenColor(pair.fg, theme.map, rootMap, bridgeMap);
|
|
306
|
+
const bgRes = resolveThemeTokenColor(pair.bg, theme.map, rootMap, bridgeMap);
|
|
307
|
+
const report = (reason, detail) => {
|
|
308
|
+
unresolved.push({
|
|
309
|
+
pairId: pair.pairId,
|
|
310
|
+
foregroundToken: pair.fg,
|
|
311
|
+
backgroundToken: pair.bg,
|
|
312
|
+
theme: theme.key,
|
|
313
|
+
reason,
|
|
314
|
+
...(detail ? { detail } : {}),
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
if (!fgRes.ok) {
|
|
318
|
+
report(fgRes.reason === "undefined" ? "foreground-unresolved" : "unsupported-color-syntax", fgRes.reason === "unsupported" ? fgRes.detail : undefined);
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (!bgRes.ok) {
|
|
322
|
+
report(bgRes.reason === "undefined" ? "background-unresolved" : "unsupported-color-syntax", bgRes.reason === "unsupported" ? bgRes.detail : undefined);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
let bg = bgRes.color;
|
|
326
|
+
if (bg.a < 0.999) {
|
|
327
|
+
const canvas = canvasFor(theme.map);
|
|
328
|
+
if (!canvas) {
|
|
329
|
+
report("background-translucent-without-base");
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
bg = compositeOver(bg, canvas);
|
|
333
|
+
}
|
|
334
|
+
const fg = compositeOver(fgRes.color, bg);
|
|
335
|
+
const ratio = computeWcagContrastRatio(fg, bg);
|
|
336
|
+
results[theme.key] = {
|
|
337
|
+
fgHex: rgbaToHex(fg),
|
|
338
|
+
bgHex: rgbaToHex(bg),
|
|
339
|
+
ratio,
|
|
340
|
+
passes: ratio >= pair.minRatio,
|
|
341
|
+
};
|
|
342
|
+
anyEvaluated = true;
|
|
343
|
+
}
|
|
344
|
+
if (!anyEvaluated)
|
|
345
|
+
continue;
|
|
346
|
+
const evaluated = Object.values(results);
|
|
347
|
+
const passesAll = evaluated.every((r) => r.passes);
|
|
348
|
+
const primaryDecl = (results.light && !results.light.passes
|
|
349
|
+
? lightMap.get(pair.fg) ?? lightMap.get(pair.bg)
|
|
350
|
+
: undefined) ??
|
|
351
|
+
darkMap.get(pair.fg) ??
|
|
352
|
+
rootMap.get(pair.fg) ??
|
|
353
|
+
bridgeMap.get(pair.fg) ??
|
|
354
|
+
darkMap.get(pair.bg) ??
|
|
355
|
+
rootMap.get(pair.bg) ??
|
|
356
|
+
bridgeMap.get(pair.bg);
|
|
357
|
+
audits.push({
|
|
358
|
+
pairId: pair.pairId,
|
|
359
|
+
foregroundToken: pair.fg,
|
|
360
|
+
backgroundToken: pair.bg,
|
|
361
|
+
contextLabel: pair.label,
|
|
362
|
+
minRequiredRatio: pair.minRatio,
|
|
363
|
+
...(results.dark ? { dark: results.dark } : {}),
|
|
364
|
+
...(results.light ? { light: results.light } : {}),
|
|
365
|
+
...(results.root ? { root: results.root } : {}),
|
|
366
|
+
passesAll,
|
|
367
|
+
...(primaryDecl
|
|
368
|
+
? { filePath: primaryDecl.filePath, lineNumber: primaryDecl.lineNumber }
|
|
369
|
+
: {}),
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return { audits, unresolved };
|
|
373
|
+
}
|
|
374
|
+
/** Back-compat wrapper returning only evaluated audits. */
|
|
375
|
+
export function evaluateThemeContrastPairs(tokenDeclarations, styleRules, options) {
|
|
376
|
+
return evaluateContrast(tokenDeclarations, styleRules, options).audits;
|
|
377
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/cssParser.ts
|
|
3
|
+
*
|
|
4
|
+
* Line-preserving CSS/SCSS/Less stylesheet parser: extracts token declarations,
|
|
5
|
+
* var() references (preserving global-theme vs style-rule distinction),
|
|
6
|
+
* parsed style rules with selector structure metadata, and color/length literals.
|
|
7
|
+
*/
|
|
8
|
+
import { type DesignSystemConfig } from "./config.js";
|
|
9
|
+
import { type StylesheetSyntax } from "./preprocessors.js";
|
|
10
|
+
import type { CssTokenDeclaration, TokenReferenceOccurrence, TokenTier } from "./types.js";
|
|
11
|
+
import { type LiteralValueOccurrence } from "./valueClusters.js";
|
|
12
|
+
export declare function buildLineStarts(text: string): number[];
|
|
13
|
+
export declare function offsetToLineNumber(lineStarts: number[], offset: number): number;
|
|
14
|
+
export declare function getLineSnippet(lines: string[], lineNumber: number): string;
|
|
15
|
+
export declare function stripCssCommentsPreservingLines(cssText: string): string;
|
|
16
|
+
export declare function classifyTokenTier(tokenName: string, inThemeBridge?: boolean, tiers?: DesignSystemConfig["tokens"]["tiers"]): TokenTier;
|
|
17
|
+
export declare const VAR_REF_RE: RegExp;
|
|
18
|
+
/** Token names referenced with a fallback (`var(--x, #fff)`): an intentional customization hook, not a leak. */
|
|
19
|
+
export declare function extractVarFallbackNames(value: string): Set<string>;
|
|
20
|
+
export declare function extractVarReferences(value: string): string[];
|
|
21
|
+
export interface ParsedCssRule {
|
|
22
|
+
selector: string;
|
|
23
|
+
filePath: string;
|
|
24
|
+
lineNumber: number;
|
|
25
|
+
declarations: Array<{
|
|
26
|
+
prop: string;
|
|
27
|
+
value: string;
|
|
28
|
+
lineNumber: number;
|
|
29
|
+
}>;
|
|
30
|
+
classesInSelector: string[];
|
|
31
|
+
tokensConsumed: string[];
|
|
32
|
+
/** Individual comma-separated selector parts (trimmed). */
|
|
33
|
+
selectorParts?: string[];
|
|
34
|
+
/** True when any selector part has multiple combinator-separated segments (e.g. `.drawer h1`). */
|
|
35
|
+
isMultiSegment?: boolean;
|
|
36
|
+
/** Heading tag names targeted as trailing segments (e.g. `h1` in `.drawer h1`). */
|
|
37
|
+
headingTagsTargeted?: string[];
|
|
38
|
+
}
|
|
39
|
+
export interface ParsedCssStylesheet {
|
|
40
|
+
filePath: string;
|
|
41
|
+
tokenDeclarations: CssTokenDeclaration[];
|
|
42
|
+
varReferences: TokenReferenceOccurrence[];
|
|
43
|
+
/** Token names referenced from actual style rules (consumer roots for live BFS). */
|
|
44
|
+
styleRuleTokenRefs: string[];
|
|
45
|
+
styleRules: ParsedCssRule[];
|
|
46
|
+
/** All CSS class selectors declared in non-global rules (including inside theme overrides). */
|
|
47
|
+
definedClasses: string[];
|
|
48
|
+
/** Hardcoded color/length literals found in non-token declarations. */
|
|
49
|
+
literals: LiteralValueOccurrence[];
|
|
50
|
+
}
|
|
51
|
+
export interface ParseCssOptions {
|
|
52
|
+
/** Compiled dark/light theme selector patterns (default: `DEFAULT_DESIGN_SYSTEM_CONFIG.themes`). */
|
|
53
|
+
themes?: {
|
|
54
|
+
dark: RegExp[];
|
|
55
|
+
light: RegExp[];
|
|
56
|
+
};
|
|
57
|
+
tiers?: DesignSystemConfig["tokens"]["tiers"];
|
|
58
|
+
/** Stylesheet syntax mode (`css`, `scss`, or `less`; auto-detected from `filePath` by default). */
|
|
59
|
+
syntax?: StylesheetSyntax;
|
|
60
|
+
/** True when parsing `<style scoped>` or Svelte/Astro component-scoped `<style>` blocks. */
|
|
61
|
+
defaultScoped?: boolean;
|
|
62
|
+
}
|
|
63
|
+
export declare function parseCssStylesheet(filePath: string, rawText: string, options?: ParseCssOptions): ParsedCssStylesheet;
|