All files / packages/sds-customization-compliance/src/checks/helpers classifyHook.ts

88.23% Statements 30/34
76.31% Branches 29/38
100% Functions 1/1
90.62% Lines 29/32

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143                                                          11x                   11x 11x   11x                                         104x 104x 104x   104x 104x 5x   5x 5x               99x 99x 83x 83x   83x 17x 15x               2x 1x             1x 1x                               66x 3x 3x               3x               3x   63x    
/**
 * Classify a defined hook name against RFC 1157. Mirrors the classifier in
 * `generate-component-tasks.mjs` verbatim, with types added.
 */
 
import { resolveHookPrefix } from './resolveHookPrefix.js';
 
/** Colour categories recognized by the RFC. */
export type ColorCategory = 'background' | 'foreground' | 'border';
 
/** Possible outputs of {@link classifyHook}. */
export type HookClassification =
  'canonical' | 'canonical-state' | 'property-first' | 'invented-state' | 'invented-modifier' | 'non-color';
 
export interface NonColorHook {
  isColor: false;
  classification: 'non-color';
}
 
export interface ColorHook {
  isColor: true;
  classification: Exclude<HookClassification, 'non-color'>;
  category: ColorCategory;
  element: string | null;
  state?: string;
}
 
export type ClassifiedHook = ColorHook | NonColorHook;
 
const STATE_TOKENS = new Set([
  'hover',
  'focus',
  'focus-visible',
  'active',
  'disabled',
  'visited',
  'selected',
]);
 
const CARVEOUT_STATE_SUFFIXES = new Set(['hover', 'focus', 'active', 'disabled']);
const CARVEOUT_COLOR_CATEGORIES = new Set<ColorCategory>(['background', 'foreground', 'border']);
 
const MODIFIER_TOKENS = new Set([
  'brand',
  'destructive',
  'inverse',
  'neutral',
  'success',
  'warning',
  'error',
  'info',
  'outline',
  'ghost',
  'small',
  'medium',
  'large',
  'x-small',
  'xx-small',
  'x-large',
  'xx-large',
]);
 
export function classifyHook(hook: string, name: string): ClassifiedHook {
  const prefix = `--slds-c-${resolveHookPrefix(name)}-`;
  Iif (!hook.startsWith(prefix)) return { isColor: false, classification: 'non-color' };
  const rest = hook.slice(prefix.length);
 
  const propertyFirst = /(^|-)(text-color|background-color|border-color)(-|$)/.test(rest);
  if (propertyFirst) {
    const m = rest.match(/(text-color|background-color|border-color)/);
    const cat: ColorCategory =
      m?.[1] === 'text-color' ? 'foreground' : m?.[1] === 'background-color' ? 'background' : 'border';
    return {
      isColor: true,
      classification: 'property-first',
      category: cat,
      element: null,
    };
  }
 
  const m = rest.match(/^(?:(.+?)-)?color-(background|foreground|border)(?:-(.+))?$/);
  if (!m) return { isColor: false, classification: 'non-color' };
  const [, elementPart, category, tail] = m;
  const cat = category as ColorCategory;
 
  if (tail) {
    if (CARVEOUT_STATE_SUFFIXES.has(tail) && CARVEOUT_COLOR_CATEGORIES.has(cat)) {
      return {
        isColor: true,
        classification: 'canonical-state',
        category: cat,
        element: elementPart ?? null,
        state: tail,
      };
    }
    if (STATE_TOKENS.has(tail)) {
      return {
        isColor: true,
        classification: 'invented-state',
        category: cat,
        element: elementPart ?? null,
      };
    }
    Eif (MODIFIER_TOKENS.has(tail)) {
      return {
        isColor: true,
        classification: 'invented-modifier',
        category: cat,
        element: elementPart ?? null,
      };
    }
    // Unknown trailing segment — conservative.
    return {
      isColor: true,
      classification: 'invented-state',
      category: cat,
      element: elementPart ?? null,
    };
  }
 
  if (elementPart) {
    const firstSeg = elementPart.split('-')[0];
    Iif (STATE_TOKENS.has(elementPart) || STATE_TOKENS.has(firstSeg)) {
      return {
        isColor: true,
        classification: 'invented-state',
        category: cat,
        element: elementPart,
      };
    }
    Iif (MODIFIER_TOKENS.has(elementPart) || MODIFIER_TOKENS.has(firstSeg)) {
      return {
        isColor: true,
        classification: 'invented-modifier',
        category: cat,
        element: elementPart,
      };
    }
    return { isColor: true, classification: 'canonical', category: cat, element: elementPart };
  }
  return { isColor: true, classification: 'canonical', category: cat, element: null };
}