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 | 8x 8x 43x 43x 43x 43x 43x 44x 44x 44x 83x 12x 12x 14x 2x 2x 12x 12x 14x 3x 3x 2x 3x 54x 6x 54x 44x 44x 44x 43x 2x 43x 43x 54x 8x 12x 10x 8x 10x 11x 11x 11x 11x 12x 12x 12x 12x 12x 21x 12x 11x 12x 12x 12x 26x 1x 1x 25x 12x 12x | /**
* Issue-oriented selector classifier used by the **structural** Rule 4
* checks ("hook writes only on the customer-reachable selector").
*
* Complementary to {@link selectorRole} (which classifies selector
* *role*: component-base / state / modifier / descendant). This
* classifier asks a different question: does this selector contain
* structural patterns that defeat customer overrides?
*
* The customer-reachable selector for a component is:
* - Light DOM: a single class selector matching the component's BEM
* root (`.slds-<component>`) or a sibling root modifier
* (`.slds-<component>_<modifier>` or `.slds-<component>-<state>`).
* - LBC: the host (`:host`, `:host([attr])`).
*
* Anything more deeply qualified than that is *not* customer-reachable
* through a documented pattern, and a hook write there is a Rule 4
* violation. The categories we care about:
*
* - `descendant-or-child`: `.slds-button .icon`, `.slds-button > .x`
* - `bem-element`: `.slds-button__icon` (BEM `__element` notation)
* - `attribute-substring`: `[icon-name^='action']`, `[class*='action']`
* - `pseudo-state`: `:hover`, `:focus-visible`, `:active`, `:disabled`,
* `:focus-within`, `:checked` (also covered by Rule 3)
* - `global-root`: `:root` (covered by Rule 1)
*
* Selectors that ARE customer-reachable include compound class chains
* on the root (e.g. `.slds-button.slds-button_brand` or
* `.slds-button_icon-border` standalone). These are addressable via the
* documented "extend with my class" pattern because the customer's
* class composes with the component root on the same element.
*/
import selectorParser from 'postcss-selector-parser';
export type SelectorIssue =
| 'descendant-or-child'
| 'bem-element'
| 'attribute-substring'
| 'pseudo-state'
| 'global-root';
const PSEUDO_STATES = new Set([
':hover',
':focus',
':focus-visible',
':focus-within',
':active',
':disabled',
':checked',
':target',
':visited',
':empty',
':placeholder-shown',
]);
const ATTRIBUTE_SUBSTRING_OPS = new Set(['^=', '$=', '*=', '~=', '|=']);
export interface SelectorClassification {
raw: string;
/** Issues found across any selector in the comma list. */
issues: SelectorIssue[];
/** True if every selector in the list is customer-reachable. */
isCustomerReachable: boolean;
/** True if this selector is `:root` (or contains it). */
isRoot: boolean;
}
/**
* Classify a selector against Rule 1 / 3 / 4 categories. Returns the
* full set of issues across every selector in the comma list, plus
* convenience booleans.
*/
export function classifySelector(rawSelector: string, componentName: string): SelectorClassification {
const issues = new Set<SelectorIssue>();
let isRoot = false;
let allReachable = true;
const root = selectorParser().astSync(rawSelector);
root.each((sel) => {
const perSelectorIssues = new Set<SelectorIssue>();
let perSelectorRoot = false;
sel.walk((node) => {
switch (node.type) {
case 'combinator': {
// Any combinator means a multi-compound selector (descendant
// ` `, child `>`, sibling `+`, general sibling `~`). The
// descendant combinator's value is a literal space which
// trims to empty, so we treat any combinator node as the
// signal regardless of value contents.
perSelectorIssues.add('descendant-or-child');
break;
}
case 'pseudo': {
if (node.value === ':root') {
perSelectorRoot = true;
isRoot = true;
} else Eif (PSEUDO_STATES.has(node.value)) {
perSelectorIssues.add('pseudo-state');
}
break;
}
case 'attribute': {
const op = node.operator ?? '';
if (ATTRIBUTE_SUBSTRING_OPS.has(op)) {
perSelectorIssues.add('attribute-substring');
}
break;
}
case 'class': {
if (looksLikeBemElement(node.value)) {
perSelectorIssues.add('bem-element');
}
break;
}
default:
break;
}
});
perSelectorIssues.forEach((i) => issues.add(i));
const reachable = perSelectorIssues.size === 0 && !perSelectorRoot;
if (!reachable) allReachable = false;
});
if (isRoot) {
issues.add('global-root');
}
// `componentName` is reserved for future per-component refinements
// (e.g. distinguishing self-vs-other BEM elements). Not used today.
void componentName;
return {
raw: rawSelector,
issues: Array.from(issues),
isCustomerReachable: allReachable && !isRoot,
isRoot,
};
}
/**
* BEM `__element` notation: `.slds-button__icon`, `.slds-input__icon`.
* These are inner elements of a component, never customer-reachable
* as a standalone class because the customer cannot place that class
* on their own element without breaking the component's internal
* structure.
*
* We accept any `__` element naming, not just for the component being
* audited, because cross-component descendant writes (e.g. an
* `.slds-input__icon` rule inside `icon.css`) are also Rule 4
* violations.
*/
function looksLikeBemElement(className: string): boolean {
return className.includes('__');
}
/**
* BEM `_modifier` notation on the component root:
* `.slds-button_neutral`, `.slds-button_full-width`, `.slds-button_reset`.
* Excludes `__element` (double-underscore) which is a separate category.
*
* Modifier rules are part of the system's painting surface for that
* modifier. The customer-reachable extension point is the base selector
* (`.slds-button`); customizing a modifier's per-state hook on the base
* is by design overridden by the modifier's own write. So hook writes
* inside modifier pseudo-states or modifier rules are not Rule 3 / 4d
* violations.
*/
/* Multi-word components author kebab-case BEM roots
(`slds-avatar-group_x-small`), so the root segment allows hyphens. */
const BEM_MODIFIER_RE = /^slds-[a-z][a-z0-9-]*_[a-z][a-z0-9-]*$/;
function looksLikeBemModifier(className: string): boolean {
if (className.includes('__')) return false;
return BEM_MODIFIER_RE.test(className);
}
/**
* BEM `__element_modifier` notation on a child element:
* `.slds-avatar__initials_inverse`, `.slds-button__icon_large`.
*
* Element-modifier rules are the system's painting surface for a
* specific variant of a child element. Same reasoning as root-level
* modifiers: customers can't reach a per-variant child rule through
* the base selector, and the system intentionally overrides the
* base hook for this variant. Pseudo-state and mixed-rule writes
* here are part of the variant's own paint, not Rule 3 / 4d
* violations.
*/
const BEM_ELEMENT_MODIFIER_RE = /^slds-[a-z][a-z0-9-]*__[a-z][a-z0-9-]*_[a-z][a-z0-9-]*$/;
function looksLikeBemElementModifier(className: string): boolean {
return BEM_ELEMENT_MODIFIER_RE.test(className);
}
/**
* True when *every* selector in the comma list targets a BEM modifier
* class on the rightmost compound — either a root modifier
* (`.slds-<root>_<mod>`) or an element-modifier
* (`.slds-<root>__<el>_<mod>`) — optionally with pseudo-state
* qualifiers (`.slds-button_neutral:hover`,
* `.slds-avatar__initials_inverse:hover`).
*
* "Targets" here means the rightmost compound contains a modifier class
* — `.slds-button_neutral .icon` does NOT target the modifier (it
* targets a descendant), and stays subject to descendant / paint
* checks. `.slds-avatar .slds-avatar__initials_inverse` DOES target
* the element-modifier on its rightmost compound.
*/
export function targetsBemModifier(rawSelector: string): boolean {
const root = selectorParser().astSync(rawSelector);
let allTargetModifier = true;
let sawAny = false;
root.each((sel) => {
sawAny = true;
const compounds = splitCompounds(sel);
Iif (compounds.length === 0) {
allTargetModifier = false;
return;
}
const rightmost = compounds.at(-1) ?? [];
const hasModifier = rightmost.some(
(node) =>
node.type === 'class' &&
(looksLikeBemModifier(node.value ?? '') || looksLikeBemElementModifier(node.value ?? '')),
);
if (!hasModifier) allTargetModifier = false;
});
return sawAny && allTargetModifier;
}
type SelectorAstNode = { type: string; value?: string };
/**
* Split a selector list entry into compound selectors at combinator
* boundaries. `selectorParser` exposes combinators as siblings of the
* compound's class / pseudo / attribute nodes, so a simple iteration
* over `selector.nodes` is enough.
*/
function splitCompounds(sel: { nodes: SelectorAstNode[] }): SelectorAstNode[][] {
const compounds: SelectorAstNode[][] = [];
let current: SelectorAstNode[] = [];
for (const node of sel.nodes) {
if (node.type === 'combinator') {
Eif (current.length > 0) compounds.push(current);
current = [];
} else {
current.push(node);
}
}
Eif (current.length > 0) compounds.push(current);
return compounds;
}
|