All files / packages/sds-stylelint-config/src/validators/global font.ts

94.73% Statements 18/19
95.23% Branches 20/21
100% Functions 2/2
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42        4x 80x               16x 16x   16x 1x 1x     15x 15x 16x 16x   16x 1x 1x     14x       14x 1x     14x    
import type { ValidationResult, ValidationError, ValidationWarning } from '../../types.js';
import { err, hasNumericRange, resolveDescriptor } from './helpers.js';
import { FONT_DESCRIPTORS } from './__generated-allowlists.js';
 
const TERMINAL = new Set<string>(FONT_DESCRIPTORS.filter((d) =>
  d.endsWith('-base') || d.endsWith('-monospace') || d.endsWith('-bold') ||
  d.endsWith('-ratio') || d.endsWith('-clamp') || d === 'family',
));
 
export default function validateFont(
  segments: string[],
  aliases: Record<string, string> = {},
): ValidationResult {
  const errors: ValidationError[] = [];
  const warnings: ValidationWarning[] = [];
 
  if (segments.length === 0) {
    errors.push(err('font', 'a descriptor after font', '(empty)'));
    return { errors, warnings };
  }
 
  const hasRange = hasNumericRange(segments);
  const descriptorSegments = hasRange ? segments.slice(0, -1) : segments;
  const { descriptor, warnings: aliasWarnings } = resolveDescriptor(descriptorSegments, aliases);
  warnings.push(...aliasWarnings);
 
  if (!FONT_DESCRIPTORS.includes(descriptor)) {
    errors.push(err('font-descriptor', FONT_DESCRIPTORS, descriptor));
    return { errors, warnings };
  }
 
  Iif (hasRange && TERMINAL.has(descriptor)) {
    errors.push(err('font-descriptor', `"${descriptor}" does not take a numeric range`, segments.join('-')));
  }
 
  if (!hasRange && !TERMINAL.has(descriptor)) {
    errors.push(err('range', `"${descriptor}" requires a numeric range`, segments.join('-')));
  }
 
  return { errors, warnings };
}