impeccable 3.2.1 → 3.3.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.
@@ -288,15 +288,74 @@ function addTypographyFonts(out, typography) {
288
288
  }
289
289
  }
290
290
 
291
+ function addFontSizeStep(out, raw, { fluid = false } = {}) {
292
+ const text = String(raw ?? '').trim().toLowerCase();
293
+ if (!FONT_SIZE_LITERAL_RE.test(text)) return;
294
+ const px = resolveLengthPx(text, 16);
295
+ if (px == null || !Number.isFinite(px) || px <= 0) return;
296
+ out.allowedFontSizes.push({ value: text, px, fluid });
297
+ }
298
+
299
+ // Split a fluid value into its three terms, or null when it is not a
300
+ // well-formed clamp(). Used both to read DESIGN.md's fluid roles and to
301
+ // validate fluid values in source, so the two stay symmetric.
302
+ function parseClampArgs(raw) {
303
+ const match = /^clamp\(\s*([\s\S]+)\s*\)$/i.exec(String(raw ?? '').trim());
304
+ if (!match) return null;
305
+ const args = splitTopLevelArgs(match[1]);
306
+ return args.length === 3 ? args : null;
307
+ }
308
+
309
+ // A fluid role declares its two fixed endpoints and interpolates between them
310
+ // with a viewport unit. Both endpoints are documented sizes, so they belong in
311
+ // the allowlist; the middle term is viewport-relative and never a fixed step.
312
+ // Endpoints are marked `fluid` because they do not *enumerate* a ramp: see
313
+ // `hasFontSizes` below for why that distinction has to survive.
314
+ function addClampEndpoints(out, raw) {
315
+ const args = parseClampArgs(raw);
316
+ if (!args) return false;
317
+ addFontSizeStep(out, args[0], { fluid: true });
318
+ addFontSizeStep(out, args[2], { fluid: true });
319
+ return true;
320
+ }
321
+
322
+ function splitTopLevelArgs(s) {
323
+ const args = [];
324
+ let depth = 0;
325
+ let current = '';
326
+ for (const ch of String(s)) {
327
+ if (ch === '(') depth++;
328
+ else if (ch === ')') depth--;
329
+ if (ch === ',' && depth === 0) {
330
+ args.push(current.trim());
331
+ current = '';
332
+ continue;
333
+ }
334
+ current += ch;
335
+ }
336
+ if (current.trim()) args.push(current.trim());
337
+ return args;
338
+ }
339
+
291
340
  function addTypographySizes(out, typography) {
292
341
  if (!typography || typeof typography !== 'object') return;
293
- for (const role of Object.values(typography)) {
342
+
343
+ // `scale` is the enumerated ramp: a name -> size map, since the frontmatter
344
+ // parser has no list support. It sits alongside the named roles.
345
+ const scale = typography.scale;
346
+ if (scale && typeof scale === 'object') {
347
+ for (const value of Object.values(scale)) {
348
+ if (typeof value !== 'string' && typeof value !== 'number') continue;
349
+ addFontSizeStep(out, value);
350
+ }
351
+ }
352
+
353
+ for (const [name, role] of Object.entries(typography)) {
354
+ if (name === 'scale') continue;
294
355
  if (!role || typeof role !== 'object') continue;
295
356
  const raw = String(role.fontSize ?? '').trim().toLowerCase();
296
- if (!FONT_SIZE_LITERAL_RE.test(raw)) continue;
297
- const px = resolveLengthPx(raw, 16);
298
- if (px == null || !Number.isFinite(px) || px <= 0) continue;
299
- out.allowedFontSizes.push({ value: raw, px });
357
+ if (addClampEndpoints(out, raw)) continue;
358
+ addFontSizeStep(out, raw);
300
359
  }
301
360
  }
302
361
 
@@ -371,7 +430,10 @@ function normalizeDesignSystem(input = {}) {
371
430
  out.hasFonts = out.allowedFonts.size > 0;
372
431
  out.hasColors = out.allowedColorKeys.size > 0;
373
432
  out.hasRadii = out.allowedRadii.length > 0;
374
- out.hasFontSizes = out.allowedFontSizes.length > 0;
433
+ // Gate on *enumerated* steps only. A fully fluid system declares clamp
434
+ // endpoints but no discrete ramp, so treating those endpoints as the whole
435
+ // allowlist would flag every intermediate size. Abstain instead.
436
+ out.hasFontSizes = out.allowedFontSizes.some(entry => !entry.fluid);
375
437
  return out;
376
438
  }
377
439
 
@@ -438,17 +500,43 @@ function isAllowedRadiusRaw(raw, designSystem) {
438
500
  return designSystem.allowedRadii.some(entry => Math.abs(entry.px - px) <= RADIUS_TOLERANCE_PX);
439
501
  }
440
502
 
441
- function isAllowedFontSizeRaw(raw, designSystem) {
442
- if (!designSystem?.hasFontSizes) return true;
443
- const text = String(raw || '').trim().toLowerCase().replace(/\s*!important\s*$/, '');
444
- if (!FONT_SIZE_LITERAL_RE.test(text)) return true;
503
+ // One term of a font-size value. `unjudgeable` covers var(), calc(), percentages
504
+ // and units the ramp cannot resolve (em is parent-relative, not root-relative);
505
+ // those abstain rather than guess.
506
+ function fontSizeStepStatus(raw, designSystem) {
507
+ const text = String(raw || '').trim().toLowerCase();
508
+ if (!FONT_SIZE_LITERAL_RE.test(text)) return 'unjudgeable';
445
509
  const px = resolveLengthPx(text, 16);
446
- if (px == null || !Number.isFinite(px) || px <= 0) return true;
510
+ if (px == null || !Number.isFinite(px) || px <= 0) return 'unjudgeable';
447
511
  return designSystem.allowedFontSizes.some(
448
512
  entry => Math.abs(entry.px - px) <= FONT_SIZE_TOLERANCE_PX,
513
+ ) ? 'on-ramp' : 'off-ramp';
514
+ }
515
+
516
+ // The off-ramp endpoints of a fluid value, or null when `raw` is not a fluid
517
+ // value at all. Only the min and max are judged: the viewport term interpolates
518
+ // between them and is never a fixed step.
519
+ //
520
+ // Reading clamp endpoints as documented steps without also checking them in
521
+ // usage would let `clamp(99rem, 1vw, 200rem)` through, which is how a fluid
522
+ // declaration stayed invisible until someone measured computed styles.
523
+ export function offRampClampEndpoints(raw, designSystem) {
524
+ if (!designSystem?.hasFontSizes) return null;
525
+ const args = parseClampArgs(String(raw || '').trim().replace(/\s*!important\s*$/i, ''));
526
+ if (!args) return null;
527
+ return [args[0], args[2]].filter(
528
+ endpoint => fontSizeStepStatus(endpoint, designSystem) === 'off-ramp',
449
529
  );
450
530
  }
451
531
 
532
+ function isAllowedFontSizeRaw(raw, designSystem) {
533
+ if (!designSystem?.hasFontSizes) return true;
534
+ const text = String(raw || '').trim().toLowerCase().replace(/\s*!important\s*$/, '');
535
+ const offRampEndpoints = offRampClampEndpoints(text, designSystem);
536
+ if (offRampEndpoints) return offRampEndpoints.length === 0;
537
+ return fontSizeStepStatus(text, designSystem) !== 'off-ramp';
538
+ }
539
+
452
540
  function lineLooksCommented(line) {
453
541
  const trimmed = String(line || '').trim();
454
542
  return trimmed.startsWith('//') || trimmed.startsWith('/*') || trimmed.startsWith('*') || trimmed.startsWith('<!--');
@@ -543,12 +631,31 @@ function checkRadiusValue(value, filePath, line, designSystem, context) {
543
631
  function checkFontSizeValue(value, filePath, line, designSystem, context) {
544
632
  const token = String(value || '').trim();
545
633
  if (isAllowedFontSizeRaw(token, designSystem)) return [];
634
+
635
+ // Name the offending endpoint on a fluid value; the whole clamp() string is
636
+ // not actionable on its own, and it makes a poor ignore-value.
637
+ const offRampEndpoints = offRampClampEndpoints(token, designSystem) || [];
638
+ if (offRampEndpoints.length > 0) {
639
+ const plural = offRampEndpoints.length > 1 ? 's' : '';
640
+ return [makeDesignFinding(
641
+ 'design-system-font-size',
642
+ filePath,
643
+ `${context}: ${token} has fluid endpoint${plural} ${offRampEndpoints.join(' and ')} off the DESIGN.md type ramp`,
644
+ line,
645
+ { ignoreValue: offRampEndpoints[0] },
646
+ )];
647
+ }
648
+
649
+ // The snippet shows the declaration as authored, but the ignoreValue has to
650
+ // be what a `hooks ignore-value` waiver can match, so the priority marker is
651
+ // stripped. Otherwise the same size needs two different waivers depending on
652
+ // whether it carries !important. font-family already behaves this way.
546
653
  return [makeDesignFinding(
547
654
  'design-system-font-size',
548
655
  filePath,
549
656
  `${context}: ${token} is off the DESIGN.md type ramp`,
550
657
  line,
551
- { ignoreValue: token },
658
+ { ignoreValue: token.replace(/\s*!important\s*$/i, '').trim() },
552
659
  )];
553
660
  }
554
661