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

100% Statements 18/18
93.33% Branches 14/15
100% Functions 2/2
100% Lines 16/16

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        16x           9x 9x   9x 1x 1x     8x 1x     7x 1x 1x     6x   5x 5x 1x     5x    
import type { ValidationResult, ValidationError, ValidationWarning } from '../../types.js';
import { err, hasNumericRange } from './helpers.js';
import { SIZING_DESCRIPTORS } from './__generated-allowlists.js';
 
const TERMINAL = new Set<string>(SIZING_DESCRIPTORS.filter((d) => d === 'base'));
 
export default function validateSizing(
  segments: string[],
  aliases: Record<string, string> = {},
): ValidationResult {
  const errors: ValidationError[] = [];
  const warnings: ValidationWarning[] = [];
 
  if (segments.length === 0) {
    errors.push(err('sizing', 'a range or descriptor after sizing', '(empty)'));
    return { errors, warnings };
  }
 
  if (segments.length === 1 && TERMINAL.has(segments[0])) {
    return { errors, warnings };
  }
 
  if (!hasNumericRange(segments)) {
    errors.push(err('range', 'numeric range as final segment', segments.at(-1) ?? '(missing)'));
    return { errors, warnings };
  }
 
  if (segments.length === 1) return { errors, warnings };
 
  const descriptor = segments.slice(0, -1).join('-');
  if (!SIZING_DESCRIPTORS.includes(descriptor)) {
    errors.push(err('sizing-descriptor', SIZING_DESCRIPTORS, descriptor));
  }
 
  return { errors, warnings };
}