@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,469 @@
|
|
|
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 { compileRegexes, DEFAULT_DESIGN_SYSTEM_CONFIG } from "./config.js";
|
|
9
|
+
import { detectStylesheetSyntax, extractPreprocessorVarReferences, LESS_AT_RULE_KEYWORDS, stripStylesheetCommentsPreservingLines, } from "./preprocessors.js";
|
|
10
|
+
import { isScaleStepTokenName } from "./tokens.js";
|
|
11
|
+
import { extractColorLiterals, extractLengthLiterals, isLengthSensitiveProperty, } from "./valueClusters.js";
|
|
12
|
+
export function buildLineStarts(text) {
|
|
13
|
+
const starts = [0];
|
|
14
|
+
for (let i = 0; i < text.length; i++) {
|
|
15
|
+
if (text.charCodeAt(i) === 10) {
|
|
16
|
+
starts.push(i + 1);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return starts;
|
|
20
|
+
}
|
|
21
|
+
export function offsetToLineNumber(lineStarts, offset) {
|
|
22
|
+
let lo = 0;
|
|
23
|
+
let hi = lineStarts.length - 1;
|
|
24
|
+
while (lo <= hi) {
|
|
25
|
+
const mid = (lo + hi) >> 1;
|
|
26
|
+
if (lineStarts[mid] <= offset) {
|
|
27
|
+
lo = mid + 1;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
hi = mid - 1;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return Math.max(1, hi + 1);
|
|
34
|
+
}
|
|
35
|
+
export function getLineSnippet(lines, lineNumber) {
|
|
36
|
+
const raw = lines[lineNumber - 1] ?? "";
|
|
37
|
+
return raw.trim().slice(0, 200);
|
|
38
|
+
}
|
|
39
|
+
export function stripCssCommentsPreservingLines(cssText) {
|
|
40
|
+
return cssText.replace(/\/\*[\s\S]*?\*\//g, (match) => match.replace(/[^\n]/g, " "));
|
|
41
|
+
}
|
|
42
|
+
export function classifyTokenTier(tokenName, inThemeBridge = false, tiers) {
|
|
43
|
+
if (inThemeBridge) {
|
|
44
|
+
return "semantic";
|
|
45
|
+
}
|
|
46
|
+
if (tiers) {
|
|
47
|
+
if (compileRegexes(tiers.component).some((re) => re.test(tokenName)))
|
|
48
|
+
return "component";
|
|
49
|
+
if (compileRegexes(tiers.semantic).some((re) => re.test(tokenName)))
|
|
50
|
+
return "semantic";
|
|
51
|
+
if (compileRegexes(tiers.primitive).some((re) => re.test(tokenName)))
|
|
52
|
+
return "primitive";
|
|
53
|
+
}
|
|
54
|
+
const lower = tokenName.toLowerCase();
|
|
55
|
+
if (/^(?:--|\$|@)primitive-/.test(lower) ||
|
|
56
|
+
/^(?:--|\$|@)(?:space|spacing|size|radius|font-size|font-family|leading|tracking|shadow|z|opacity)-/.test(lower) ||
|
|
57
|
+
isScaleStepTokenName(lower)) {
|
|
58
|
+
return "primitive";
|
|
59
|
+
}
|
|
60
|
+
return "semantic";
|
|
61
|
+
}
|
|
62
|
+
export const VAR_REF_RE = /var\(\s*(--[a-zA-Z0-9_-]+)(\s*,)?/g;
|
|
63
|
+
/** Token names referenced with a fallback (`var(--x, #fff)`): an intentional customization hook, not a leak. */
|
|
64
|
+
export function extractVarFallbackNames(value) {
|
|
65
|
+
const names = new Set();
|
|
66
|
+
for (const m of value.matchAll(VAR_REF_RE)) {
|
|
67
|
+
if (m[1] && m[2])
|
|
68
|
+
names.add(m[1]);
|
|
69
|
+
}
|
|
70
|
+
return names;
|
|
71
|
+
}
|
|
72
|
+
export function extractVarReferences(value) {
|
|
73
|
+
const refs = [];
|
|
74
|
+
for (const m of value.matchAll(VAR_REF_RE)) {
|
|
75
|
+
if (m[1] && !refs.includes(m[1])) {
|
|
76
|
+
refs.push(m[1]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return refs;
|
|
80
|
+
}
|
|
81
|
+
const CLASS_IN_SELECTOR_RE = /\.([a-zA-Z_][a-zA-Z0-9_-]*)/g;
|
|
82
|
+
const HEADING_SEGMENT_RE = /(?:^|[\s>+~])(h[1-6])(?:$|[.:#\s\[,+~>])/i;
|
|
83
|
+
const CSS_CONTAINER_AT_RULES = [
|
|
84
|
+
"@media",
|
|
85
|
+
"@supports",
|
|
86
|
+
"@layer",
|
|
87
|
+
"@container",
|
|
88
|
+
"@scope",
|
|
89
|
+
"@starting-style",
|
|
90
|
+
"@document",
|
|
91
|
+
"@mixin",
|
|
92
|
+
"@include",
|
|
93
|
+
"@if",
|
|
94
|
+
"@else",
|
|
95
|
+
"@each",
|
|
96
|
+
"@for",
|
|
97
|
+
"@while",
|
|
98
|
+
"@at-root",
|
|
99
|
+
];
|
|
100
|
+
const NON_LITERAL_PROPS = new Set(["content", "font-family", "src", "quotes", "counter-reset"]);
|
|
101
|
+
function resolveNestedSelector(header, parent) {
|
|
102
|
+
if (!parent)
|
|
103
|
+
return header;
|
|
104
|
+
return header
|
|
105
|
+
.split(",")
|
|
106
|
+
.map((part) => {
|
|
107
|
+
const p = part.trim();
|
|
108
|
+
return p.includes("&") ? p.replace(/&/g, parent) : `${parent} ${p}`;
|
|
109
|
+
})
|
|
110
|
+
.join(", ");
|
|
111
|
+
}
|
|
112
|
+
function isGlobalSelector(header, themeRes) {
|
|
113
|
+
for (const part of header.split(",")) {
|
|
114
|
+
let rest = part;
|
|
115
|
+
for (const re of themeRes) {
|
|
116
|
+
rest = rest.replace(new RegExp(re.source, "gi"), " ");
|
|
117
|
+
}
|
|
118
|
+
rest = rest
|
|
119
|
+
.replace(/:root|\bhtml\b|\bbody\b|\*|&/gi, " ")
|
|
120
|
+
.replace(/:(?:where|is)\(\s*\)/g, " ")
|
|
121
|
+
.replace(/[>+~\s]+/g, "");
|
|
122
|
+
if (rest !== "" && !/^(?:\[[^\]]+\])+$/.test(rest))
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
function inspectSelectorStructure(selector) {
|
|
128
|
+
const selectorParts = selector
|
|
129
|
+
.split(",")
|
|
130
|
+
.map((s) => s.trim())
|
|
131
|
+
.filter(Boolean);
|
|
132
|
+
let isMultiSegment = false;
|
|
133
|
+
const headingSet = new Set();
|
|
134
|
+
for (const part of selectorParts) {
|
|
135
|
+
// Strip :where(...) / :is(...) / attribute brackets before checking descendant combinators
|
|
136
|
+
const simplified = part.replace(/\[[^\]]+\]/g, "").trim();
|
|
137
|
+
if (/[\s>+~]+/.test(simplified)) {
|
|
138
|
+
isMultiSegment = true;
|
|
139
|
+
const hm = simplified.match(HEADING_SEGMENT_RE);
|
|
140
|
+
if (hm?.[1]) {
|
|
141
|
+
headingSet.add(hm[1].toLowerCase());
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
selectorParts,
|
|
147
|
+
isMultiSegment,
|
|
148
|
+
headingTagsTargeted: [...headingSet],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
export function parseCssStylesheet(filePath, rawText, options = {}) {
|
|
152
|
+
const darkRes = options.themes?.dark ?? compileRegexes(DEFAULT_DESIGN_SYSTEM_CONFIG.themes.dark);
|
|
153
|
+
const lightRes = options.themes?.light ?? compileRegexes(DEFAULT_DESIGN_SYSTEM_CONFIG.themes.light);
|
|
154
|
+
const themeRes = [...darkRes, ...lightRes];
|
|
155
|
+
const syntax = options.syntax ?? detectStylesheetSyntax(filePath);
|
|
156
|
+
const cleaned = stripStylesheetCommentsPreservingLines(rawText, syntax !== "css");
|
|
157
|
+
const lineStarts = buildLineStarts(rawText);
|
|
158
|
+
const rawLines = rawText.split(/\r?\n/);
|
|
159
|
+
const tokenDeclarations = [];
|
|
160
|
+
const varReferences = [];
|
|
161
|
+
const styleRuleTokenRefSet = new Set();
|
|
162
|
+
const styleRules = [];
|
|
163
|
+
const definedClassSet = new Set();
|
|
164
|
+
const literals = [];
|
|
165
|
+
const cleanPreprocFlags = (val) => val.replace(/\s*!(?:default|global|important)\s*$/i, "").trim();
|
|
166
|
+
const isPreprocTokenProp = (prop) => {
|
|
167
|
+
if (syntax === "scss" && /^\$[a-zA-Z_][a-zA-Z0-9_-]*$/.test(prop))
|
|
168
|
+
return true;
|
|
169
|
+
if (syntax === "less" &&
|
|
170
|
+
/^@[a-zA-Z_][a-zA-Z0-9_-]*$/.test(prop) &&
|
|
171
|
+
!LESS_AT_RULE_KEYWORDS.has(prop.slice(1).toLowerCase())) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
175
|
+
};
|
|
176
|
+
const parseBlocks = (textSlice, baseOffset, enclosingAtRule, parentSelector) => {
|
|
177
|
+
let i = 0;
|
|
178
|
+
const len = textSlice.length;
|
|
179
|
+
while (i < len) {
|
|
180
|
+
while (i < len && /\s/.test(textSlice[i]))
|
|
181
|
+
i++;
|
|
182
|
+
if (i >= len)
|
|
183
|
+
break;
|
|
184
|
+
const openBrace = textSlice.indexOf("{", i);
|
|
185
|
+
const semicolon = textSlice.indexOf(";", i);
|
|
186
|
+
if (semicolon !== -1 && (openBrace === -1 || semicolon < openBrace)) {
|
|
187
|
+
if (syntax !== "css") {
|
|
188
|
+
const stmtSlice = textSlice.slice(i, semicolon).trim();
|
|
189
|
+
const colonIdx = stmtSlice.indexOf(":");
|
|
190
|
+
if (colonIdx > 0) {
|
|
191
|
+
const prop = stmtSlice.slice(0, colonIdx).trim();
|
|
192
|
+
if (isPreprocTokenProp(prop)) {
|
|
193
|
+
const value = cleanPreprocFlags(stmtSlice.slice(colonIdx + 1).replace(/\s+/g, " ").trim());
|
|
194
|
+
const stmtLine = offsetToLineNumber(lineStarts, baseOffset + i);
|
|
195
|
+
const refs = [
|
|
196
|
+
...extractVarReferences(value),
|
|
197
|
+
...extractPreprocessorVarReferences(value, syntax),
|
|
198
|
+
];
|
|
199
|
+
const fallbackNames = extractVarFallbackNames(value);
|
|
200
|
+
for (const refName of refs) {
|
|
201
|
+
varReferences.push({
|
|
202
|
+
name: refName,
|
|
203
|
+
filePath,
|
|
204
|
+
lineNumber: stmtLine,
|
|
205
|
+
snippet: getLineSnippet(rawLines, stmtLine),
|
|
206
|
+
kind: "css-var",
|
|
207
|
+
...(fallbackNames.has(refName) ? { hasFallback: true } : {}),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
const isScopedTop = Boolean(options.defaultScoped && !parentSelector);
|
|
211
|
+
tokenDeclarations.push({
|
|
212
|
+
name: prop,
|
|
213
|
+
value,
|
|
214
|
+
tier: isScopedTop
|
|
215
|
+
? "component"
|
|
216
|
+
: classifyTokenTier(prop, false, options.tiers),
|
|
217
|
+
themeContext: "root",
|
|
218
|
+
selector: parentSelector ?? ":root",
|
|
219
|
+
filePath,
|
|
220
|
+
lineNumber: stmtLine,
|
|
221
|
+
referencedVars: refs,
|
|
222
|
+
scope: isScopedTop ? "scoped" : "global",
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
i = semicolon + 1;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (openBrace === -1)
|
|
231
|
+
break;
|
|
232
|
+
const rawHeader = textSlice.slice(i, openBrace).trim();
|
|
233
|
+
const headerOffset = baseOffset + i;
|
|
234
|
+
const headerLine = offsetToLineNumber(lineStarts, headerOffset);
|
|
235
|
+
let depth = 1;
|
|
236
|
+
let j = openBrace + 1;
|
|
237
|
+
while (j < len && depth > 0) {
|
|
238
|
+
if (textSlice[j] === "{")
|
|
239
|
+
depth++;
|
|
240
|
+
else if (textSlice[j] === "}")
|
|
241
|
+
depth--;
|
|
242
|
+
j++;
|
|
243
|
+
}
|
|
244
|
+
const bodyStartOffset = baseOffset + openBrace + 1;
|
|
245
|
+
const bodySlice = textSlice.slice(openBrace + 1, Math.max(openBrace + 1, j - 1));
|
|
246
|
+
if (CSS_CONTAINER_AT_RULES.some((at) => rawHeader.startsWith(at))) {
|
|
247
|
+
parseBlocks(bodySlice, bodyStartOffset, rawHeader, parentSelector);
|
|
248
|
+
i = j;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (rawHeader.startsWith("@keyframes") ||
|
|
252
|
+
rawHeader.startsWith("@font-face") ||
|
|
253
|
+
rawHeader.startsWith("@function")) {
|
|
254
|
+
i = j;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (rawHeader.startsWith("@property")) {
|
|
258
|
+
const propName = rawHeader.slice("@property".length).trim();
|
|
259
|
+
if (propName.startsWith("--")) {
|
|
260
|
+
const initial = bodySlice.match(/initial-value\s*:\s*([^;]+)/);
|
|
261
|
+
tokenDeclarations.push({
|
|
262
|
+
name: propName,
|
|
263
|
+
value: initial ? initial[1].replace(/\s+/g, " ").trim() : "",
|
|
264
|
+
tier: classifyTokenTier(propName, false, options.tiers),
|
|
265
|
+
themeContext: "root",
|
|
266
|
+
selector: rawHeader,
|
|
267
|
+
filePath,
|
|
268
|
+
lineNumber: headerLine,
|
|
269
|
+
referencedVars: [],
|
|
270
|
+
scope: "global",
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
i = j;
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const header = resolveNestedSelector(rawHeader, parentSelector);
|
|
277
|
+
const isThemeBridge = header.startsWith("@theme");
|
|
278
|
+
const hasRoot = /:root\b|(?:^|[\s,])html\b|:global\s*\(\s*:root\s*\)/i.test(header);
|
|
279
|
+
const isGlobal = isThemeBridge ||
|
|
280
|
+
hasRoot ||
|
|
281
|
+
(!options.defaultScoped && isGlobalSelector(header, themeRes));
|
|
282
|
+
const themeContexts = [];
|
|
283
|
+
let hasDark = false;
|
|
284
|
+
let hasLight = false;
|
|
285
|
+
if (isThemeBridge) {
|
|
286
|
+
themeContexts.push("theme-bridge");
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
for (const part of header.split(",")) {
|
|
290
|
+
const partScope = `${enclosingAtRule ?? ""} ${part}`;
|
|
291
|
+
const partDark = darkRes.some((re) => re.test(partScope));
|
|
292
|
+
const partLight = lightRes.some((re) => re.test(partScope));
|
|
293
|
+
hasDark ||= partDark;
|
|
294
|
+
hasLight ||= partLight;
|
|
295
|
+
if (partDark && !themeContexts.includes("dark"))
|
|
296
|
+
themeContexts.push("dark");
|
|
297
|
+
if (partLight && !themeContexts.includes("light"))
|
|
298
|
+
themeContexts.push("light");
|
|
299
|
+
if (!partDark &&
|
|
300
|
+
!partLight &&
|
|
301
|
+
/:root\b|(?:^|[\s,])html\b/i.test(part) &&
|
|
302
|
+
!themeContexts.includes("root")) {
|
|
303
|
+
themeContexts.push("root");
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const declarations = [];
|
|
308
|
+
const ruleTokensConsumed = [];
|
|
309
|
+
const nestedBlocks = [];
|
|
310
|
+
let declStart = 0;
|
|
311
|
+
let parenDepth = 0;
|
|
312
|
+
for (let k = 0; k <= bodySlice.length; k++) {
|
|
313
|
+
const ch = k < bodySlice.length ? bodySlice[k] : ";";
|
|
314
|
+
if (ch === "(")
|
|
315
|
+
parenDepth++;
|
|
316
|
+
else if (ch === ")" && parenDepth > 0)
|
|
317
|
+
parenDepth--;
|
|
318
|
+
else if (ch === "{" && parenDepth === 0) {
|
|
319
|
+
let nestDepth = 1;
|
|
320
|
+
let m = k + 1;
|
|
321
|
+
while (m < bodySlice.length && nestDepth > 0) {
|
|
322
|
+
if (bodySlice[m] === "{")
|
|
323
|
+
nestDepth++;
|
|
324
|
+
else if (bodySlice[m] === "}")
|
|
325
|
+
nestDepth--;
|
|
326
|
+
m++;
|
|
327
|
+
}
|
|
328
|
+
nestedBlocks.push({ start: declStart, end: m });
|
|
329
|
+
k = m - 1;
|
|
330
|
+
declStart = m;
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
else if (ch === ";" && parenDepth === 0) {
|
|
334
|
+
const rawDecl = bodySlice.slice(declStart, k);
|
|
335
|
+
const colonIdx = rawDecl.indexOf(":");
|
|
336
|
+
if (colonIdx > 0) {
|
|
337
|
+
const prop = rawDecl.slice(0, colonIdx).trim();
|
|
338
|
+
const value = rawDecl
|
|
339
|
+
.slice(colonIdx + 1)
|
|
340
|
+
.replace(/\s+/g, " ")
|
|
341
|
+
.trim();
|
|
342
|
+
const propOffset = bodyStartOffset + declStart + rawDecl.search(/\S/);
|
|
343
|
+
const declLine = offsetToLineNumber(lineStarts, propOffset);
|
|
344
|
+
const refs = [
|
|
345
|
+
...extractVarReferences(value),
|
|
346
|
+
...extractPreprocessorVarReferences(value, syntax),
|
|
347
|
+
];
|
|
348
|
+
const fallbackNames = extractVarFallbackNames(value);
|
|
349
|
+
for (const refName of refs) {
|
|
350
|
+
varReferences.push({
|
|
351
|
+
name: refName,
|
|
352
|
+
filePath,
|
|
353
|
+
lineNumber: declLine,
|
|
354
|
+
snippet: getLineSnippet(rawLines, declLine),
|
|
355
|
+
kind: "css-var",
|
|
356
|
+
...(fallbackNames.has(refName) ? { hasFallback: true } : {}),
|
|
357
|
+
});
|
|
358
|
+
if (!ruleTokensConsumed.includes(refName)) {
|
|
359
|
+
ruleTokensConsumed.push(refName);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (prop.startsWith("--") || isPreprocTokenProp(prop)) {
|
|
363
|
+
const cleanedTokenValue = cleanPreprocFlags(value);
|
|
364
|
+
const contextsToEmit = themeContexts.length > 0 ? themeContexts : ["root"];
|
|
365
|
+
for (const ctx of contextsToEmit) {
|
|
366
|
+
tokenDeclarations.push({
|
|
367
|
+
name: prop,
|
|
368
|
+
value: cleanedTokenValue,
|
|
369
|
+
tier: isGlobal
|
|
370
|
+
? classifyTokenTier(prop, ctx === "theme-bridge", options.tiers)
|
|
371
|
+
: "component",
|
|
372
|
+
themeContext: ctx,
|
|
373
|
+
selector: enclosingAtRule ? `${enclosingAtRule} ${header}` : header,
|
|
374
|
+
filePath,
|
|
375
|
+
lineNumber: declLine,
|
|
376
|
+
referencedVars: refs,
|
|
377
|
+
scope: isGlobal ? "global" : "scoped",
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
if (!isGlobal) {
|
|
381
|
+
for (const refName of refs)
|
|
382
|
+
styleRuleTokenRefSet.add(refName);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
for (const refName of refs)
|
|
387
|
+
styleRuleTokenRefSet.add(refName);
|
|
388
|
+
const propLower = prop.toLowerCase();
|
|
389
|
+
declarations.push({
|
|
390
|
+
prop: propLower,
|
|
391
|
+
value,
|
|
392
|
+
lineNumber: declLine,
|
|
393
|
+
});
|
|
394
|
+
if (!NON_LITERAL_PROPS.has(propLower)) {
|
|
395
|
+
const snippet = getLineSnippet(rawLines, declLine);
|
|
396
|
+
for (const c of extractColorLiterals(value)) {
|
|
397
|
+
literals.push({
|
|
398
|
+
kind: "color",
|
|
399
|
+
value: c,
|
|
400
|
+
surface: "css-rule",
|
|
401
|
+
context: propLower,
|
|
402
|
+
filePath,
|
|
403
|
+
lineNumber: declLine,
|
|
404
|
+
snippet,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
if (isLengthSensitiveProperty(propLower)) {
|
|
408
|
+
for (const l of extractLengthLiterals(value)) {
|
|
409
|
+
literals.push({
|
|
410
|
+
kind: "length",
|
|
411
|
+
value: l,
|
|
412
|
+
surface: "css-rule",
|
|
413
|
+
context: propLower,
|
|
414
|
+
filePath,
|
|
415
|
+
lineNumber: declLine,
|
|
416
|
+
snippet,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
declStart = k + 1;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (!isThemeBridge && !isGlobal) {
|
|
427
|
+
for (const m of header.matchAll(CLASS_IN_SELECTOR_RE)) {
|
|
428
|
+
if (m[1])
|
|
429
|
+
definedClassSet.add(m[1]);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (!isThemeBridge && !hasRoot && !hasDark && !hasLight) {
|
|
433
|
+
const classesInSelector = [];
|
|
434
|
+
for (const m of header.matchAll(CLASS_IN_SELECTOR_RE)) {
|
|
435
|
+
if (m[1] && !classesInSelector.includes(m[1])) {
|
|
436
|
+
classesInSelector.push(m[1]);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
const normalizedSelector = header.replace(/\s+/g, " ");
|
|
440
|
+
const { selectorParts, isMultiSegment, headingTagsTargeted } = inspectSelectorStructure(normalizedSelector);
|
|
441
|
+
styleRules.push({
|
|
442
|
+
selector: normalizedSelector,
|
|
443
|
+
filePath,
|
|
444
|
+
lineNumber: headerLine,
|
|
445
|
+
declarations,
|
|
446
|
+
classesInSelector,
|
|
447
|
+
tokensConsumed: ruleTokensConsumed,
|
|
448
|
+
selectorParts,
|
|
449
|
+
isMultiSegment,
|
|
450
|
+
headingTagsTargeted,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
for (const nb of nestedBlocks) {
|
|
454
|
+
parseBlocks(bodySlice.slice(nb.start, nb.end), bodyStartOffset + nb.start, enclosingAtRule, header);
|
|
455
|
+
}
|
|
456
|
+
i = j;
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
parseBlocks(cleaned, 0);
|
|
460
|
+
return {
|
|
461
|
+
filePath,
|
|
462
|
+
tokenDeclarations,
|
|
463
|
+
varReferences,
|
|
464
|
+
styleRuleTokenRefs: [...styleRuleTokenRefSet],
|
|
465
|
+
styleRules,
|
|
466
|
+
definedClasses: [...definedClassSet],
|
|
467
|
+
literals,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/merges.ts
|
|
3
|
+
*
|
|
4
|
+
* Persisted merge tracker for the `design-system` plugin catalog:
|
|
5
|
+
* records redundant components, color tokens, and typography tokens
|
|
6
|
+
* marked to merge into a canonical target, stored under
|
|
7
|
+
* `.chomp/plugins/design-system/merge-decisions.json`.
|
|
8
|
+
*/
|
|
9
|
+
export type DesignSystemMergeKind = "component" | "color" | "typography";
|
|
10
|
+
export interface DesignSystemMergeDecision {
|
|
11
|
+
/** Deterministic identifier `${kind}:${sourceId}->${targetId}`. */
|
|
12
|
+
id: string;
|
|
13
|
+
kind: DesignSystemMergeKind;
|
|
14
|
+
/** ID of the redundant component (`entityId`) or token (`--token-name`) that needs to be merged. */
|
|
15
|
+
sourceId: string;
|
|
16
|
+
sourceLabel: string;
|
|
17
|
+
sourceFilePath?: string;
|
|
18
|
+
sourceLineNumber?: number;
|
|
19
|
+
/** Canonical component `entityId` or token `--name` to merge INTO. */
|
|
20
|
+
targetId: string;
|
|
21
|
+
targetLabel: string;
|
|
22
|
+
targetFilePath?: string;
|
|
23
|
+
similarity?: number;
|
|
24
|
+
sharedSignals?: string[];
|
|
25
|
+
note?: string;
|
|
26
|
+
createdAt: string;
|
|
27
|
+
}
|
|
28
|
+
export interface DesignSystemMergeStore {
|
|
29
|
+
schemaVersion: 1;
|
|
30
|
+
updatedAt: string;
|
|
31
|
+
decisions: DesignSystemMergeDecision[];
|
|
32
|
+
}
|
|
33
|
+
export declare const DEFAULT_MERGE_DECISIONS_REL = ".chomp/plugins/design-system/merge-decisions.json";
|
|
34
|
+
export declare function resolveMergeDecisionsPath(repoPath: string): string;
|
|
35
|
+
export declare function buildMergeDecisionId(kind: DesignSystemMergeKind, sourceId: string, targetId: string): string;
|
|
36
|
+
export declare function readMergeDecisions(repoPath: string): DesignSystemMergeDecision[];
|
|
37
|
+
export interface UpsertMergeDecisionInput {
|
|
38
|
+
kind: DesignSystemMergeKind;
|
|
39
|
+
sourceId: string;
|
|
40
|
+
sourceLabel: string;
|
|
41
|
+
sourceFilePath?: string;
|
|
42
|
+
sourceLineNumber?: number;
|
|
43
|
+
targetId: string;
|
|
44
|
+
targetLabel: string;
|
|
45
|
+
targetFilePath?: string;
|
|
46
|
+
similarity?: number;
|
|
47
|
+
sharedSignals?: string[];
|
|
48
|
+
note?: string;
|
|
49
|
+
}
|
|
50
|
+
export declare function upsertMergeDecision(repoPath: string, input: UpsertMergeDecisionInput): {
|
|
51
|
+
decision: DesignSystemMergeDecision;
|
|
52
|
+
decisions: DesignSystemMergeDecision[];
|
|
53
|
+
};
|
|
54
|
+
export declare function removeMergeDecision(repoPath: string, idOrSourceId: string, kind?: DesignSystemMergeKind): {
|
|
55
|
+
removed: boolean;
|
|
56
|
+
decisions: DesignSystemMergeDecision[];
|
|
57
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/merges.ts
|
|
3
|
+
*
|
|
4
|
+
* Persisted merge tracker for the `design-system` plugin catalog:
|
|
5
|
+
* records redundant components, color tokens, and typography tokens
|
|
6
|
+
* marked to merge into a canonical target, stored under
|
|
7
|
+
* `.chomp/plugins/design-system/merge-decisions.json`.
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { dirname, resolve } from "node:path";
|
|
11
|
+
export const DEFAULT_MERGE_DECISIONS_REL = ".chomp/plugins/design-system/merge-decisions.json";
|
|
12
|
+
export function resolveMergeDecisionsPath(repoPath) {
|
|
13
|
+
return resolve(repoPath, DEFAULT_MERGE_DECISIONS_REL);
|
|
14
|
+
}
|
|
15
|
+
export function buildMergeDecisionId(kind, sourceId, targetId) {
|
|
16
|
+
return `${kind}:${sourceId}->${targetId}`;
|
|
17
|
+
}
|
|
18
|
+
export function readMergeDecisions(repoPath) {
|
|
19
|
+
const filePath = resolveMergeDecisionsPath(repoPath);
|
|
20
|
+
if (!existsSync(filePath))
|
|
21
|
+
return [];
|
|
22
|
+
try {
|
|
23
|
+
const raw = readFileSync(filePath, "utf8");
|
|
24
|
+
const parsed = JSON.parse(raw);
|
|
25
|
+
const list = Array.isArray(parsed)
|
|
26
|
+
? parsed
|
|
27
|
+
: Array.isArray(parsed?.decisions)
|
|
28
|
+
? parsed.decisions
|
|
29
|
+
: [];
|
|
30
|
+
return list.filter((d) => Boolean(d &&
|
|
31
|
+
typeof d === "object" &&
|
|
32
|
+
typeof d.id === "string" &&
|
|
33
|
+
(d.kind === "component" || d.kind === "color" || d.kind === "typography") &&
|
|
34
|
+
typeof d.sourceId === "string" &&
|
|
35
|
+
typeof d.targetId === "string"));
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function writeMergeDecisions(repoPath, decisions) {
|
|
42
|
+
const filePath = resolveMergeDecisionsPath(repoPath);
|
|
43
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
44
|
+
const store = {
|
|
45
|
+
schemaVersion: 1,
|
|
46
|
+
updatedAt: new Date().toISOString(),
|
|
47
|
+
decisions,
|
|
48
|
+
};
|
|
49
|
+
writeFileSync(filePath, `${JSON.stringify(store, null, 2)}\n`, "utf8");
|
|
50
|
+
}
|
|
51
|
+
export function upsertMergeDecision(repoPath, input) {
|
|
52
|
+
const existing = readMergeDecisions(repoPath);
|
|
53
|
+
const id = buildMergeDecisionId(input.kind, input.sourceId, input.targetId);
|
|
54
|
+
const decision = {
|
|
55
|
+
id,
|
|
56
|
+
kind: input.kind,
|
|
57
|
+
sourceId: input.sourceId,
|
|
58
|
+
sourceLabel: input.sourceLabel,
|
|
59
|
+
...(input.sourceFilePath ? { sourceFilePath: input.sourceFilePath } : {}),
|
|
60
|
+
...(input.sourceLineNumber !== undefined ? { sourceLineNumber: input.sourceLineNumber } : {}),
|
|
61
|
+
targetId: input.targetId,
|
|
62
|
+
targetLabel: input.targetLabel,
|
|
63
|
+
...(input.targetFilePath ? { targetFilePath: input.targetFilePath } : {}),
|
|
64
|
+
...(typeof input.similarity === "number" ? { similarity: input.similarity } : {}),
|
|
65
|
+
...(input.sharedSignals && input.sharedSignals.length > 0
|
|
66
|
+
? { sharedSignals: input.sharedSignals }
|
|
67
|
+
: {}),
|
|
68
|
+
...(input.note ? { note: input.note } : {}),
|
|
69
|
+
createdAt: new Date().toISOString(),
|
|
70
|
+
};
|
|
71
|
+
// One active merge target per `(kind, sourceId)` so changing merge target replaces cleanly.
|
|
72
|
+
const next = existing.filter((d) => !(d.kind === input.kind && d.sourceId === input.sourceId) && d.id !== id);
|
|
73
|
+
next.push(decision);
|
|
74
|
+
next.sort((a, b) => a.kind.localeCompare(b.kind) ||
|
|
75
|
+
a.sourceLabel.localeCompare(b.sourceLabel) ||
|
|
76
|
+
a.targetLabel.localeCompare(b.targetLabel));
|
|
77
|
+
writeMergeDecisions(repoPath, next);
|
|
78
|
+
return { decision, decisions: next };
|
|
79
|
+
}
|
|
80
|
+
export function removeMergeDecision(repoPath, idOrSourceId, kind) {
|
|
81
|
+
const existing = readMergeDecisions(repoPath);
|
|
82
|
+
const next = existing.filter((d) => {
|
|
83
|
+
if (d.id === idOrSourceId)
|
|
84
|
+
return false;
|
|
85
|
+
if (d.sourceId === idOrSourceId && (!kind || d.kind === kind))
|
|
86
|
+
return false;
|
|
87
|
+
return true;
|
|
88
|
+
});
|
|
89
|
+
const removed = next.length !== existing.length;
|
|
90
|
+
if (removed) {
|
|
91
|
+
writeMergeDecisions(repoPath, next);
|
|
92
|
+
}
|
|
93
|
+
return { removed, decisions: next };
|
|
94
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/preprocessors.ts
|
|
3
|
+
*
|
|
4
|
+
* Line-preserving preprocessors for non-TSX/CSS frontend formats:
|
|
5
|
+
* - Stylesheets: `.scss`, `.less` (single-line `//` comments outside `url(...)`/strings, `$var` / `@var` tokens)
|
|
6
|
+
* - Single-File & Content Components: `.vue`, `.svelte`, `.astro`, `.mdx`
|
|
7
|
+
* - Extracts embedded `<style>` blocks with exact 1-based line padding for `parseCssStylesheet`
|
|
8
|
+
* - Transforms `<script>` + `<template>` / Astro frontmatter+markup / MDX JSX into valid,
|
|
9
|
+
* line-preserving TSX so `scanTsxOrTsFile` inventories DOM primitives, Tailwind utilities,
|
|
10
|
+
* inline style literals, prop contracts, JSX skeletons, and component call sites accurately.
|
|
11
|
+
*/
|
|
12
|
+
export type StylesheetSyntax = "css" | "scss" | "less";
|
|
13
|
+
export interface ExtractedStyleBlock {
|
|
14
|
+
/** Virtual file path (keeps host path for evidence traceability). */
|
|
15
|
+
filePath: string;
|
|
16
|
+
/** Stylesheet source padded with leading `\n`s so 1-based line numbers match the host file. */
|
|
17
|
+
paddedCssText: string;
|
|
18
|
+
syntax: StylesheetSyntax;
|
|
19
|
+
/** True when `<style scoped>` (Vue) or default component-scoped `<style>` (Svelte/Astro without global). */
|
|
20
|
+
defaultScoped: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare const LESS_AT_RULE_KEYWORDS: ReadonlySet<string>;
|
|
23
|
+
export declare function detectStylesheetSyntax(filePath: string): StylesheetSyntax;
|
|
24
|
+
export declare function isSupportedStylesheetFile(filePath: string): boolean;
|
|
25
|
+
export declare function isPreprocessedUiComponentFile(filePath: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Strips `/* ... * /` block comments and (when `allowLineComments` is true for `.scss`/`.less`)
|
|
28
|
+
* `// ...` single-line comments outside string literals and `(...)` (e.g. `url(https://...)`),
|
|
29
|
+
* replacing stripped characters with spaces while preserving every `\n`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function stripStylesheetCommentsPreservingLines(rawText: string, allowLineComments?: boolean): string;
|
|
32
|
+
/**
|
|
33
|
+
* Extracts `$var` (SCSS) or `@var` (Less) token references from a CSS declaration value.
|
|
34
|
+
*/
|
|
35
|
+
export declare function extractPreprocessorVarReferences(value: string, syntax: StylesheetSyntax): string[];
|
|
36
|
+
/**
|
|
37
|
+
* Extracts `<style>` blocks from `.vue`, `.svelte`, and `.astro` files, padding leading lines
|
|
38
|
+
* with `\n` so 1-based line numbers reported by `parseCssStylesheet` match the original file.
|
|
39
|
+
*/
|
|
40
|
+
export declare function extractEmbeddedStyleBlocks(filePath: string, rawText: string): ExtractedStyleBlock[];
|
|
41
|
+
export declare function deriveComponentNameFromPath(filePath: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Preprocesses `.vue`, `.svelte`, `.astro`, and `.mdx` source text into valid,
|
|
44
|
+
* line-preserving TSX source text for `scanTsxOrTsFile`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function preprocessUiComponentToTsx(filePath: string, rawText: string): string;
|