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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | 4x 4x 4x 4x 4x 4x 4x 18x 18x 16x 16x 16x 8x 8x 8x 8x 8x 269x 269x 261x 16x 16x 261x 8x 8x 4x 9x 9x 38x 7x 1x 1x 37x 17x 14x 10x 10x 10x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 8x 8x 1x 7x 14x 14x 21x 21x 14x 14x 14x 14x 14x 14x 21x 21x 14x 14x 14x 14x 21x 14x 1x 1x 14x 14x 14x 14x 21x 21x 14x 14x 14x 3x 14x 7x 3x 1x 1x 14x 4x 4x 20x 20x 15x 19x 15x 1x 14x 14x 14x 4x 14x 10x 4x | /**
* Rule: spacing / margin / padding declarations on logical axes must
* be authored on the *direction-specific* property and reference the
* direction hook with the axis hook as a `var()` fallback.
*
* The theme-layer architecture exposes spacing as a two-tier hook
* surface so customers can override either the whole axis or one
* direction:
*
* ✓ padding-inline-start: var(--slds-c-foo-spacing-inline-start, var(--slds-c-foo-spacing-inline));
* ✓ padding-block-end: var(--slds-c-foo-spacing-block-end, var(--slds-c-foo-spacing-block));
*
* Authoring on the axis property (`padding-inline`) walls the
* direction overrides — the customer sets `--slds-c-foo-spacing-inline-end`
* but the cascade never reads it. Reading the direction hook without
* the axis fallback walls the axis override — `--slds-c-foo-spacing-inline`
* has no read site to flow through.
*
* Spacing-pattern hooks named on a logical-axis suffix are subject to
* the rule: `padding-inline-{start,end}`, `padding-block-{start,end}`,
* `margin-inline-{start,end}`, `margin-block-{start,end}`. Hooks that
* paint a non-logical axis (`gap`, four-value `padding`, `margin: auto`)
* are unaffected.
*
* Severity: `fail` (`customer-reach`). A wrong shape blocks the
* customer override path; the rule is a hard requirement.
*
* Pair check: {@link spacingAxisOnlyAssignment} enforces the theme
* side of the contract — themes assign at the axis level only.
*/
import type { Declaration, Rule } from 'postcss';
import type { ComplianceCheck, ComplianceRow, ComponentSourceFile, Offender } from '../../types.js';
import { declLocation, failRow, notRunYetRow, passRow, themeLayerFilesFor } from './internals.js';
const ID = 'structural-spacing-direction-syntax';
const LABEL = 'Logical-axis spacing reads target the direction property with axis fallback';
const CATEGORY = 'customer-reach' as const;
/** Logical-axis CSS property names this check governs. */
const DIRECTION_PROPS: ReadonlySet<string> = new Set([
'padding-inline-start',
'padding-inline-end',
'padding-block-start',
'padding-block-end',
'margin-inline-start',
'margin-inline-end',
'margin-block-start',
'margin-block-end',
]);
/** Axis-level property names — readable but not the right authoring site. */
const AXIS_PROPS: ReadonlySet<string> = new Set([
'padding-inline',
'padding-block',
'margin-inline',
'margin-block',
]);
/** Match the *opening* of a `var(--slds-c-…` call. Sticky-anchorless. */
const VAR_OPEN_RE = /var\(\s*(--slds-c-[a-z0-9-]+)\s*/i;
/** Cheap presence check used to tell hook-routed values from literals. */
const HAS_HOOK_VAR_RE = /var\(\s*--slds-c-/i;
/**
* Parse a CSS value and return the first `var(--slds-c-…[, fallback])`
* call as `{ hook, fallback }`. The fallback may itself contain a
* nested `var()` (the canonical shape for the spacing rule), which
* defeats a non-greedy regex; this function tracks paren depth so
* the fallback substring is captured fully. Returns `null` when no
* matching call is found.
*/
function parseVarCall(value: string): { hook: string; fallback: string } | null {
const open = VAR_OPEN_RE.exec(value);
if (open?.index === undefined) return null;
const hook = open[1];
let cursor = open.index + open[0].length;
// We're now positioned just after the hook name (and any trailing
// whitespace the regex consumed). Two cases: `)` closes the call
// immediately, or `,` introduces a fallback expression.
if (value[cursor] === ')') return { hook, fallback: '' };
Iif (value[cursor] !== ',') return null;
cursor += 1;
// Walk until the matching `)` of the outer var() call, respecting
// nested parentheses inside the fallback expression.
let depth = 1;
const fallbackStart = cursor;
while (cursor < value.length && depth > 0) {
const ch = value[cursor];
if (ch === '(') depth += 1;
else if (ch === ')') {
depth -= 1;
if (depth === 0) break;
}
cursor += 1;
}
Iif (depth !== 0) return null;
return { hook, fallback: value.slice(fallbackStart, cursor).trim() };
}
interface DeclOffender {
selector: string;
prop: string;
declValue: string;
location: string;
reason: 'no-fallback' | 'wrong-fallback' | 'axis-property' | 'literal-value';
fixHint: string;
}
function inferComponentFromHook(hookName: string): string | null {
// Strip `--slds-c-` and take the leading segments up to a category
// boundary. The component prefix can be multi-segment, but for the
// purposes of building a sibling axis hook we only need to swap the
// tail — the component scope is whatever precedes the spacing token.
if (!hookName.startsWith('--slds-c-')) return null;
return hookName.slice('--slds-c-'.length);
}
/**
* Given the direction-specific hook name (`--slds-c-foo-spacing-inline-end`),
* return the axis hook name (`--slds-c-foo-spacing-inline`). Returns
* `null` when the hook isn't shaped like a recognized direction hook.
*/
const DIRECTION_HOOK_RE = /^(--slds-c-[a-z0-9-]+-(?:spacing|margin)-(?:inline|block))-(?:start|end)$/i;
function expectedAxisHookFor(directionHook: string): string | null {
const m = DIRECTION_HOOK_RE.exec(directionHook);
return m ? m[1] : null;
}
/** Direction the prop addresses — `inline` or `block`. */
function axisOf(prop: string): 'inline' | 'block' | null {
if (prop.includes('-inline-')) return 'inline';
if (prop.includes('-block-')) return 'block';
return null;
}
function buildDirectionFixHint(prop: string, declValue: string, expectedAxisHook: string): string {
return (
`\`${prop}\` should reference the direction hook with the axis hook as fallback: ` +
`\`${prop}: var(<direction-hook>, var(${expectedAxisHook}))\`. Saw \`${declValue}\`.`
);
}
function directionTailOf(prop: string): 'start' | 'end' | null {
if (prop.endsWith('-start')) return 'start';
Eif (prop.endsWith('-end')) return 'end';
return null;
}
interface DirectionContext {
/** True when the partner direction (`-end` for a `-start` decl, etc.)
* is also read on the same selector + axis. The axis-fallback
* requirement only applies when both directions are read together
* — singleton direction reads are allowed to skip the fallback. */
partnerPresent: boolean;
}
/**
* Inspect a `padding-inline-start` / `margin-block-end` / etc.
* declaration and report what's wrong with its `var()` shape, or
* `null` when the shape is canonical.
*
* The whole rule is gated by `ctx.partnerPresent`. When a selector
* reads both the start AND end of an axis, the pair must cascade
* through a shared axis hook so a customer can override either tier
* and the missing tier still resolves: that's the
* direction-with-axis-fallback contract.
*
* When a selector reads only one direction, the axis isn't being
* painted as a cohesive unit. Any shape is acceptable: literal
* values, bare direction hooks, even reads of an orthogonal-axis
* hook (e.g. a vertical-orientation slider painting
* `padding-block-start: var(--…-spacing-inline)` because the
* writing-mode flip rotates the visual axis). The author has
* declared "this direction stands alone," and the pair-cascade
* requirement doesn't apply. Hook-routing bugs on singletons are
* caught by other checks (no-orphan-hooks, etc.); this rule's only
* concern is the start/end pair contract.
*/
function inspectDirectionDecl(
decl: Declaration,
ctx: DirectionContext,
): { reason: DeclOffender['reason']; fixHint: string } | null {
// Singleton direction → opt out of the rule entirely.
if (!ctx.partnerPresent) return null;
const value = decl.value.trim();
const call = parseVarCall(value);
if (!call) {
return {
reason: 'literal-value',
fixHint:
`\`${decl.prop}\` paints a literal value. The theme layer expects ` +
`\`var(<direction-hook>, var(<axis-hook>))\` so customer overrides on either tier reach the cascade.`,
};
}
const directionHook = call.hook;
const fallback = call.fallback;
const propAxis = axisOf(decl.prop);
const directionTail = directionTailOf(decl.prop);
Iif (!propAxis || !directionTail) return null;
const expectedSuffix = `-${propAxis}-${directionTail}`;
Iif (!directionHook.endsWith(expectedSuffix)) {
const expectedAxisHook =
expectedAxisHookFor(`${inferComponentFromHook(directionHook) ?? '<component>'}${expectedSuffix}`) ??
`--slds-c-<component>-spacing-${propAxis}`;
return {
reason: 'wrong-fallback',
fixHint: buildDirectionFixHint(decl.prop, value, expectedAxisHook),
};
}
const expectedAxisHook = expectedAxisHookFor(directionHook);
Iif (!expectedAxisHook) return null;
if (!fallback) {
return {
reason: 'no-fallback',
fixHint:
`\`${decl.prop}: var(${directionHook})\` has no fallback, but the partner direction is also read on this selector — both must cascade through \`${expectedAxisHook}\`. ` +
`Add the axis hook: \`var(${directionHook}, var(${expectedAxisHook}))\`. (If you intentionally don't want this direction to cascade through the axis, drop the partner direction's fallback too — the axis hook then doesn't apply.)`,
};
}
// Fallback must be `var(<expectedAxisHook>[, …])`. We accept
// additional nested fallbacks inside that var() but the head must
// be the axis hook.
const fallbackCall = parseVarCall(fallback);
if (fallbackCall?.hook !== expectedAxisHook) {
return {
reason: 'wrong-fallback',
fixHint: buildDirectionFixHint(decl.prop, value, expectedAxisHook),
};
}
return null;
}
/**
* Walk a single rule's direction declarations and build the
* `(prop) → DirectionContext` map. Two directions on the same axis
* are partners; partnership is determined per selector, not per file,
* because a different rule block may legitimately paint a different
* shape (e.g. a modifier compound asserting only one direction).
*/
function buildDirectionContextsForRule(rule: Rule): Map<string, DirectionContext> {
// Collect which `(axis, direction)` pairs exist.
const seen = new Set<string>(); // `${axis}|${tail}`
rule.walkDecls((decl: Declaration) => {
const prop = decl.prop.toLowerCase();
if (!DIRECTION_PROPS.has(prop)) return;
const axis = axisOf(prop);
const tail = directionTailOf(prop);
Iif (!axis || !tail) return;
seen.add(`${axis}|${tail}`);
});
const ctxMap = new Map<string, DirectionContext>();
rule.walkDecls((decl: Declaration) => {
const prop = decl.prop.toLowerCase();
if (!DIRECTION_PROPS.has(prop)) return;
const axis = axisOf(prop);
const tail = directionTailOf(prop);
Iif (!axis || !tail) return;
const partnerTail: 'start' | 'end' = tail === 'start' ? 'end' : 'start';
ctxMap.set(prop, { partnerPresent: seen.has(`${axis}|${partnerTail}`) });
});
return ctxMap;
}
function inspectAxisDecl(decl: Declaration): { reason: DeclOffender['reason']; fixHint: string } {
const propAxis = axisOf(decl.prop) ?? (decl.prop.endsWith('-inline') ? 'inline' : 'block');
return {
reason: 'axis-property',
fixHint:
`\`${decl.prop}\` paints the whole axis at once, walling the direction-level customer override. ` +
`Split into \`${decl.prop}-start\` and \`${decl.prop}-end\` declarations, each reading ` +
`\`var(<direction-hook>, var(<axis-hook>))\`. The axis hook (\`-${propAxis}\`) stays the customer override surface.`,
};
}
function collectDirectionViolations(file: ComponentSourceFile): DeclOffender[] {
const out: DeclOffender[] = [];
file.root.walkRules((rule: Rule) => {
const ctxByProp = buildDirectionContextsForRule(rule);
rule.walkDecls((decl: Declaration) => {
const prop = decl.prop.toLowerCase();
if (DIRECTION_PROPS.has(prop)) {
const ctx = ctxByProp.get(prop) ?? { partnerPresent: false };
const issue = inspectDirectionDecl(decl, ctx);
if (issue) {
out.push({
selector: rule.selector,
prop,
declValue: decl.value.trim(),
location: declLocation(file, decl),
reason: issue.reason,
fixHint: issue.fixHint,
});
}
return;
}
if (AXIS_PROPS.has(prop)) {
// Axis property is only a violation when the value is hook-routed.
// A literal `padding-inline: 0` (e.g. on a reset modifier) doesn't
// hide a customer-reachable hook; flag only the var-call form.
if (!HAS_HOOK_VAR_RE.test(decl.value)) return;
const issue = inspectAxisDecl(decl);
out.push({
selector: rule.selector,
prop,
declValue: decl.value.trim(),
location: declLocation(file, decl),
reason: issue.reason,
fixHint: issue.fixHint,
});
}
});
});
return out;
}
function offenderFrom(violation: DeclOffender, file: ComponentSourceFile): Offender {
return {
selector: violation.selector,
prop: violation.prop,
note: `${violation.location} — ${violation.reason}`,
fix: `${violation.fixHint} (file: \`${file.path}\`)`,
};
}
export const spacingDirectionSyntax: ComplianceCheck = (input): ComplianceRow => {
const files = themeLayerFilesFor(input);
if (!files) return notRunYetRow(ID, LABEL, CATEGORY);
Iif (files.length === 0) {
return passRow(ID, LABEL, 'Component has no theme or theme-base files; nothing to check.', CATEGORY);
}
// Only base reads should follow the direction-with-axis-fallback
// shape; the axis-only-assignment partner check covers theme files.
const baseFiles = files.filter((f) => f.role === 'theme-base');
if (baseFiles.length === 0) {
return passRow(ID, LABEL, 'Component has no themes/base.css; nothing to check.', CATEGORY);
}
const offenders: Offender[] = [];
for (const file of baseFiles) {
for (const v of collectDirectionViolations(file)) {
offenders.push(offenderFrom(v, file));
}
}
if (offenders.length === 0) {
return passRow(
ID,
LABEL,
'All logical-axis spacing reads use the direction-with-axis-fallback shape.',
CATEGORY,
);
}
return failRow(
ID,
LABEL,
`${offenders.length} declaration${offenders.length === 1 ? '' : 's'} on \`padding-inline-*\` / \`margin-block-*\` / etc. don't follow the direction-with-axis-fallback shape. ` +
`The convention is \`padding-inline-start: var(<direction-hook>, var(<axis-hook>))\` so customers can override either tier and the missing tier still resolves.`,
offenders,
);
};
|