@mandujs/core 0.51.0 → 0.53.0
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/package.json +1 -1
- package/src/design/__tests__/agents-link.test.ts +109 -0
- package/src/design/__tests__/extract-patch-diff.test.ts +265 -0
- package/src/design/__tests__/lint.test.ts +110 -0
- package/src/design/agents-link.ts +165 -0
- package/src/design/diff.ts +138 -0
- package/src/design/extract.ts +284 -0
- package/src/design/index.ts +39 -0
- package/src/design/lint.ts +209 -0
- package/src/design/patch.ts +242 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DESIGN.md upstream diff (Issue #245 M4 §3.5 external loop).
|
|
3
|
+
*
|
|
4
|
+
* Compare a local DESIGN.md against an upstream source (typically a
|
|
5
|
+
* brand spec from awesome-design-md). The diff is computed at the
|
|
6
|
+
* **structured-token level** — added / changed / removed entries per
|
|
7
|
+
* section — so agents can patch only the sections the user wants to
|
|
8
|
+
* sync without touching free-form prose.
|
|
9
|
+
*
|
|
10
|
+
* The diff is intentionally narrow: it ignores prose changes inside
|
|
11
|
+
* `rawBody`, comment shifts, and heading wording differences. It
|
|
12
|
+
* answers one question — "did the upstream catalog gain, change, or
|
|
13
|
+
* drop a token compared to my local file?".
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { ColorToken, DesignSpec, ShadowToken, SpacingToken, TypographyToken } from "./types";
|
|
17
|
+
|
|
18
|
+
export interface DiffEntryAddedRemoved<T> {
|
|
19
|
+
kind: "added" | "removed";
|
|
20
|
+
name: string;
|
|
21
|
+
token: T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DiffEntryChanged<T> {
|
|
25
|
+
kind: "changed";
|
|
26
|
+
name: string;
|
|
27
|
+
before: T;
|
|
28
|
+
after: T;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type DiffEntry<T> =
|
|
32
|
+
| DiffEntryAddedRemoved<T>
|
|
33
|
+
| DiffEntryChanged<T>;
|
|
34
|
+
|
|
35
|
+
export interface DesignSpecDiff {
|
|
36
|
+
/** Per-section diff arrays (only structured sections). */
|
|
37
|
+
colorPalette: DiffEntry<ColorToken>[];
|
|
38
|
+
typography: DiffEntry<TypographyToken>[];
|
|
39
|
+
layout: DiffEntry<SpacingToken>[];
|
|
40
|
+
shadows: DiffEntry<ShadowToken>[];
|
|
41
|
+
/** Total count of differences across all sections — convenience. */
|
|
42
|
+
totalChanges: number;
|
|
43
|
+
/** Sections whose `present` flag flipped between local and upstream. */
|
|
44
|
+
sectionPresenceChanged: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Compute the diff. Pure — accepts already-parsed specs so callers
|
|
49
|
+
* own fetching / caching of the upstream source.
|
|
50
|
+
*/
|
|
51
|
+
export function diffDesignSpecs(local: DesignSpec, upstream: DesignSpec): DesignSpecDiff {
|
|
52
|
+
const colorPalette = diffByName(
|
|
53
|
+
local.sections["color-palette"].tokens,
|
|
54
|
+
upstream.sections["color-palette"].tokens,
|
|
55
|
+
(a, b) => a.value === b.value && a.role === b.role,
|
|
56
|
+
);
|
|
57
|
+
const typography = diffByName(
|
|
58
|
+
local.sections.typography.tokens,
|
|
59
|
+
upstream.sections.typography.tokens,
|
|
60
|
+
(a, b) =>
|
|
61
|
+
a.fontFamily === b.fontFamily &&
|
|
62
|
+
a.size === b.size &&
|
|
63
|
+
a.weight === b.weight &&
|
|
64
|
+
a.lineHeight === b.lineHeight,
|
|
65
|
+
);
|
|
66
|
+
const layout = diffByName(
|
|
67
|
+
local.sections.layout.tokens,
|
|
68
|
+
upstream.sections.layout.tokens,
|
|
69
|
+
(a, b) => a.value === b.value,
|
|
70
|
+
);
|
|
71
|
+
const shadows = diffByName(
|
|
72
|
+
local.sections.shadows.tokens,
|
|
73
|
+
upstream.sections.shadows.tokens,
|
|
74
|
+
(a, b) => a.value === b.value,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const sectionPresenceChanged: string[] = [];
|
|
78
|
+
for (const id of [
|
|
79
|
+
"theme",
|
|
80
|
+
"color-palette",
|
|
81
|
+
"typography",
|
|
82
|
+
"components",
|
|
83
|
+
"layout",
|
|
84
|
+
"shadows",
|
|
85
|
+
"dos-donts",
|
|
86
|
+
"responsive",
|
|
87
|
+
"agent-prompts",
|
|
88
|
+
] as const) {
|
|
89
|
+
if (local.sections[id].present !== upstream.sections[id].present) {
|
|
90
|
+
sectionPresenceChanged.push(id);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const totalChanges = colorPalette.length + typography.length + layout.length + shadows.length;
|
|
95
|
+
return { colorPalette, typography, layout, shadows, totalChanges, sectionPresenceChanged };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface NamedToken {
|
|
99
|
+
name: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function diffByName<T extends NamedToken>(
|
|
103
|
+
local: readonly T[],
|
|
104
|
+
upstream: readonly T[],
|
|
105
|
+
same: (a: T, b: T) => boolean,
|
|
106
|
+
): DiffEntry<T>[] {
|
|
107
|
+
const localByKey = new Map<string, T>();
|
|
108
|
+
for (const t of local) localByKey.set(slug(t.name), t);
|
|
109
|
+
const upstreamByKey = new Map<string, T>();
|
|
110
|
+
for (const t of upstream) upstreamByKey.set(slug(t.name), t);
|
|
111
|
+
|
|
112
|
+
const entries: DiffEntry<T>[] = [];
|
|
113
|
+
for (const [key, upstreamToken] of upstreamByKey) {
|
|
114
|
+
const localToken = localByKey.get(key);
|
|
115
|
+
if (!localToken) {
|
|
116
|
+
entries.push({ kind: "added", name: upstreamToken.name, token: upstreamToken });
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (!same(localToken, upstreamToken)) {
|
|
120
|
+
entries.push({ kind: "changed", name: upstreamToken.name, before: localToken, after: upstreamToken });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
for (const [key, localToken] of localByKey) {
|
|
124
|
+
if (!upstreamByKey.has(key)) {
|
|
125
|
+
entries.push({ kind: "removed", name: localToken.name, token: localToken });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return entries;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function slug(name: string): string {
|
|
132
|
+
return name
|
|
133
|
+
.normalize("NFKD")
|
|
134
|
+
.replace(/[^\w\s-]/g, "")
|
|
135
|
+
.trim()
|
|
136
|
+
.replace(/\s+/g, "-")
|
|
137
|
+
.toLowerCase();
|
|
138
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DESIGN.md token extractor — scan project source for tokens that
|
|
3
|
+
* should be promoted into DESIGN.md (Issue #245 M4 §3.5 internal loop).
|
|
4
|
+
*
|
|
5
|
+
* Scope:
|
|
6
|
+
* - **color**: `#rgb`, `#rrggbb`, `#rrggbbaa`, `rgb(...)`, `rgba(...)`,
|
|
7
|
+
* `hsl(...)`, `oklch(...)` literals in TS/TSX/JSX/CSS/MDX sources.
|
|
8
|
+
* - **font-family**: `font-family: "X", sans-serif` declarations and
|
|
9
|
+
* Tailwind v4-style `--font-<slug>: ...;` tokens that don't yet
|
|
10
|
+
* appear in DESIGN.md typography.
|
|
11
|
+
* - **component**: identifier-style className combos that recur 3+
|
|
12
|
+
* times across files, suggesting a missing extraction.
|
|
13
|
+
*
|
|
14
|
+
* The extractor is **proposal-only**: it never edits anything.
|
|
15
|
+
* Callers (CLI / MCP `mandu.design.extract`) decide whether to flow
|
|
16
|
+
* the proposals into DESIGN.md via `patchDesignMd`. Confidence is a
|
|
17
|
+
* coarse 0..1 score driven by occurrence count; agents can filter on
|
|
18
|
+
* it before showing the user.
|
|
19
|
+
*
|
|
20
|
+
* Performance: the walker bounds itself to the conventional source
|
|
21
|
+
* roots and skips dotfile dirs / node_modules. Large monorepos can
|
|
22
|
+
* narrow further with the `scope` option.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import path from "node:path";
|
|
26
|
+
import { promises as fs } from "node:fs";
|
|
27
|
+
import type { DesignSpec } from "./types";
|
|
28
|
+
|
|
29
|
+
// ─── Public surface ───────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
export type ExtractKind = "color" | "typography" | "spacing" | "component";
|
|
32
|
+
|
|
33
|
+
export interface ExtractProposal {
|
|
34
|
+
kind: ExtractKind;
|
|
35
|
+
/** DESIGN.md section the proposal would land in. */
|
|
36
|
+
section: "color-palette" | "typography" | "layout" | "components";
|
|
37
|
+
/** Stable key to compare against existing tokens (slug or literal). */
|
|
38
|
+
key: string;
|
|
39
|
+
/** Suggested DESIGN.md value (`#FF8C42`, `Inter sans-serif`, …). */
|
|
40
|
+
value: string;
|
|
41
|
+
/** Total occurrences across the scanned tree. */
|
|
42
|
+
occurrences: number;
|
|
43
|
+
/** Up to 5 distinct file paths the literal/pattern was found in. */
|
|
44
|
+
files: string[];
|
|
45
|
+
/** Coarse 0..1 confidence — higher = more occurrences. */
|
|
46
|
+
confidence: number;
|
|
47
|
+
/** Optional human note ("seen in ButtonPrimary, ButtonGhost"). */
|
|
48
|
+
note?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ExtractOptions {
|
|
52
|
+
/** Glob-rooted scopes (relative). Defaults to `["src", "app"]`. */
|
|
53
|
+
scope?: readonly string[];
|
|
54
|
+
/** Filter the kinds emitted. Defaults to all four. */
|
|
55
|
+
kinds?: readonly ExtractKind[];
|
|
56
|
+
/** Minimum occurrence threshold for color/font/component. Default 3. */
|
|
57
|
+
minOccurrences?: number;
|
|
58
|
+
/**
|
|
59
|
+
* When provided, proposals already represented by an existing
|
|
60
|
+
* DesignSpec token are dropped (so agents only see "new" candidates).
|
|
61
|
+
*/
|
|
62
|
+
existing?: DesignSpec;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ExtractResult {
|
|
66
|
+
proposals: ExtractProposal[];
|
|
67
|
+
/** Total source files scanned. */
|
|
68
|
+
scannedFiles: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Walk the project and collect proposals. Pure async — no caching, no
|
|
73
|
+
* side effects beyond reads.
|
|
74
|
+
*/
|
|
75
|
+
export async function extractDesignTokens(
|
|
76
|
+
rootDir: string,
|
|
77
|
+
options: ExtractOptions = {},
|
|
78
|
+
): Promise<ExtractResult> {
|
|
79
|
+
const scope = options.scope ?? ["src", "app"];
|
|
80
|
+
const kinds = new Set<ExtractKind>(options.kinds ?? ["color", "typography", "spacing", "component"]);
|
|
81
|
+
const minOccurrences = options.minOccurrences ?? 3;
|
|
82
|
+
|
|
83
|
+
const colorOccurrences = new Map<string, { files: Set<string>; count: number }>();
|
|
84
|
+
const fontFamilyOccurrences = new Map<string, { files: Set<string>; count: number }>();
|
|
85
|
+
const classnameComboOccurrences = new Map<string, { files: Set<string>; count: number }>();
|
|
86
|
+
|
|
87
|
+
let scannedFiles = 0;
|
|
88
|
+
for (const dir of scope) {
|
|
89
|
+
const root = path.join(rootDir, dir);
|
|
90
|
+
const files = await collectFiles(root);
|
|
91
|
+
scannedFiles += files.length;
|
|
92
|
+
for (const file of files) {
|
|
93
|
+
let content: string;
|
|
94
|
+
try {
|
|
95
|
+
content = await fs.readFile(file, "utf8");
|
|
96
|
+
} catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const rel = path.relative(rootDir, file).replace(/\\/g, "/");
|
|
100
|
+
if (kinds.has("color")) collectColors(content, rel, colorOccurrences);
|
|
101
|
+
if (kinds.has("typography")) collectFontFamilies(content, rel, fontFamilyOccurrences);
|
|
102
|
+
if (kinds.has("component")) collectClassNameCombos(content, rel, classnameComboOccurrences);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const proposals: ExtractProposal[] = [];
|
|
107
|
+
if (kinds.has("color")) {
|
|
108
|
+
proposals.push(
|
|
109
|
+
...buildProposals({
|
|
110
|
+
section: "color-palette",
|
|
111
|
+
kind: "color",
|
|
112
|
+
keyer: (literal) => literal,
|
|
113
|
+
valuer: (literal) => literal,
|
|
114
|
+
seed: colorOccurrences,
|
|
115
|
+
minOccurrences,
|
|
116
|
+
existingKeys: collectExistingColorValues(options.existing),
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
if (kinds.has("typography")) {
|
|
121
|
+
proposals.push(
|
|
122
|
+
...buildProposals({
|
|
123
|
+
section: "typography",
|
|
124
|
+
kind: "typography",
|
|
125
|
+
keyer: (literal) => literal,
|
|
126
|
+
valuer: (literal) => literal,
|
|
127
|
+
seed: fontFamilyOccurrences,
|
|
128
|
+
minOccurrences,
|
|
129
|
+
existingKeys: collectExistingFontFamilies(options.existing),
|
|
130
|
+
}),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (kinds.has("component")) {
|
|
134
|
+
proposals.push(
|
|
135
|
+
...buildProposals({
|
|
136
|
+
section: "components",
|
|
137
|
+
kind: "component",
|
|
138
|
+
keyer: (literal) => literal,
|
|
139
|
+
valuer: (literal) => literal,
|
|
140
|
+
seed: classnameComboOccurrences,
|
|
141
|
+
minOccurrences,
|
|
142
|
+
existingKeys: new Set(),
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
proposals.sort((a, b) => b.occurrences - a.occurrences);
|
|
148
|
+
return { proposals, scannedFiles };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ─── Walker ───────────────────────────────────────────────────────────
|
|
152
|
+
|
|
153
|
+
const SOURCE_RX = /\.(?:tsx?|jsx?|mdx?|css)$/;
|
|
154
|
+
|
|
155
|
+
async function collectFiles(root: string): Promise<string[]> {
|
|
156
|
+
const out: string[] = [];
|
|
157
|
+
async function walk(dir: string): Promise<void> {
|
|
158
|
+
let entries: import("node:fs").Dirent[];
|
|
159
|
+
try {
|
|
160
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
161
|
+
} catch {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
for (const entry of entries) {
|
|
165
|
+
const full = path.join(dir, entry.name);
|
|
166
|
+
if (entry.isDirectory()) {
|
|
167
|
+
if (entry.name === "node_modules" || entry.name.startsWith(".")) continue;
|
|
168
|
+
await walk(full);
|
|
169
|
+
} else if (entry.isFile() && SOURCE_RX.test(entry.name)) {
|
|
170
|
+
out.push(full);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
await walk(root);
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ─── Collectors ───────────────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
const COLOR_RX = /#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b|(?:rgba?|hsla?|oklch|hwb|lab|lch)\([^)]+\)/g;
|
|
181
|
+
|
|
182
|
+
function collectColors(
|
|
183
|
+
content: string,
|
|
184
|
+
rel: string,
|
|
185
|
+
bucket: Map<string, { files: Set<string>; count: number }>,
|
|
186
|
+
): void {
|
|
187
|
+
let m: RegExpExecArray | null;
|
|
188
|
+
while ((m = COLOR_RX.exec(content)) !== null) {
|
|
189
|
+
const literal = m[0]!.toLowerCase();
|
|
190
|
+
const entry = bucket.get(literal) ?? { files: new Set(), count: 0 };
|
|
191
|
+
entry.files.add(rel);
|
|
192
|
+
entry.count++;
|
|
193
|
+
bucket.set(literal, entry);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const FONT_FAMILY_RX = /font-family\s*:\s*([^;\n}]+)/gi;
|
|
198
|
+
|
|
199
|
+
function collectFontFamilies(
|
|
200
|
+
content: string,
|
|
201
|
+
rel: string,
|
|
202
|
+
bucket: Map<string, { files: Set<string>; count: number }>,
|
|
203
|
+
): void {
|
|
204
|
+
let m: RegExpExecArray | null;
|
|
205
|
+
while ((m = FONT_FAMILY_RX.exec(content)) !== null) {
|
|
206
|
+
const value = m[1]!.trim().replace(/[`'"]/g, "");
|
|
207
|
+
if (!value || value.length > 200) continue;
|
|
208
|
+
const entry = bucket.get(value) ?? { files: new Set(), count: 0 };
|
|
209
|
+
entry.files.add(rel);
|
|
210
|
+
entry.count++;
|
|
211
|
+
bucket.set(value, entry);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const CLASSNAME_RX = /className\s*=\s*["']([^"']{16,200})["']/g;
|
|
216
|
+
|
|
217
|
+
function collectClassNameCombos(
|
|
218
|
+
content: string,
|
|
219
|
+
rel: string,
|
|
220
|
+
bucket: Map<string, { files: Set<string>; count: number }>,
|
|
221
|
+
): void {
|
|
222
|
+
let m: RegExpExecArray | null;
|
|
223
|
+
while ((m = CLASSNAME_RX.exec(content)) !== null) {
|
|
224
|
+
const literal = m[1]!.trim().split(/\s+/).sort().join(" ");
|
|
225
|
+
if (!literal) continue;
|
|
226
|
+
// Skip combos that are mostly variant prefixes — they're rarely
|
|
227
|
+
// good extraction candidates.
|
|
228
|
+
const tokenCount = literal.split(/\s+/).length;
|
|
229
|
+
if (tokenCount < 3) continue;
|
|
230
|
+
const entry = bucket.get(literal) ?? { files: new Set(), count: 0 };
|
|
231
|
+
entry.files.add(rel);
|
|
232
|
+
entry.count++;
|
|
233
|
+
bucket.set(literal, entry);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ─── Proposal builder ────────────────────────────────────────────────
|
|
238
|
+
|
|
239
|
+
interface BuildProposalsArgs {
|
|
240
|
+
section: ExtractProposal["section"];
|
|
241
|
+
kind: ExtractKind;
|
|
242
|
+
keyer: (literal: string) => string;
|
|
243
|
+
valuer: (literal: string) => string;
|
|
244
|
+
seed: Map<string, { files: Set<string>; count: number }>;
|
|
245
|
+
minOccurrences: number;
|
|
246
|
+
existingKeys: Set<string>;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function buildProposals(args: BuildProposalsArgs): ExtractProposal[] {
|
|
250
|
+
const out: ExtractProposal[] = [];
|
|
251
|
+
for (const [literal, info] of args.seed) {
|
|
252
|
+
if (info.count < args.minOccurrences) continue;
|
|
253
|
+
const key = args.keyer(literal).toLowerCase();
|
|
254
|
+
if (args.existingKeys.has(key)) continue;
|
|
255
|
+
out.push({
|
|
256
|
+
kind: args.kind,
|
|
257
|
+
section: args.section,
|
|
258
|
+
key: literal,
|
|
259
|
+
value: args.valuer(literal),
|
|
260
|
+
occurrences: info.count,
|
|
261
|
+
files: [...info.files].slice(0, 5),
|
|
262
|
+
confidence: Math.min(1, info.count / 10),
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function collectExistingColorValues(spec: DesignSpec | undefined): Set<string> {
|
|
269
|
+
const out = new Set<string>();
|
|
270
|
+
if (!spec) return out;
|
|
271
|
+
for (const t of spec.sections["color-palette"].tokens) {
|
|
272
|
+
if (t.value) out.add(t.value.toLowerCase());
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function collectExistingFontFamilies(spec: DesignSpec | undefined): Set<string> {
|
|
278
|
+
const out = new Set<string>();
|
|
279
|
+
if (!spec) return out;
|
|
280
|
+
for (const t of spec.sections.typography.tokens) {
|
|
281
|
+
if (t.fontFamily) out.add(t.fontFamily.toLowerCase());
|
|
282
|
+
}
|
|
283
|
+
return out;
|
|
284
|
+
}
|
package/src/design/index.ts
CHANGED
|
@@ -34,6 +34,45 @@ export {
|
|
|
34
34
|
type ThemeMergeResult,
|
|
35
35
|
} from "./tailwind-theme";
|
|
36
36
|
|
|
37
|
+
export {
|
|
38
|
+
extractDesignTokens,
|
|
39
|
+
type ExtractKind,
|
|
40
|
+
type ExtractOptions,
|
|
41
|
+
type ExtractProposal,
|
|
42
|
+
type ExtractResult,
|
|
43
|
+
} from "./extract";
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
patchDesignMd,
|
|
47
|
+
patchDesignMdBatch,
|
|
48
|
+
type PatchableSection,
|
|
49
|
+
type PatchOperation,
|
|
50
|
+
type PatchResult,
|
|
51
|
+
type PatchBatchResult,
|
|
52
|
+
} from "./patch";
|
|
53
|
+
|
|
54
|
+
export {
|
|
55
|
+
diffDesignSpecs,
|
|
56
|
+
type DesignSpecDiff,
|
|
57
|
+
type DiffEntry,
|
|
58
|
+
} from "./diff";
|
|
59
|
+
|
|
60
|
+
export {
|
|
61
|
+
buildAgentsDesignBlock,
|
|
62
|
+
linkAgentsToDesignMd,
|
|
63
|
+
DESIGN_LINK_MARKER_START,
|
|
64
|
+
DESIGN_LINK_MARKER_END,
|
|
65
|
+
type LinkAgentsOptions,
|
|
66
|
+
type LinkAgentsResult,
|
|
67
|
+
} from "./agents-link";
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
lintDesignSpec,
|
|
71
|
+
type LintIssue,
|
|
72
|
+
type LintResult,
|
|
73
|
+
type LintSeverity,
|
|
74
|
+
} from "./lint";
|
|
75
|
+
|
|
37
76
|
export type {
|
|
38
77
|
AgentPrompt,
|
|
39
78
|
AgentPromptsSection,
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DESIGN.md self-consistency lint (Issue #245 M5).
|
|
3
|
+
*
|
|
4
|
+
* Different from `validateDesignSpec` (M1), which reports section
|
|
5
|
+
* presence / shape. The linter checks the *content* for issues that
|
|
6
|
+
* compile fine but waste reader time:
|
|
7
|
+
*
|
|
8
|
+
* - color-palette: hex values that aren't 3/6/8 digits, duplicate
|
|
9
|
+
* hex values across distinct names, slug collisions across names
|
|
10
|
+
* - typography: tokens missing both `fontFamily` and `size`,
|
|
11
|
+
* duplicate display-name slugs
|
|
12
|
+
* - layout / shadows: tokens with no value, duplicate slugs
|
|
13
|
+
* - components: duplicate H3 names
|
|
14
|
+
* - dos-donts: rules under §7 with no `do` / `don't` mode marker
|
|
15
|
+
*
|
|
16
|
+
* The linter is conservative — every rule is opt-out via severity
|
|
17
|
+
* filtering at the call site. The tool returns `{ ok, issues[] }`
|
|
18
|
+
* so CLIs can produce exit codes and MCP tools can stream the issue
|
|
19
|
+
* list.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { slugifyTokenName } from "./tailwind-theme";
|
|
23
|
+
import type { DesignSpec } from "./types";
|
|
24
|
+
|
|
25
|
+
export type LintSeverity = "error" | "warning" | "info";
|
|
26
|
+
|
|
27
|
+
export interface LintIssue {
|
|
28
|
+
rule: string;
|
|
29
|
+
section:
|
|
30
|
+
| "color-palette"
|
|
31
|
+
| "typography"
|
|
32
|
+
| "layout"
|
|
33
|
+
| "shadows"
|
|
34
|
+
| "components"
|
|
35
|
+
| "dos-donts"
|
|
36
|
+
| "agent-prompts"
|
|
37
|
+
| "responsive";
|
|
38
|
+
severity: LintSeverity;
|
|
39
|
+
message: string;
|
|
40
|
+
/** Token / row name when the issue is per-row. */
|
|
41
|
+
name?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface LintResult {
|
|
45
|
+
ok: boolean;
|
|
46
|
+
issues: LintIssue[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const HEX_RX = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
50
|
+
const COLOR_FN_RX = /^(rgba?|hsla?|oklch|hwb|lab|lch)\(/i;
|
|
51
|
+
|
|
52
|
+
export function lintDesignSpec(spec: DesignSpec): LintResult {
|
|
53
|
+
const issues: LintIssue[] = [];
|
|
54
|
+
|
|
55
|
+
// ─── color-palette ────────────────────────────────────────────────
|
|
56
|
+
const seenColorSlug = new Map<string, string>();
|
|
57
|
+
const seenColorValue = new Map<string, string>();
|
|
58
|
+
for (const token of spec.sections["color-palette"].tokens) {
|
|
59
|
+
if (!token.value) {
|
|
60
|
+
issues.push({
|
|
61
|
+
rule: "color-missing-value",
|
|
62
|
+
section: "color-palette",
|
|
63
|
+
severity: "warning",
|
|
64
|
+
message: `Color "${token.name}" has no parseable value — Tailwind sync will skip it.`,
|
|
65
|
+
name: token.name,
|
|
66
|
+
});
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (!HEX_RX.test(token.value) && !COLOR_FN_RX.test(token.value)) {
|
|
70
|
+
issues.push({
|
|
71
|
+
rule: "color-malformed-value",
|
|
72
|
+
section: "color-palette",
|
|
73
|
+
severity: "error",
|
|
74
|
+
message: `Color "${token.name}" value "${token.value}" is not a recognised hex/rgb/hsl/oklch literal.`,
|
|
75
|
+
name: token.name,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const sl = slugifyTokenName(token.name);
|
|
79
|
+
const prevByName = seenColorSlug.get(sl);
|
|
80
|
+
if (prevByName) {
|
|
81
|
+
issues.push({
|
|
82
|
+
rule: "color-slug-collision",
|
|
83
|
+
section: "color-palette",
|
|
84
|
+
severity: "warning",
|
|
85
|
+
message: `Color "${token.name}" slugifies to the same id as "${prevByName}" — Tailwind sync keeps only the first.`,
|
|
86
|
+
name: token.name,
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
seenColorSlug.set(sl, token.name);
|
|
90
|
+
}
|
|
91
|
+
const prevByValue = seenColorValue.get(token.value.toLowerCase());
|
|
92
|
+
if (prevByValue && prevByValue !== token.name) {
|
|
93
|
+
issues.push({
|
|
94
|
+
rule: "color-duplicate-value",
|
|
95
|
+
section: "color-palette",
|
|
96
|
+
severity: "info",
|
|
97
|
+
message: `Color "${token.name}" shares value ${token.value} with "${prevByValue}" — consider a single canonical name.`,
|
|
98
|
+
name: token.name,
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
seenColorValue.set(token.value.toLowerCase(), token.name);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ─── typography ──────────────────────────────────────────────────
|
|
106
|
+
const seenTypoSlug = new Map<string, string>();
|
|
107
|
+
for (const token of spec.sections.typography.tokens) {
|
|
108
|
+
if (!token.fontFamily && !token.size) {
|
|
109
|
+
issues.push({
|
|
110
|
+
rule: "typography-empty-token",
|
|
111
|
+
section: "typography",
|
|
112
|
+
severity: "warning",
|
|
113
|
+
message: `Typography token "${token.name}" has neither fontFamily nor size — nothing to sync.`,
|
|
114
|
+
name: token.name,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const sl = slugifyTokenName(token.name);
|
|
118
|
+
const prev = seenTypoSlug.get(sl);
|
|
119
|
+
if (prev) {
|
|
120
|
+
issues.push({
|
|
121
|
+
rule: "typography-slug-collision",
|
|
122
|
+
section: "typography",
|
|
123
|
+
severity: "warning",
|
|
124
|
+
message: `Typography "${token.name}" collides with "${prev}" on slug "${sl}".`,
|
|
125
|
+
name: token.name,
|
|
126
|
+
});
|
|
127
|
+
} else {
|
|
128
|
+
seenTypoSlug.set(sl, token.name);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ─── layout (spacing) ────────────────────────────────────────────
|
|
133
|
+
const seenLayoutSlug = new Map<string, string>();
|
|
134
|
+
for (const token of spec.sections.layout.tokens) {
|
|
135
|
+
if (!token.value) {
|
|
136
|
+
issues.push({
|
|
137
|
+
rule: "spacing-missing-value",
|
|
138
|
+
section: "layout",
|
|
139
|
+
severity: "warning",
|
|
140
|
+
message: `Spacing "${token.name}" has no value.`,
|
|
141
|
+
name: token.name,
|
|
142
|
+
});
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const sl = slugifyTokenName(token.name);
|
|
146
|
+
const prev = seenLayoutSlug.get(sl);
|
|
147
|
+
if (prev) {
|
|
148
|
+
issues.push({
|
|
149
|
+
rule: "spacing-slug-collision",
|
|
150
|
+
section: "layout",
|
|
151
|
+
severity: "warning",
|
|
152
|
+
message: `Spacing "${token.name}" collides with "${prev}" on slug "${sl}".`,
|
|
153
|
+
name: token.name,
|
|
154
|
+
});
|
|
155
|
+
} else {
|
|
156
|
+
seenLayoutSlug.set(sl, token.name);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ─── shadows ─────────────────────────────────────────────────────
|
|
161
|
+
const seenShadowSlug = new Map<string, string>();
|
|
162
|
+
for (const token of spec.sections.shadows.tokens) {
|
|
163
|
+
if (!token.value) {
|
|
164
|
+
issues.push({
|
|
165
|
+
rule: "shadow-missing-value",
|
|
166
|
+
section: "shadows",
|
|
167
|
+
severity: "warning",
|
|
168
|
+
message: `Shadow "${token.name}" has no value.`,
|
|
169
|
+
name: token.name,
|
|
170
|
+
});
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const sl = slugifyTokenName(token.name);
|
|
174
|
+
const prev = seenShadowSlug.get(sl);
|
|
175
|
+
if (prev) {
|
|
176
|
+
issues.push({
|
|
177
|
+
rule: "shadow-slug-collision",
|
|
178
|
+
section: "shadows",
|
|
179
|
+
severity: "warning",
|
|
180
|
+
message: `Shadow "${token.name}" collides with "${prev}" on slug "${sl}".`,
|
|
181
|
+
name: token.name,
|
|
182
|
+
});
|
|
183
|
+
} else {
|
|
184
|
+
seenShadowSlug.set(sl, token.name);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ─── components ──────────────────────────────────────────────────
|
|
189
|
+
const seenComponentSlug = new Map<string, string>();
|
|
190
|
+
for (const token of spec.sections.components.tokens) {
|
|
191
|
+
const sl = slugifyTokenName(token.name);
|
|
192
|
+
const prev = seenComponentSlug.get(sl);
|
|
193
|
+
if (prev) {
|
|
194
|
+
issues.push({
|
|
195
|
+
rule: "component-duplicate",
|
|
196
|
+
section: "components",
|
|
197
|
+
severity: "warning",
|
|
198
|
+
message: `Component "${token.name}" appears twice (also as "${prev}").`,
|
|
199
|
+
name: token.name,
|
|
200
|
+
});
|
|
201
|
+
} else {
|
|
202
|
+
seenComponentSlug.set(sl, token.name);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// The most critical level of severity wins for the `ok` flag.
|
|
207
|
+
const ok = !issues.some((i) => i.severity === "error");
|
|
208
|
+
return { ok, issues };
|
|
209
|
+
}
|