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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | 4x 4x 4x 4x 13x 13x 5x 5x 21x 21x 21x 13x 13x 11x 13x 7x 7x 5x 18x 18x 21x 21x 13x 13x 12x 21x 18x 18x 18x 6x 5x 5x 5x 5x 5x 18x 5x 5x 1x 4x 18x 18x 13x 1x 12x 12x 12x 12x 12x 12x 12x 18x 12x 11x 5x 5x 18x 5x 1x 1x 12x 12x 9x 7x 5x 5x 5x | /**
* Discoverability advisory: surface unregistered component identities.
*
* `resolveHookPrefix` and `resolveBemRoot` (in `helpers/resolveHookPrefix.ts`)
* carry three override maps that tell the classifier which BEM class roots
* a component owns and which `--slds-c-*` hook namespace its hooks use:
*
* - `BEM_ROOT_OVERRIDES` — directory name doesn't match the kebab-case
* BEM class root (e.g. `globalNavigation` ships `.slds-context-bar`,
* not `.slds-global-navigation`).
* - `BEM_SIBLING_ROOTS` — directory ships more than one independent root
* (e.g. `card` ships both `.slds-card` and `.slds-card-wrapper`).
* - `HOOK_PREFIX_OVERRIDES` — hook namespace differs from the directory
* name (e.g. `buttonIcon` writes `--slds-c-button-*`).
*
* When a component's actual class roots / hook namespaces aren't covered
* by these maps, every selector that paints the unregistered root
* classifies as `foreign-or-generic`. Many structural and hook-surface
* checks then silently report 0 offenders even though the component has
* no protection.
*
* The diagnostic is simple: the directory name's kebab form should match
* the leftmost SLDS class root the component paints under. When they
* disagree, the override maps need an entry. This check makes that
* discrepancy visible by comparing the directory's resolved roots to
* the leftmost class roots actually painted by the directory's own
* CSS rules.
*
* Severity: `info`. The discoverability gap doesn't directly break the
* customer override path — other rules still flag underlying authoring
* problems if they exist. It just makes those rules go quiet on
* components that need an override-map entry. Surfacing this as
* advisory is the intended shape: a contributor sees the row, follows
* the offender to `helpers/resolveHookPrefix.ts`, and adds the entry
* as part of the per-component uplift.
*/
import type { Declaration } from 'postcss';
import type { ComplianceCheck, ComplianceRow, ComponentSourceFile, Offender } from '../../types.js';
import { resolveBemRoots, resolveHookPrefix } from '../helpers/resolveHookPrefix.js';
import { isRelayRoutingRule } from '../helpers/relayRouting.js';
import {
declLocation,
isHookWriteDecl,
notRunYetRow,
passRow,
themeLayerFilesFor,
visitHookWriteRules,
} from './internals.js';
const ID = 'structural-component-registration-discoverability';
const LABEL = 'Component identity is registered for the compliance classifier';
const CATEGORY = 'customer-reach' as const;
const HELPER_PATH = 'packages/sds-customization-compliance/src/checks/helpers/resolveHookPrefix.ts';
/** Extract the leftmost `.slds-*` class token from a selector string, or null. */
function leftmostSldsClass(selector: string): string | null {
const match = /^\.slds-([a-z0-9][a-z0-9_-]*)/i.exec(selector.trim());
return match ? match[1] : null;
}
/** Extract the prefix segment from a `--slds-c-{prefix}-...` declaration name. */
function hookPrefix(propName: string): string | null {
const match = /^--slds-c-([a-z0-9]+)(?:-|$)/i.exec(propName);
return match ? match[1] : null;
}
/**
* True when a comma-branch starts with a class token that the directory
* paints AS its own surface rather than scoping a descendant. The
* leftmost class is the leaf paint target when nothing in the rest of
* the selector pulls in another `.slds-*` class via descendant
* combinator.
*
* `.slds-foo` → own paint
* `.slds-foo:hover` → own paint
* `.slds-foo.slds-foo_brand` → own paint (compound on same element)
* `.slds-foo .slds-bar` → NOT own paint (descendant scope)
* `.slds-foo > .slds-bar` → NOT own paint
*/
function isOwnPaintBranch(branch: string): boolean {
const trimmed = branch.trim();
const leftmostMatch = /^\.slds-[a-z0-9][a-z0-9_-]*/i.exec(trimmed);
if (!leftmostMatch) return false;
const tail = trimmed.slice(leftmostMatch[0].length);
return !/[\s>+~]\.slds-/.test(tail);
}
/**
* True when `rootSegment` is recognized by the directory's registered
* roots — either as an exact match, or as a BEM child / modifier of a
* registered root (e.g. `accordion__summary` matches the registered
* `accordion` root).
*/
function isRegisteredRoot(rootSegment: string, ownedRoots: ReadonlyArray<string>): boolean {
for (const owned of ownedRoots) {
if (rootSegment === owned) return true;
Iif (rootSegment.startsWith(`${owned}__`)) return true; // BEM element
Iif (rootSegment.startsWith(`${owned}_`)) return true; // BEM modifier
}
return false;
}
/**
* Collect every distinct leftmost `.slds-*` class the file paints AS
* its own paint surface (not as a descendant scoping ancestor), with
* a representative source location for the citation.
*/
function collectLeftmostClasses(file: ComponentSourceFile): Map<string, string> {
const out = new Map<string, string>();
file.root.walkRules((rule) => {
for (const branch of rule.selector.split(',')) {
if (!isOwnPaintBranch(branch)) continue;
const root = leftmostSldsClass(branch);
if (!root || out.has(root)) continue;
const line = rule.source?.start?.line;
out.set(root, line ? `${file.path}:${line}` : file.path);
}
});
return out;
}
/**
* Collect every distinct hook prefix the file writes, with a
* representative declaration for the citation. Skips relay-routing
* rules: those legitimately write an embedded component's hook
* namespace from this component's own surface and would be a false
* positive here.
*/
function collectHookPrefixes(file: ComponentSourceFile, ownHookPrefix: string): Map<string, Declaration> {
const out = new Map<string, Declaration>();
visitHookWriteRules(file, ({ hookDecls }) => {
if (isRelayRoutingRule(hookDecls, ownHookPrefix)) return;
for (const decl of hookDecls) {
Iif (!isHookWriteDecl(decl)) continue;
const prefix = hookPrefix(decl.prop);
Iif (!prefix || out.has(prefix)) continue;
out.set(prefix, decl);
}
});
return out;
}
function offenderForUnregisteredRoot(
rootSegment: string,
location: string,
componentName: string,
ownedRoots: ReadonlyArray<string>,
): Offender {
const ownedList = ownedRoots.map((r) => `\`.slds-${r}\``).join(', ');
return {
selector: `.slds-${rootSegment}`,
location,
note:
`Component paints \`.slds-${rootSegment}\` but the compliance ` +
`classifier resolves \`${componentName}\` to ${ownedList}. ` +
`Selectors under \`.slds-${rootSegment}\` will classify as ` +
`\`foreign-or-generic\` and many structural / hook-surface checks ` +
`will silently report 0 offenders for paint they should be auditing.`,
fix:
`In ${HELPER_PATH}, add an entry to either \`BEM_ROOT_OVERRIDES\` ` +
`(if \`.slds-${rootSegment}\` is the directory's primary root and ` +
`kebab(componentName) doesn't already produce it) or ` +
`\`BEM_SIBLING_ROOTS\` (if the directory ships multiple top-level ` +
`roots and \`.slds-${rootSegment}\` is one of them alongside the ` +
`existing primary).`,
};
}
function offenderForUnregisteredHookPrefix(
observedPrefix: string,
expectedPrefix: string,
decl: Declaration,
file: ComponentSourceFile,
componentName: string,
): Offender {
return {
location: declLocation(file, decl),
note:
`Component writes \`--slds-c-${observedPrefix}-*\` hooks but the ` +
`compliance classifier resolves \`${componentName}\`'s hook ` +
`namespace to \`--slds-c-${expectedPrefix}-*\`. Hook-surface ` +
`checks key on the resolved namespace; mismatched writes won't ` +
`surface in coverage reports.`,
fix:
`If \`${componentName}\` legitimately writes hooks under the ` +
`\`${observedPrefix}\` namespace (because it shares a hook ` +
`surface with another component, like \`buttonIcon\` → \`button\`), ` +
`add an entry to \`HOOK_PREFIX_OVERRIDES\` in ${HELPER_PATH}. ` +
`If the namespace is genuinely unique to this component, expect ` +
`\`--slds-c-${expectedPrefix}-*\` instead.`,
};
}
export const componentRegistrationDiscoverability: ComplianceCheck = (input): ComplianceRow => {
const files = themeLayerFilesFor(input);
if (!files) return notRunYetRow(ID, LABEL, CATEGORY);
if (files.length === 0) {
return passRow(ID, LABEL, 'Component has no theme or theme-base files; nothing to check.', CATEGORY);
}
const ownedRoots = resolveBemRoots(input.componentName);
const expectedHookPrefix = resolveHookPrefix(input.componentName);
const rootOffenders: Offender[] = [];
const seenRoots = new Set<string>();
const prefixOffenders: Offender[] = [];
const seenPrefixes = new Set<string>();
for (const file of files) {
for (const [rootSegment, location] of collectLeftmostClasses(file)) {
if (seenRoots.has(rootSegment)) continue;
if (isRegisteredRoot(rootSegment, ownedRoots)) continue;
seenRoots.add(rootSegment);
rootOffenders.push(offenderForUnregisteredRoot(rootSegment, location, input.componentName, ownedRoots));
}
for (const [prefix, decl] of collectHookPrefixes(file, expectedHookPrefix)) {
if (prefix === expectedHookPrefix || seenPrefixes.has(prefix)) continue;
seenPrefixes.add(prefix);
prefixOffenders.push(
offenderForUnregisteredHookPrefix(prefix, expectedHookPrefix, decl, file, input.componentName),
);
}
}
const offenders = [...rootOffenders, ...prefixOffenders];
if (offenders.length === 0) {
const ownedList = ownedRoots.map((r) => `\`.slds-${r}\``).join(', ');
return passRow(
ID,
LABEL,
`Component identity registered correctly: BEM roots ${ownedList}, ` +
`hook namespace \`--slds-c-${expectedHookPrefix}-*\`. The ` +
`compliance classifier will run every check against this component's ` +
`actual paint surface.`,
CATEGORY,
);
}
const ownedList = ownedRoots.map((r) => `\`.slds-${r}\``).join(', ');
const detail =
`Component paints class roots or writes hook namespaces that aren't ` +
`registered in ${HELPER_PATH}. Each offender below names the ` +
`expected map (\`BEM_ROOT_OVERRIDES\`, \`BEM_SIBLING_ROOTS\`, or ` +
`\`HOOK_PREFIX_OVERRIDES\`). Until registered, structural and ` +
`hook-surface checks classify the affected selectors as ` +
`\`foreign-or-generic\` and silently underreport offenders. ` +
`Resolved identity for \`${input.componentName}\`: BEM roots ${ownedList}, ` +
`hook namespace \`--slds-c-${expectedHookPrefix}-*\`.`;
return {
id: ID,
label: LABEL,
category: CATEGORY,
status: 'info',
count: offenders.length,
detail,
offenders,
};
};
|