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

100% Statements 15/15
90.9% Branches 10/11
100% Functions 1/1
100% Lines 15/15

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                      4x                     4x             32x 1x           31x 31x 32x   32x 3x           28x   28x 3x 1x         2x     25x        
import type { Metadata, CategoryValidator, ValidationResult } from '../../types.js';
import { err } from './helpers.js';
import validateColor from './color.js';
import validateFont from './font.js';
import validateShadow from './shadow.js';
import validateSpacing from './spacing.js';
import validateSizing from './sizing.js';
import validateRadius from './radius.js';
import validateRatio from './ratio.js';
import validateDuration from './duration.js';
 
const CATEGORY_VALIDATORS: Record<string, CategoryValidator> = {
  color: validateColor,
  font: validateFont,
  shadow: validateShadow,
  spacing: validateSpacing,
  sizing: validateSizing,
  radius: validateRadius,
  ratio: validateRatio,
  duration: validateDuration,
};
 
const VALID_CATEGORIES = Object.keys(CATEGORY_VALIDATORS);
 
export function validateGlobalToken(
  tokens: string[],
  meta: Metadata,
  aliases: Record<string, string> = {},
): ValidationResult {
  if (tokens.length < 3) {
    return {
      errors: [err('structure', 'at least namespace-g-category', tokens.join('-'))],
      warnings: [],
    };
  }
 
  const category = tokens[2];
  const extendedCategories = meta?.global?.valid?.category || [];
  const allCategories = [...new Set([...VALID_CATEGORIES, ...extendedCategories])];
 
  if (!allCategories.includes(category)) {
    return {
      errors: [err('category', allCategories, category)],
      warnings: [],
    };
  }
 
  const validator = CATEGORY_VALIDATORS[category];
 
  if (!validator) {
    if (tokens.length < 4) {
      return {
        errors: [err('structure', 'at least one segment after category', category)],
        warnings: [],
      };
    }
    return { errors: [], warnings: [] };
  }
 
  return validator(tokens.slice(3), aliases);
}
 
export default validateGlobalToken;