@luci-dev/format 0.7.0 → 0.8.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.
Files changed (61) hide show
  1. package/dist/capture/check.d.ts +44 -2
  2. package/dist/capture/check.d.ts.map +1 -1
  3. package/dist/capture/check.js +381 -23
  4. package/dist/capture/check.js.map +1 -1
  5. package/dist/capture/collection.d.ts +240 -6
  6. package/dist/capture/collection.d.ts.map +1 -1
  7. package/dist/capture/format.d.ts +1 -1
  8. package/dist/capture/format.d.ts.map +1 -1
  9. package/dist/capture/format.js +31 -14
  10. package/dist/capture/format.js.map +1 -1
  11. package/dist/capture/layout.js +1 -1
  12. package/dist/capture/layout.js.map +1 -1
  13. package/dist/capture/merge.d.ts +7 -0
  14. package/dist/capture/merge.d.ts.map +1 -1
  15. package/dist/capture/merge.js +24 -0
  16. package/dist/capture/merge.js.map +1 -1
  17. package/dist/capture/read.d.ts +16 -1
  18. package/dist/capture/read.d.ts.map +1 -1
  19. package/dist/capture/read.js +82 -9
  20. package/dist/capture/read.js.map +1 -1
  21. package/dist/capture/schema.d.ts +256 -8
  22. package/dist/capture/schema.d.ts.map +1 -1
  23. package/dist/capture/schema.js +21 -8
  24. package/dist/capture/schema.js.map +1 -1
  25. package/dist/capture/spec.d.ts +35 -5
  26. package/dist/capture/spec.d.ts.map +1 -1
  27. package/dist/capture/spec.js +71 -16
  28. package/dist/capture/spec.js.map +1 -1
  29. package/dist/capture/values.d.ts +11 -0
  30. package/dist/capture/values.d.ts.map +1 -1
  31. package/dist/capture/values.js +58 -1
  32. package/dist/capture/values.js.map +1 -1
  33. package/dist/feedback/prompt.js +1 -1
  34. package/dist/feedback/prompt.js.map +1 -1
  35. package/dist/feedback/review.js +1 -1
  36. package/dist/feedback/review.js.map +1 -1
  37. package/dist/figma/diff.d.ts +13 -0
  38. package/dist/figma/diff.d.ts.map +1 -0
  39. package/dist/figma/diff.js +454 -0
  40. package/dist/figma/diff.js.map +1 -0
  41. package/dist/figma/index.d.ts +10 -0
  42. package/dist/figma/index.d.ts.map +1 -0
  43. package/dist/figma/index.js +10 -0
  44. package/dist/figma/index.js.map +1 -0
  45. package/dist/figma/match.d.ts +56 -0
  46. package/dist/figma/match.d.ts.map +1 -0
  47. package/dist/figma/match.js +329 -0
  48. package/dist/figma/match.js.map +1 -0
  49. package/dist/figma/normalize.d.ts +62 -0
  50. package/dist/figma/normalize.d.ts.map +1 -0
  51. package/dist/figma/normalize.js +932 -0
  52. package/dist/figma/normalize.js.map +1 -0
  53. package/dist/figma/schema.d.ts +1662 -0
  54. package/dist/figma/schema.d.ts.map +1 -0
  55. package/dist/figma/schema.js +545 -0
  56. package/dist/figma/schema.js.map +1 -0
  57. package/dist/figma/tokens.d.ts +32 -0
  58. package/dist/figma/tokens.d.ts.map +1 -0
  59. package/dist/figma/tokens.js +391 -0
  60. package/dist/figma/tokens.js.map +1 -0
  61. package/package.json +5 -1
@@ -6,7 +6,7 @@
6
6
  * is one sentence a person can pin on the element as a feedback note. Pure and browser-safe.
7
7
  */
8
8
  import { z } from 'zod';
9
- import type { ComponentSpec, ComponentStateName } from './read.js';
9
+ import type { ComponentSpec, SpecVariant, ComponentStateName } from './read.js';
10
10
  export declare const CHECK_KINDS: readonly ['style', 'token', 'part', 'state', 'motion', 'accessibility'];
11
11
  export type CheckKind = (typeof CHECK_KINDS)[number];
12
12
  export interface CheckDifference {
@@ -33,6 +33,8 @@ export interface CheckResult {
33
33
  type: string;
34
34
  source: string;
35
35
  };
36
+ /** Variant row label from `## Variants` that this component was checked against, if any. */
37
+ variant?: string;
36
38
  /** Every value compared that matched ("container background-color", "hover: container background-color"). */
37
39
  matches: string[];
38
40
  differences: CheckDifference[];
@@ -49,9 +51,49 @@ export interface CheckOptions {
49
51
  knownTokens?: readonly string[];
50
52
  /** When true, accessibility levels are compared strictly symmetrically (for compareComponentFiles). */
51
53
  symmetricAccessibility?: boolean;
54
+ /** Bound Figma layer `{ fileKey, nodeId }` to match its exact `## Variants` row when checking one instance. */
55
+ variantLayer?: {
56
+ fileKey?: string;
57
+ nodeId: string;
58
+ };
59
+ /** Built non-container part names whose `text-align` came from inheritance rather than a declaration on that element. */
60
+ inheritedTextAlignParts?: ReadonlySet<string> | readonly string[];
52
61
  }
53
- /** Do two written values mean the same? Font families compare by their first font. */
62
+ /** Do two written values mean the same? Font families compare by their first font; alignment flow-* matches flex-*. */
54
63
  export declare function sameValue(property: string, a: string, b: string, options?: CheckOptions): boolean;
64
+ export type PartKind = 'container' | 'text' | 'icon' | 'image' | 'field' | 'action' | 'link' | 'item' | 'other';
65
+ export interface PartLike {
66
+ part: string;
67
+ element: string;
68
+ styles: readonly {
69
+ property: string;
70
+ value: string | {
71
+ computed: string;
72
+ };
73
+ }[];
74
+ }
75
+ export declare function classifyPartKind(p: Pick<PartLike, 'part' | 'element'>): PartKind;
76
+ export interface PartPairingResult<D extends PartLike, B extends PartLike> {
77
+ pairs: {
78
+ want: D;
79
+ have: B;
80
+ }[];
81
+ unmatchedDesign: D[];
82
+ unmatchedBuilt: B[];
83
+ }
84
+ /**
85
+ * Pair Figma design parts with target parts: exact part-name matches first, then in order
86
+ * within each part kind (`container`, `text`, `icon`, ...). Unmatched non-root wrapper `div`s
87
+ * without visible styles are skipped cleanly.
88
+ */
89
+ export declare function pairPartsByPositionAndKind<D extends PartLike, B extends PartLike>(designParts: readonly D[], builtParts: readonly B[]): PartPairingResult<D, B>;
90
+ export declare function summarizeVariantDifferences(items: readonly {
91
+ part?: string;
92
+ property: string;
93
+ value: string;
94
+ token?: string;
95
+ }[]): string;
96
+ export declare function formatVariantCheckLabel(v: SpecVariant): string;
55
97
  /** Compare a built component (its file, read) with its design file (read). */
56
98
  export declare function checkComponent(design: ComponentSpec, built: ComponentSpec, options?: CheckOptions): CheckResult;
57
99
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../../src/capture/check.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,O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note. Pure and browser-safe.
7
7
  */
8
8
  import { z } from 'zod';
9
- import { colorKey } from './values.js';
9
+ import { colorKey, isFlowAlignmentProperty, isNothingStyleValue, normalizeFlowAlignment } from './values.js';
10
10
  export const CHECK_KINDS = ['style', 'token', 'part', 'state', 'motion', 'accessibility'];
11
11
  /** Tokens of a value at the top level: words and commas, with function calls kept whole. */
12
12
  function tokens(text) {
@@ -145,26 +145,87 @@ function sameShadow(a, b, o) {
145
145
  }
146
146
  /** The first font in a family list, as people name it: "Inter" for `"Inter", system-ui, sans-serif`. */
147
147
  const firstFamily = (value) => (value.split(',')[0] ?? '').trim().replace(/^["']|["']$/g, '').toLowerCase();
148
- /** Do two written values mean the same? Font families compare by their first font. */
148
+ /** Do two written values mean the same? Font families compare by their first font; alignment flow-* matches flex-*. */
149
149
  export function sameValue(property, a, b, options = {}) {
150
150
  const o = { tolerancePx: options.tolerancePx ?? 0.5, rootFontSize: options.rootFontSize ?? 16 };
151
151
  if (property === 'font-family')
152
152
  return firstFamily(a) === firstFamily(b);
153
153
  if (property === 'box-shadow')
154
154
  return sameShadow(a, b, o);
155
- return sameList(tokens(a), tokens(b), o);
155
+ let na = normalizeFlowAlignment(property, a.trim());
156
+ let nb = normalizeFlowAlignment(property, b.trim());
157
+ if (isFlowAlignmentProperty(property)) {
158
+ const modRx = /^(safe|unsafe)\s+/i;
159
+ const ma = modRx.exec(na);
160
+ const mb = modRx.exec(nb);
161
+ if (!ma || !mb || ma[1].toLowerCase() === mb[1].toLowerCase()) {
162
+ na = na.replace(modRx, '');
163
+ nb = nb.replace(modRx, '');
164
+ }
165
+ }
166
+ if (isNothingStyleValue(property, na) && isNothingStyleValue(property, nb))
167
+ return true;
168
+ return sameList(tokens(na), tokens(nb), o);
156
169
  }
157
170
  const withToken = (value, token) => (token ? `${value} (${token})` : value);
158
171
  /** "its background-color" for the container (the note sits on it), "the label's color" for a part. */
159
172
  const subject = (part, property) => (part === 'container' ? `its ${property}` : `the ${part}'s ${property}`);
160
173
  const capital = (text) => text.charAt(0).toUpperCase() + text.slice(1);
161
174
  const article = (word) => (/^[aeiou]/i.test(word) ? 'an' : 'a');
162
- function compareStyles(design, built, o, out) {
175
+ /** Main axis for a non-wrapping flex container (`row` or `column`), or null for grid / wrapped flex / non-flex. */
176
+ function flexMainAxisWithoutWrap(design, built) {
177
+ const find = (prop) => design.styles.find(s => s.property === prop)?.value.trim().toLowerCase()
178
+ ?? built.styles.find(s => s.property === prop)?.value.trim().toLowerCase();
179
+ const display = find('display') ?? '';
180
+ const flexDir = find('flex-direction');
181
+ const isFlex = display === 'flex'
182
+ || display === 'inline-flex'
183
+ || (!display && (/\bflex\b/i.test(design.element) || /\bflex\b/i.test(built.element) || Boolean(flexDir)));
184
+ if (!isFlex)
185
+ return null;
186
+ const wrap = find('flex-wrap') ?? 'nowrap';
187
+ if (wrap === 'wrap' || wrap === 'wrap-reverse')
188
+ return null;
189
+ return flexDir === 'column' || flexDir === 'column-reverse' ? 'column' : 'row';
190
+ }
191
+ /** Expand `gap` into axis gaps (`row-gap`, `column-gap`) and drop cross-axis gap for non-wrapping flex. */
192
+ function expandStylesForCompare(styles, flexAxis) {
193
+ const out = [];
194
+ const hasExplicitRowGap = styles.some(s => s.property === 'row-gap');
195
+ const hasExplicitColGap = styles.some(s => s.property === 'column-gap');
196
+ for (const s of styles) {
197
+ if (s.property === 'gap') {
198
+ const parts = tokens(s.value);
199
+ const rowVal = parts[0] ?? s.value;
200
+ const colVal = parts[1] ?? rowVal;
201
+ const sharedToken = parts.length <= 1 ? s.token : undefined;
202
+ if (!hasExplicitRowGap)
203
+ out.push({ property: 'row-gap', value: rowVal, ...(sharedToken ? { token: sharedToken } : {}) });
204
+ if (!hasExplicitColGap)
205
+ out.push({ property: 'column-gap', value: colVal, ...(sharedToken ? { token: sharedToken } : {}) });
206
+ }
207
+ else {
208
+ out.push(s);
209
+ }
210
+ }
211
+ if (flexAxis === 'column')
212
+ return out.filter(s => s.property !== 'column-gap');
213
+ if (flexAxis === 'row')
214
+ return out.filter(s => s.property !== 'row-gap');
215
+ return out;
216
+ }
217
+ function compareStyles(design, built, o, out, ctx = {}) {
163
218
  const known = new Set(o.knownTokens ?? []);
164
- for (const want of design.styles) {
165
- const have = built.styles.find(s => s.property === want.property);
219
+ const flexAxis = flexMainAxisWithoutWrap(design, built);
220
+ const designStyles = expandStylesForCompare(design.styles, flexAxis);
221
+ const builtStyles = expandStylesForCompare(built.styles, flexAxis);
222
+ for (const want of designStyles) {
223
+ const have = builtStyles.find(s => s.property === want.property);
166
224
  const label = `${design.part} ${want.property}`;
167
225
  if (!have) {
226
+ if (isNothingStyleValue(want.property, want.value)) {
227
+ continue;
228
+ }
168
229
  out.differences.push({
169
230
  kind: 'style', part: design.part, property: want.property, expected: withToken(want.value, want.token), actual: 'not set',
170
231
  comment: `${capital(subject(design.part, want.property))} is not set; the design says ${withToken(want.value, want.token)}.`, priority: 'medium'
@@ -188,7 +249,23 @@ function compareStyles(design, built, o, out) {
188
249
  }
189
250
  out.matches.push(label);
190
251
  }
191
- for (const extra of built.styles.filter(s => !design.styles.some(d => d.property === s.property))) {
252
+ for (const extra of builtStyles.filter(s => !designStyles.some(d => d.property === s.property))) {
253
+ if (isNothingStyleValue(extra.property, extra.value))
254
+ continue;
255
+ if (extra.property === 'text-align') {
256
+ if (design.part === 'container' && ctx.inferredContainerTextAlign && sameValue('text-align', ctx.inferredContainerTextAlign, extra.value, o)) {
257
+ out.matches.push(`${design.part} text-align`);
258
+ continue;
259
+ }
260
+ if (design.part !== 'container') {
261
+ const partKey = ctx.builtPartName ?? design.part;
262
+ const isInherited = ctx.inheritedTextAlign
263
+ ? ctx.inheritedTextAlign.has(partKey)
264
+ : Boolean(ctx.builtContainerTextAlign && sameValue('text-align', ctx.builtContainerTextAlign, extra.value, o));
265
+ if (isInherited)
266
+ continue;
267
+ }
268
+ }
192
269
  out.differences.push({
193
270
  kind: 'style', part: design.part, property: extra.property, expected: 'not set', actual: withToken(extra.value, extra.token),
194
271
  comment: `${capital(subject(design.part, extra.property))} is ${withToken(extra.value, extra.token)}; the design does not set it.`, priority: 'low'
@@ -292,28 +369,240 @@ function compareAccessibility(design, built, out, symmetric = false) {
292
369
  });
293
370
  }
294
371
  }
295
- /** Compare a built component (its file, read) with its design file (read). */
296
- export function checkComponent(design, built, options = {}) {
372
+ const INLINE_TEXT_RANGE_EL = /^(?:em|i|strong|b|u|s|del|ins|mark|small|sub|sup|abbr|cite|q|time)(?:\s|\(|$)/i;
373
+ const VISIBLE_WRAPPER_STYLE_RE = /^(?:background|background-color|background-image|border|border-(?:top|right|bottom|left)(?:-(?:width|style|color))?|border-width|border-style|border-color|border-radius|border-(?:top|bottom)-(?:left|right)-radius|box-shadow|opacity)$/;
374
+ const PLAIN_PART_KIND_LABELS = {
375
+ text: 'text',
376
+ container: 'container',
377
+ image: 'image',
378
+ icon: 'icon',
379
+ field: 'field',
380
+ action: 'button',
381
+ link: 'link',
382
+ item: 'list item',
383
+ other: 'other'
384
+ };
385
+ const PART_KIND_ORDER = ['container', 'text', 'image', 'icon', 'field', 'action', 'link', 'item', 'other'];
386
+ export function classifyPartKind(p) {
387
+ if (p.part === 'container')
388
+ return 'container';
389
+ const base = p.part.replace(/-\d+$/, '');
390
+ const el = p.element.trim().toLowerCase();
391
+ if (base === 'icon' || /^svg(?:\s|\(|$)/.test(el))
392
+ return 'icon';
393
+ if (base === 'image' || /^(?:img|picture|image|video|canvas)(?:\s|\(|$)/.test(el))
394
+ return 'image';
395
+ if (base === 'field' || /^(?:input|select|textarea)(?:\s|\(|$)/.test(el))
396
+ return 'field';
397
+ if (base === 'action' || /^button(?:\s|\(|$)/.test(el))
398
+ return 'action';
399
+ if (base === 'link' || /^a(?:\s|\(|$)/.test(el))
400
+ return 'link';
401
+ if (base === 'item' || /^li(?:\s|\(|$)/.test(el))
402
+ return 'item';
403
+ if (el === 'text'
404
+ || /\(text\)/.test(el)
405
+ || /^(?:span|p|h[1-6]|label|code|pre|kbd|blockquote|figcaption|dt|dd)(?:\s|\(|$)/.test(el)
406
+ || ['heading', 'description', 'label', 'code'].includes(base)) {
407
+ return 'text';
408
+ }
409
+ if (/^(?:div|section|article|main|aside|header|footer|nav|ul|ol|figure|dialog)(?:\s|\(|$)/.test(el)
410
+ || /\((?:flex|box|frame|group|component|instance|section)\)/.test(el)) {
411
+ return 'container';
412
+ }
413
+ return 'other';
414
+ }
415
+ function isUnstyledWrapperDiv(p) {
416
+ if (p.part === 'container')
417
+ return false;
418
+ if (classifyPartKind(p) !== 'container')
419
+ return false;
420
+ if (!/^div(?:\s|\(|$)/i.test(p.element.trim()))
421
+ return false;
422
+ for (const s of p.styles) {
423
+ if (!VISIBLE_WRAPPER_STYLE_RE.test(s.property))
424
+ continue;
425
+ const val = typeof s.value === 'string' ? s.value : s.value.computed;
426
+ if (!isNothingStyleValue(s.property, val))
427
+ return false;
428
+ }
429
+ return true;
430
+ }
431
+ /**
432
+ * Pair Figma design parts with target parts: exact part-name matches first, then in order
433
+ * within each part kind (`container`, `text`, `icon`, ...). Unmatched non-root wrapper `div`s
434
+ * without visible styles are skipped cleanly.
435
+ */
436
+ export function pairPartsByPositionAndKind(designParts, builtParts) {
437
+ const usedD = new Set();
438
+ const usedB = new Set();
439
+ const pairs = [];
440
+ // 1. Exact part-name agreement wins first
441
+ designParts.forEach((want, di) => {
442
+ const bi = builtParts.findIndex((have, idx) => !usedB.has(idx) && have.part === want.part);
443
+ if (bi !== -1) {
444
+ usedD.add(di);
445
+ usedB.add(bi);
446
+ pairs.push({ want, have: builtParts[bi] });
447
+ }
448
+ });
449
+ // 2. Remaining parts paired in order within each kind (excluding inline text-formatting spans)
450
+ const remD = designParts
451
+ .map((p, idx) => ({ p, idx }))
452
+ .filter(x => !usedD.has(x.idx) && !INLINE_TEXT_RANGE_EL.test(x.p.element));
453
+ const remB = builtParts
454
+ .map((p, idx) => ({ p, idx }))
455
+ .filter(x => !usedB.has(x.idx) && !INLINE_TEXT_RANGE_EL.test(x.p.element));
456
+ const builtQueues = new Map();
457
+ for (const b of remB) {
458
+ const kind = classifyPartKind(b.p);
459
+ const list = builtQueues.get(kind) ?? [];
460
+ list.push(b);
461
+ builtQueues.set(kind, list);
462
+ }
463
+ for (const d of remD) {
464
+ const kind = classifyPartKind(d.p);
465
+ const q = builtQueues.get(kind);
466
+ if (q && q.length > 0) {
467
+ const b = q.shift();
468
+ usedD.add(d.idx);
469
+ usedB.add(b.idx);
470
+ pairs.push({ want: d.p, have: b.p });
471
+ }
472
+ }
473
+ const unmatchedDesign = remD
474
+ .filter(d => !usedD.has(d.idx) && !isUnstyledWrapperDiv(d.p))
475
+ .map(d => d.p);
476
+ const unmatchedBuilt = remB
477
+ .filter(b => !usedB.has(b.idx) && !isUnstyledWrapperDiv(b.p))
478
+ .map(b => b.p);
479
+ return { pairs, unmatchedDesign, unmatchedBuilt };
480
+ }
481
+ function applyVariantToSpec(design, variant) {
482
+ if (variant.styles.length === 0)
483
+ return design;
484
+ const parts = design.parts.map(p => ({ ...p, styles: p.styles.map(s => ({ ...s })) }));
485
+ for (const override of variant.styles) {
486
+ const target = parts.find(p => p.part === override.part);
487
+ if (!target)
488
+ continue;
489
+ const idx = target.styles.findIndex(s => s.property === override.property);
490
+ const nextStyle = {
491
+ property: override.property,
492
+ value: override.value,
493
+ ...(override.token ? { token: override.token } : {})
494
+ };
495
+ if (idx >= 0)
496
+ target.styles[idx] = nextStyle;
497
+ else
498
+ target.styles.push(nextStyle);
499
+ }
500
+ return { ...design, parts };
501
+ }
502
+ function normFigmaNodeId(id) {
503
+ const m = /^I([^;]+);(.*)$/.exec(id.trim());
504
+ const clean = m ? m[1] : id.trim();
505
+ return clean.replace(/-/g, ':');
506
+ }
507
+ function variantMatchesLayer(variant, layer) {
508
+ if (!variant.nodeId)
509
+ return false;
510
+ if (layer.fileKey && variant.fileKey && variant.fileKey !== layer.fileKey)
511
+ return false;
512
+ return normFigmaNodeId(variant.nodeId) === normFigmaNodeId(layer.nodeId)
513
+ || variant.nodeId.replace(/-/g, ':') === layer.nodeId.replace(/-/g, ':');
514
+ }
515
+ function sharedTextPartsAlign(design, options) {
516
+ const container = design.parts.find(p => p.part === 'container');
517
+ if (container?.styles.some(s => s.property === 'text-align'))
518
+ return undefined;
519
+ const textParts = design.parts.filter(p => p.part !== 'container' && classifyPartKind(p) === 'text');
520
+ if (textParts.length === 0)
521
+ return undefined;
522
+ const aligns = textParts.map(p => p.styles.find(s => s.property === 'text-align')?.value);
523
+ if (aligns.some(a => !a))
524
+ return undefined;
525
+ const first = aligns[0];
526
+ return aligns.every(a => sameValue('text-align', first, a, options)) ? first : undefined;
527
+ }
528
+ function checkComponentSingle(design, built, options = {}) {
297
529
  const out = { design: { name: design.meta.name, type: design.meta.type, source: design.meta.source }, matches: [], differences: [] };
298
- const builtParts = new Map(built.parts.map(p => [p.part, p]));
530
+ const isFigma = design.meta.origin === 'figma';
299
531
  const both = new Set();
300
- for (const want of design.parts) {
301
- const have = builtParts.get(want.part);
302
- if (!have) {
532
+ const stripFigmaSizes = (part) => isFigma ? { ...part, styles: part.styles.filter(s => s.property !== 'width' && s.property !== 'height') } : part;
533
+ const inferredContainerTextAlign = sharedTextPartsAlign(design, options);
534
+ const inheritedTextAlign = options.inheritedTextAlignParts
535
+ ? (options.inheritedTextAlignParts instanceof Set
536
+ ? options.inheritedTextAlignParts
537
+ : new Set(options.inheritedTextAlignParts))
538
+ : undefined;
539
+ const builtContainerTextAlign = built.parts.find(p => p.part === 'container')?.styles.find(s => s.property === 'text-align')?.value;
540
+ if (isFigma) {
541
+ const { pairs, unmatchedDesign, unmatchedBuilt } = pairPartsByPositionAndKind(design.parts, built.parts);
542
+ if (unmatchedDesign.length > 0 || unmatchedBuilt.length > 0) {
543
+ const countByKind = (paired, unmatched) => {
544
+ const counts = new Map();
545
+ for (const p of [...paired, ...unmatched]) {
546
+ const k = classifyPartKind(p);
547
+ counts.set(k, (counts.get(k) ?? 0) + 1);
548
+ }
549
+ return counts;
550
+ };
551
+ const designCounts = countByKind(pairs.map(x => x.want), unmatchedDesign);
552
+ const builtCounts = countByKind(pairs.map(x => x.have), unmatchedBuilt);
553
+ const differingKinds = PART_KIND_ORDER.filter(k => (designCounts.get(k) ?? 0) !== (builtCounts.get(k) ?? 0));
554
+ const partCountText = (n, label) => `${n}${label ? ` ${label}` : ''} ${n === 1 ? 'part' : 'parts'}`;
555
+ const sentences = differingKinds.map(k => `Structure differs: the design has ${partCountText(designCounts.get(k) ?? 0, PLAIN_PART_KIND_LABELS[k])}, the page has ${builtCounts.get(k) ?? 0}.`);
556
+ const formatCountList = (counts) => differingKinds.map(k => partCountText(counts.get(k) ?? 0, PLAIN_PART_KIND_LABELS[k])).join(', ');
303
557
  out.differences.push({
304
- kind: 'part', part: want.part, property: 'part', expected: want.element, actual: 'none',
305
- comment: `The design has ${article(want.part)} ${want.part} (${want.element})${want.optional ? ', optional' : ''}; the built one has none.`, priority: want.optional ? 'low' : 'medium'
558
+ kind: 'part',
559
+ part: 'container',
560
+ property: 'structure',
561
+ expected: formatCountList(designCounts) || '0 parts',
562
+ actual: formatCountList(builtCounts) || '0 parts',
563
+ comment: sentences.join(' ') || `Structure differs: the design has ${partCountText(pairs.length + unmatchedDesign.length)}, the page has ${pairs.length + unmatchedBuilt.length}.`,
564
+ priority: 'medium'
565
+ });
566
+ }
567
+ for (const { want, have } of pairs) {
568
+ both.add(want.part);
569
+ compareStyles(stripFigmaSizes(want), stripFigmaSizes({ ...have, part: want.part }), options, out, {
570
+ inferredContainerTextAlign,
571
+ builtPartName: have.part,
572
+ inheritedTextAlign,
573
+ builtContainerTextAlign
306
574
  });
307
- continue;
308
575
  }
309
- both.add(want.part);
310
- compareStyles(want, have, options, out);
311
576
  }
312
- for (const extra of built.parts.filter(p => !design.parts.some(d => d.part === p.part))) {
313
- out.differences.push({
314
- kind: 'part', part: extra.part, property: 'part', expected: 'none', actual: extra.element,
315
- comment: `The built one has ${article(extra.part)} ${extra.part} (${extra.element}) that the design does not have.`, priority: 'low'
316
- });
577
+ else {
578
+ const builtParts = new Map(built.parts.map(p => [p.part, p]));
579
+ for (const want of design.parts) {
580
+ const have = builtParts.get(want.part);
581
+ if (!have) {
582
+ if (INLINE_TEXT_RANGE_EL.test(want.element))
583
+ continue;
584
+ out.differences.push({
585
+ kind: 'part', part: want.part, property: 'part', expected: want.element, actual: 'none',
586
+ comment: `The design has ${article(want.part)} ${want.part} (${want.element})${want.optional ? ', optional' : ''}; the built one has none.`, priority: want.optional ? 'low' : 'medium'
587
+ });
588
+ continue;
589
+ }
590
+ both.add(want.part);
591
+ compareStyles(want, have, options, out, {
592
+ inferredContainerTextAlign,
593
+ builtPartName: have.part,
594
+ inheritedTextAlign,
595
+ builtContainerTextAlign
596
+ });
597
+ }
598
+ for (const extra of built.parts.filter(p => !design.parts.some(d => d.part === p.part))) {
599
+ if (INLINE_TEXT_RANGE_EL.test(extra.element))
600
+ continue;
601
+ out.differences.push({
602
+ kind: 'part', part: extra.part, property: 'part', expected: 'none', actual: extra.element,
603
+ comment: `The built one has ${article(extra.part)} ${extra.part} (${extra.element}) that the design does not have.`, priority: 'low'
604
+ });
605
+ }
317
606
  }
318
607
  for (const want of design.states)
319
608
  compareState(want, built.states.find(s => s.state === want.state), both, options, out);
@@ -321,6 +610,75 @@ export function checkComponent(design, built, options = {}) {
321
610
  compareAccessibility(design.accessibility, built.accessibility, out, options.symmetricAccessibility);
322
611
  return out;
323
612
  }
613
+ export function summarizeVariantDifferences(items) {
614
+ if (items.length === 0)
615
+ return 'the base design';
616
+ const formatted = [];
617
+ const seen = new Set();
618
+ for (const item of items) {
619
+ const propName = item.part ? item.property : (item.property.split(/\s+/).pop() ?? item.property);
620
+ if (!item.token && isNothingStyleValue(propName, item.value))
621
+ continue;
622
+ const cleanPart = item.part && item.part !== 'container' ? `${item.part} ` : '';
623
+ const piece = item.token
624
+ ? item.token.replace(/^--/, '').replace(/^color-/, '')
625
+ : `${cleanPart}${item.property} ${item.value}`;
626
+ if (!seen.has(piece)) {
627
+ seen.add(piece);
628
+ formatted.push(piece);
629
+ }
630
+ }
631
+ if (formatted.length === 0)
632
+ return 'the base design';
633
+ if (formatted.length <= 3)
634
+ return formatted.join(', ');
635
+ return `${formatted.slice(0, 3).join(', ')}, +${formatted.length - 3} more`;
636
+ }
637
+ export function formatVariantCheckLabel(v) {
638
+ const trimmed = v.name.trim();
639
+ const isPlaceholder = /^variant-\d+$/i.test(trimmed) || /^base$/i.test(trimmed);
640
+ if (v.styles.length === 0 && (isPlaceholder || /^the base design$/i.test(trimmed))) {
641
+ return 'the base design';
642
+ }
643
+ if (isPlaceholder) {
644
+ return summarizeVariantDifferences(v.styles);
645
+ }
646
+ return trimmed;
647
+ }
648
+ /** Compare a built component (its file, read) with its design file (read). */
649
+ export function checkComponent(design, built, options = {}) {
650
+ const isFigma = design.meta.origin === 'figma';
651
+ const variants = isFigma ? design.variants : undefined;
652
+ if (!variants || variants.length === 0) {
653
+ return checkComponentSingle(design, built, options);
654
+ }
655
+ if (options.variantLayer) {
656
+ const bound = variants.find(v => variantMatchesLayer(v, options.variantLayer));
657
+ if (bound) {
658
+ return {
659
+ ...checkComponentSingle(applyVariantToSpec(design, bound), built, options),
660
+ variant: formatVariantCheckLabel(bound)
661
+ };
662
+ }
663
+ }
664
+ const baseIdx = Math.max(0, variants.findIndex(v => v.styles.length === 0));
665
+ let bestIdx = baseIdx;
666
+ let bestRes = checkComponentSingle(applyVariantToSpec(design, variants[baseIdx]), built, options);
667
+ for (let i = 0; i < variants.length; i++) {
668
+ if (i === baseIdx)
669
+ continue;
670
+ const res = checkComponentSingle(applyVariantToSpec(design, variants[i]), built, options);
671
+ // Strict fewer differences wins; on a tie, the base row wins (closest match, never hide real differences)
672
+ if (res.differences.length < bestRes.differences.length) {
673
+ bestIdx = i;
674
+ bestRes = res;
675
+ }
676
+ }
677
+ return {
678
+ ...bestRes,
679
+ variant: formatVariantCheckLabel(variants[bestIdx])
680
+ };
681
+ }
324
682
  /**
325
683
  * One run of the design check (v0.4, automatic design check), as the dev toolbar sends it to the
326
684
  * project each time it checks (again): the design, the built component and page, when, how many