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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 4x 4x 4x 4x 22x 8x 8x 8x 7x 7x 2x 2x 8x 2x 2x 2x 2x 2x 8x 8x 8x 23x 23x 23x 23x 23x 22x 22x 22x 8x 8x 22x 8x 8x 8x 8x 8x 6x 8x 8x 2x 2x 2x 10x 2x 1x 8x 8x 8x 8x 8x 8x 1x 4x 1x 1x 4x 13x 13x 5x 8x 8x 25x 8x 8x 8x 8x 8x 7x 1x 1x 1x | /**
* Compliance row: composition elements expose the same per-state color
* coverage as the component base.
*
* The Theme Layer RFC encourages per-state hooks for color
* (`-hover`, `-focus`, `-active`, `-disabled`) on the component
* base. When a component also exposes element-segment color hooks
* (`--slds-c-{C}-{el}-color-{cat}`), API symmetry calls for the
* element to expose matching state variants so customers can drive
* per-state coloring on inner elements just like they can on the
* base.
*
* Example: Button exposes `--slds-c-button-color-foreground` plus
* `-foreground-hover`, `-foreground-focus`, `-foreground-active`,
* `-foreground-disabled`. It also exposes
* `--slds-c-button-icon-color-foreground` (for the icon child
* element). Without this check, that's where the API symmetry stops:
* the icon has no state variants. A customer setting the idle icon
* color decouples it from the label, with no way to re-couple per-
* state coloring without explicit per-state icon hooks.
*
* This check flags the asymmetry as `review` (cascade-hygiene): the
* `currentColor` cascade is a legitimate design fallback, so the
* gap isn't always wrong, but the author should consciously decide
* between "open the per-state element hooks" and "rely on cascade."
*
* Scope: color category only. State suffixes outside the carveout
* (`-hover`, `-focus`, `-active`, `-disabled`) don't trigger the
* check.
*/
import type { ComplianceCheck, ComplianceRow, Offender, ThemeData } from '../types.js';
import { classifyHook } from './helpers/classifyHook.js';
import { resolveBemRoot, resolveHookPrefix } from './helpers/resolveHookPrefix.js';
import { hookDefsFromThemeData, NOT_MIGRATED_DETAIL } from './helpers/themeDataAccess.js';
const ID = 'element-color-state-coverage';
const LABEL = 'Composition elements match the base per-state color coverage';
const CATEGORY = 'cascade-hygiene' as const;
const COMPONENT_BASE = '__component-base__';
interface SurfaceKey {
element: string;
category: 'foreground' | 'background' | 'border';
}
function keyOf(s: SurfaceKey): string {
return `${s.element}|${s.category}`;
}
interface Gap {
element: string;
category: SurfaceKey['category'];
missingStates: string[];
expectedHookForFix: string;
}
/**
* Set of element-segment names that appear as actual BEM child
* elements in the component's selectors (i.e. as
* `.slds-{C}__{element}`). The state-coverage rule only applies to
* real child elements; hooks named after a modifier (e.g.
* `--slds-c-pill-container-color-background` for the
* `.slds-pill.slds-pill_container` Rule-5 compound) sit on a
* different surface that doesn't share state symmetry with the
* component base.
*/
function collectRealElements(themeData: Pick<ThemeData, 'selectors'>, ownBemRoot: string): Set<string> {
const elementPrefix = `.slds-${ownBemRoot}__`;
const out = new Set<string>();
for (const selector of Object.keys(themeData.selectors ?? {})) {
let cursor = 0;
while ((cursor = selector.indexOf(elementPrefix, cursor)) !== -1) {
const start = cursor + elementPrefix.length;
let end = start;
while (end < selector.length && /[a-z0-9-]/.test(selector[end])) end++;
const tail = selector.slice(start, end);
// Stop at the first BEM modifier-on-element separator (`_mod`)
// so `__icon_large` registers as element `icon`, not
// `icon_large`.
const underscore = tail.indexOf('_');
const elementName = underscore === -1 ? tail : tail.slice(0, underscore);
Eif (elementName) out.add(elementName);
cursor = end;
}
}
return out;
}
/**
* Build the `(element, category) → Set<state>` index from a unique-
* hook list. Empty-string state means "idle hook." Hooks whose
* element segment isn't a real `__element` child of the component
* are skipped (they're modifier-namespaced surfaces, see
* `collectRealElements`).
*/
function buildStateSets(
uniqueHooks: string[],
componentName: string,
realElements: Set<string>,
): Map<string, Set<string>> {
const stateSets = new Map<string, Set<string>>();
for (const hook of uniqueHooks) {
const cls = classifyHook(hook, componentName);
Iif (!cls.isColor) continue;
Iif (cls.classification !== 'canonical' && cls.classification !== 'canonical-state') continue;
const element = cls.element ?? COMPONENT_BASE;
if (element !== COMPONENT_BASE && !realElements.has(element)) continue;
const k = keyOf({ element, category: cls.category });
let bucket = stateSets.get(k);
if (!bucket) {
bucket = new Set<string>();
stateSets.set(k, bucket);
}
bucket.add(cls.classification === 'canonical-state' ? (cls.state ?? '') : '');
}
return stateSets;
}
function baseStatesByCategoryFrom(
stateSets: Map<string, Set<string>>,
): Map<SurfaceKey['category'], Set<string>> {
const out = new Map<SurfaceKey['category'], Set<string>>();
for (const [k, states] of stateSets) {
const [element, category] = k.split('|') as [string, SurfaceKey['category']];
if (element !== COMPONENT_BASE) continue;
out.set(category, states);
}
return out;
}
/**
* Compute the per-element gap for a single `(element, category)`
* surface. Returns `null` when the element shouldn't be checked
* (no idle hook on this surface) or the gap is empty (full state
* coverage).
*/
function gapFor(
element: string,
category: SurfaceKey['category'],
states: Set<string>,
baseStatesByCategory: Map<SurfaceKey['category'], Set<string>>,
ownPrefix: string,
): Gap | null {
if (element === COMPONENT_BASE) return null;
// Only check elements that expose the idle hook for this category.
// An element that only exposes a state hook (rare) is its own gap;
// base-hook-shape catches that case.
Iif (!states.has('')) return null;
const baseStates = baseStatesByCategory.get(category);
Iif (!baseStates) return null;
const missing = [...baseStates].filter((state) => state !== '' && !states.has(state));
if (missing.length === 0) return null;
return {
element,
category,
missingStates: missing,
expectedHookForFix: `--slds-c-${ownPrefix}-${element}-color-${category}-${missing[0]}`,
};
}
function findGaps(
stateSets: Map<string, Set<string>>,
baseStatesByCategory: Map<SurfaceKey['category'], Set<string>>,
ownPrefix: string,
): Gap[] {
const gaps: Gap[] = [];
for (const [k, states] of stateSets) {
const [element, category] = k.split('|') as [string, SurfaceKey['category']];
const gap = gapFor(element, category, states, baseStatesByCategory, ownPrefix);
if (gap) gaps.push(gap);
}
return gaps;
}
function offenderFromGap(g: Gap, ownBemRoot: string): Offender {
const stateLabel = g.missingStates.length === 1 ? 'state' : 'states';
const missingList = g.missingStates.map((s) => `\`-${s}\``).join(', ');
const codeQuoted = '`themes/base.css`';
return {
selector: `.slds-${ownBemRoot} .slds-${ownBemRoot}__${g.element}`,
note: `${g.element}/${g.category}: missing ${stateLabel} ${missingList}.`,
fix:
`Either expose the matching per-state element hook(s) (e.g. \`${g.expectedHookForFix}\`) and have the base paint each state at the element selector, ` +
`or document that this element relies on the parent's color cascade (currentColor / inherit) for state coloring. ` +
`The migrated Button family exposes per-state icon foreground hooks; see ${codeQuoted} for the pattern.`,
};
}
export const elementColorStateCoverage: ComplianceCheck = (input): ComplianceRow => {
const { componentName: name, themeData } = input;
if (!themeData) {
return {
id: ID,
label: LABEL,
category: CATEGORY,
status: 'info',
count: 0,
detail: NOT_MIGRATED_DETAIL,
};
}
const ownPrefix = resolveHookPrefix(name);
const ownBemRoot = resolveBemRoot(name);
const uniqueHooks = [...new Set(hookDefsFromThemeData(themeData).map((d) => d.prop))];
const realElements = collectRealElements(themeData, ownBemRoot);
const stateSets = buildStateSets(uniqueHooks, name, realElements);
const baseStatesByCategory = baseStatesByCategoryFrom(stateSets);
const gaps = findGaps(stateSets, baseStatesByCategory, ownPrefix);
if (gaps.length === 0) {
return {
id: ID,
label: LABEL,
category: CATEGORY,
status: 'pass',
count: 0,
detail:
'Every composition element with an idle color hook also exposes the per-state hooks the component base does. API symmetry preserved.',
};
}
const offenders: Offender[] = gaps.map((g) => offenderFromGap(g, ownBemRoot));
const totalMissing = gaps.reduce((sum, g) => sum + g.missingStates.length, 0);
return {
id: ID,
label: LABEL,
category: CATEGORY,
status: 'review',
count: offenders.length,
detail:
`${offenders.length} composition element${offenders.length === 1 ? '' : 's'} with idle color hooks but missing per-state hooks ` +
`(${totalMissing} state slot${totalMissing === 1 ? '' : 's'} total). The component base exposes per-state coloring (` +
`\`-hover\`/\`-focus\`/\`-active\`/\`-disabled\`); the inner element doesn't, so customers who set the idle element color ` +
`lose state coloring on it. Either open the matching per-state element hooks or document the cascade fallback as intentional.`,
offenders,
};
};
|