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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | 9x 9x 9x 9x 9x 144x 9993x 9x 1390x 435x 154x 115x 6156x 23209x 17076x 32x 23209x 23209x 12087x 11x 2184x 1454x 11x 20x 20x 20x 1472x 1471x 1471x 1471x 1x 1x 4679x 1471x 1470x 12x 20x 3x 3x 5x 5x 5x 3x 8x 6x 6x 2x 2x 8x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x | /**
* Core CSS properties audit logic
*
* Finds --slds-g-* styling hooks whose `cssProperties` contradict the role
* encoded in the token name. A container hook such as
* --slds-g-color-accent-container-3 is a background fill, so its properties may
* be "background", "background-color", or "fill"; "color" and "border-color"
* are wrong for that name.
*
* Pure functions only, so the rules can be unit tested with mock data.
* The CLI entry point is css-properties-audit.ts.
*
* @module css-properties-audit-utils
*/
const BLOCK_SPACING = [
'margin-block',
'margin-block-start',
'margin-block-end',
'padding-block',
'padding-block-start',
'padding-block-end',
'inset-block',
'inset-block-start',
'inset-block-end',
'top',
'bottom',
];
const INLINE_SPACING = [
'margin-inline',
'margin-inline-start',
'margin-inline-end',
'padding-inline',
'padding-inline-start',
'padding-inline-end',
'inset-inline',
'inset-inline-start',
'inset-inline-end',
'left',
'right',
];
const GENERAL_SPACING = [
'margin',
'padding',
'inset',
'gap',
'row-gap',
'column-gap',
...BLOCK_SPACING,
...INLINE_SPACING,
];
const SIZING_DIMENSIONS = [
'width',
'height',
'min-width',
'max-width',
'min-height',
'max-height',
'inline-size',
'block-size',
'min-inline-size',
'max-inline-size',
'min-block-size',
'max-block-size',
];
const GENERAL_COLOR = ['background', 'background-color', 'border-color', 'color', 'fill'];
export interface CssPropertiesRule {
id: string;
test: (token: string) => boolean;
allowed: string[];
reason: string;
}
export interface CssPropertiesFinding {
token: string;
ruleId: string;
reason: string;
actual: string[];
allowed: string[];
unexpected: string[];
missingAllowed: boolean;
}
export interface CssPropertiesAuditResult {
scanned: number;
findings: CssPropertiesFinding[];
}
/** Builds a rule that matches every token whose name starts with `prefix`. */
const prefixRule = (id: string, prefix: string, allowed: string[], reason: string): CssPropertiesRule => ({
id,
test: (token) => token.startsWith(prefix),
allowed,
reason,
});
/**
* First match wins. More specific name roles come before the permissive
* fallbacks. Tokens with no role word (accent, error, palette, base) may
* list several color properties; container / border / on / surface may not.
*/
export const RULES: CssPropertiesRule[] = [
prefixRule(
'color-border',
'--slds-g-color-border',
['border-color'],
'A border token applies to border-color.',
),
prefixRule(
'color-on',
'--slds-g-color-on-',
['color', 'fill'],
'An on-* token is foreground text or an icon fill.',
),
{
id: 'color-container',
test: (token) => token.startsWith('--slds-g-color-') && token.includes('-container'),
allowed: ['background', 'background-color', 'fill'],
reason: 'A container token is a background fill. Use background, background-color, or fill, not color.',
},
prefixRule(
'color-surface',
'--slds-g-color-surface',
['background', 'background-color'],
'A surface token is a page or canvas background.',
),
prefixRule(
'color-general',
'--slds-g-color-',
GENERAL_COLOR,
'A color token with no container, border, on, or surface role may be used as a general color.',
),
prefixRule(
'blur',
'--slds-g-blur-',
['backdrop-filter', 'filter'],
'A blur token applies to backdrop-filter or filter.',
),
prefixRule(
'duration',
'--slds-g-duration-',
['animation*', 'transition*'],
'A duration token applies to animation and transition durations.',
),
prefixRule(
'font-line-clamp',
'--slds-g-font-line-clamp',
['line-clamp', '-webkit-line-clamp'],
'A line-clamp token applies to line-clamp, not font-size.',
),
prefixRule(
'font-line-height',
'--slds-g-font-line-height',
['line-height'],
'A line-height token applies to line-height.',
),
prefixRule(
'font-family',
'--slds-g-font-family',
['font-family'],
'A font-family token applies to font-family.',
),
prefixRule(
'font-weight',
'--slds-g-font-weight',
['font-weight'],
'A font-weight token applies to font-weight.',
),
{
id: 'font-size',
test: (token) => token.startsWith('--slds-g-font-scale') || token.startsWith('--slds-g-font-size'),
allowed: ['font-size'],
reason: 'A font-scale or font-size token applies to font-size.',
},
prefixRule('radius', '--slds-g-radius', ['border-radius'], 'A radius token applies to border-radius.'),
prefixRule('ratio', '--slds-g-ratio', ['aspect-ratio'], 'A ratio token applies to aspect-ratio.'),
prefixRule('shadow', '--slds-g-shadow', ['box-shadow'], 'A shadow token applies to box-shadow.'),
prefixRule(
'sizing-border',
'--slds-g-sizing-border',
['border-width'],
'A sizing-border token applies to border-width.',
),
prefixRule(
'sizing',
'--slds-g-sizing',
SIZING_DIMENSIONS,
'A sizing token applies to width and height, not border-width.',
),
{
id: 'spacing-block',
test: (token) => token.includes('-spacing-') && token.includes('-block'),
allowed: BLOCK_SPACING,
reason: 'A block spacing token applies to the block axis (top/bottom or padding-block/margin-block).',
},
{
id: 'spacing-inline',
test: (token) => token.includes('-spacing-') && token.includes('-inline'),
allowed: INLINE_SPACING,
reason:
'An inline spacing token applies to the inline axis (left/right or padding-inline/margin-inline).',
},
prefixRule(
'spacing',
'--slds-g-spacing-',
GENERAL_SPACING,
'A spacing token applies to margin, padding, or inset.',
),
];
/** A trailing `*` in an allowed entry matches the stem and any `stem-*` property. */
export function propertyAllowed(actual: string, allowed: string[]): boolean {
return allowed.some((expected) => {
if (actual === expected) return true;
if (!expected.endsWith('*')) return false;
const actualStem = actual.endsWith('*') ? actual.slice(0, -1) : actual;
const expectedStem = expected.slice(0, -1);
return actualStem === expectedStem || actualStem.startsWith(`${expectedStem}-`);
});
}
export function matchRule(token: string): CssPropertiesRule | null {
return RULES.find((rule) => rule.test(token)) ?? null;
}
/**
* Normalizes a design-tokens flat file (`{ 'slds-g-x': { cssProperties, scope, deprecated } }`)
* into `[--hook-name, properties]` pairs.
*
* Deprecated tokens are skipped, matching what sds-metadata publishes.
*/
export function toAuditEntries(input: Record<string, any>): Array<[string, string[]]> {
let result: Array<[string, string[]]> = Object.entries(input ?? {})
.filter(([, token]: [string, any]) => token?.scope === 'global' && !token?.deprecated)
.map(([key, token]: [string, any]) => [
`--${key}`,
Array.isArray(token?.cssProperties) ? token.cssProperties : [],
]);
return result;
}
export function auditTokenProperties(entries: Array<[string, string[]]>): CssPropertiesAuditResult {
const findings: CssPropertiesFinding[] = [];
let scanned = 0;
for (const [token, actual] of entries) {
if (!token.startsWith('--slds-g-')) continue;
scanned += 1;
const rule = matchRule(token);
if (!rule) {
findings.push({
token,
ruleId: 'unclassified',
reason: 'No property rule matches this token name.',
actual,
allowed: [],
unexpected: actual,
missingAllowed: true,
});
continue;
}
const unexpected = actual.filter((property) => !propertyAllowed(property, rule.allowed));
const hasAllowed = actual.some((property) => propertyAllowed(property, rule.allowed));
if (unexpected.length === 0 && hasAllowed) continue;
findings.push({
token,
ruleId: rule.id,
reason: rule.reason,
actual,
allowed: rule.allowed,
unexpected,
missingAllowed: !hasAllowed,
});
}
return { scanned, findings };
}
/** Groups findings that share a rule and the same set of unexpected properties. */
export function groupFindings(findings: CssPropertiesFinding[]): CssPropertiesFinding[][] {
const groups = new Map<string, CssPropertiesFinding[]>();
for (const finding of findings) {
const key = [
finding.ruleId,
finding.unexpected.join(','),
finding.missingAllowed ? 'missing' : 'present',
].join('|');
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(finding);
}
return [...groups.values()];
}
export function printReport(label: string, { scanned, findings }: CssPropertiesAuditResult) {
if (findings.length === 0) {
console.log(`✅ ${label}: no property anomalies in ${scanned} --slds-g-* tokens.`);
return;
}
const groups = groupFindings(findings);
console.log(
`❌ ${label}: ${findings.length} property ${findings.length === 1 ? 'anomaly' : 'anomalies'} in ${groups.length} ${groups.length === 1 ? 'group' : 'groups'} (${scanned} tokens scanned).`,
);
for (const group of groups) {
const sample = group[0];
console.log('');
console.log(` [${sample.ruleId}] ${sample.reason}`);
console.log(` allowed: ${sample.allowed.join(', ') || '(none)'}`);
Eif (sample.unexpected.length > 0) {
console.log(` unexpected: ${sample.unexpected.join(', ')}`);
}
if (sample.missingAllowed) {
console.log(' missing: none of the allowed properties are declared');
}
console.log(` ${group.length} ${group.length === 1 ? 'token' : 'tokens'}:`);
for (const finding of group) {
console.log(` ${finding.token} [${finding.actual.join(', ')}]`);
}
}
console.log('');
}
|