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 | 5x 109x 109x 6x 39x 39x 39x 39x 70x 70x 64x 64x 4x 4x 4x 4x 4x 35x 35x 35x 35x 35x 35x 70x 70x 35x 8x 8x 8x 8x 8x 3x 3x 8x 8x 8x 8x 16x 16x 12x 12x 4x 4x 8x 32x 32x 31x 23x 19x 32x 32x 31x 32x 13x 13x 23x 23x 19x 23x 13x 10x 10x 10x 13x 32x 32x 32x 31x 32x 32x 32x 32x 32x 32x | /**
* Build "legacy" and "effective new" CSS models for a single SLDS2 component.
*
* - **Legacy model.** Flattened `src/slds2/<name>/<name>.css` converted to
* `{selector: [{prop, rawValue, hooksRead, terminalFallback, varChain}]}`.
* - **Effective new model.** Same shape, built from `themes/base.css`. In
* addition we gather per-theme hook assignments from the sibling
* `themes/<theme>.css` files.
*
* Two entry points are provided:
*
* - {@link buildLegacyModel} / {@link buildEffectiveNewModel} — filesystem
* variants used by the `sds-compliance` CLI.
* - {@link loadModelsFromStrings} — browser-safe variant that takes raw CSS
* strings (already `@import`-flattened by the caller or by the design-
* system-2 build). Used by tests and by any future in-browser audit path.
*/
import fs from 'node:fs/promises';
import path from 'node:path';
import {
flattenCss,
flattenCssString,
extractDeclModel,
extractHookAssignments,
extractMotionSignals,
} from './extract.js';
import type { EffectiveNewModel, FileModel, LegacyModel, ThemeAssignmentsBlock } from './types.js';
/** Tracked-theme list (exported so CLI and tests agree). */
export const TRACKED_THEMES = ['cosmos', 'lightning-blue'] as const;
export type TrackedTheme = (typeof TRACKED_THEMES)[number];
async function readIfExists(filePath: string): Promise<string | null> {
try {
return await fs.readFile(filePath, 'utf-8');
} catch {
return null;
}
}
async function buildFileModel(filePath: string): Promise<FileModel | null> {
const css = await readIfExists(filePath);
Iif (css === null) return null;
const root = await flattenCss(css, filePath);
return {
source: filePath,
declsBySelector: extractDeclModel(root),
assignmentsBySelector: extractHookAssignments(root),
motion: extractMotionSignals(root),
};
}
async function buildThemeAssignments(filePath: string): Promise<ThemeAssignmentsBlock | null> {
const css = await readIfExists(filePath);
if (css === null) return null;
const root = await flattenCss(css, filePath);
return {
source: filePath,
assignmentsBySelector: extractHookAssignments(root),
motion: extractMotionSignals(root),
};
}
/**
* Build the legacy model for a component.
*
* @param componentDir Absolute path to `src/slds2/<name>/`.
*/
export async function buildLegacyModel(componentDir: string): Promise<LegacyModel | null> {
const name = path.basename(componentDir);
const filePath = path.join(componentDir, `${name}.css`);
const model = await buildFileModel(filePath);
Iif (!model) return null;
return { name, ...model };
}
/**
* Build the effective-new model for a component — the `themes/base.css`
* declaration map plus each tracked theme's hook assignments.
*
* @param componentDir Absolute path to `src/slds2/<name>/`.
*/
export async function buildEffectiveNewModel(componentDir: string): Promise<EffectiveNewModel | null> {
const name = path.basename(componentDir);
const basePath = path.join(componentDir, 'themes', 'base.css');
const base = await buildFileModel(basePath);
Iif (!base) return null;
const themes: EffectiveNewModel['themes'] = {};
for (const theme of TRACKED_THEMES) {
const themePath = path.join(componentDir, 'themes', `${theme}.css`);
themes[theme] = await buildThemeAssignments(themePath);
}
return { name, base, themes };
}
/**
* Browser-safe model loader. Accepts raw CSS strings for a single component
* and returns the same {@link LegacyModel} / {@link EffectiveNewModel}
* shapes the CLI uses. Does not touch `fs` and does not resolve `@import` —
* the caller is expected to pass already-flattened CSS (the
* design-system-2 build writes flattened output next to the source, and
* the `sds-compliance` CLI always calls the fs-based loaders instead).
*
* `virtualPaths` control the `source` field in the returned models; it's
* metadata only and does not affect diffing.
*/
export async function loadModelsFromStrings(input: {
componentName: string;
legacyCss?: string | null;
baseCss?: string | null;
themeCss?: Partial<Record<string, string | null>>;
virtualPaths?: {
legacy?: string;
base?: string;
themes?: Partial<Record<string, string>>;
};
}): Promise<{ legacy: LegacyModel | null; effective: EffectiveNewModel | null }> {
const name = input.componentName;
const virt = input.virtualPaths ?? {};
const legacy: LegacyModel | null = input.legacyCss
? await buildLegacyFromString(name, input.legacyCss, virt.legacy)
: null;
const effective: EffectiveNewModel | null = input.baseCss
? await buildEffectiveFromStrings(name, input.baseCss, input.themeCss ?? {}, virt)
: null;
return { legacy, effective };
}
async function buildLegacyFromString(name: string, css: string, virtualPath?: string): Promise<LegacyModel> {
const root = await flattenCssString(css);
return {
name,
source: virtualPath ?? `virtual:/${name}/${name}.css`,
declsBySelector: extractDeclModel(root),
assignmentsBySelector: extractHookAssignments(root),
motion: extractMotionSignals(root),
};
}
async function buildEffectiveFromStrings(
name: string,
baseCss: string,
themeCss: Partial<Record<string, string | null>>,
virtualPaths: {
base?: string;
themes?: Partial<Record<string, string>>;
},
): Promise<EffectiveNewModel> {
const baseRoot = await flattenCssString(baseCss);
const base: FileModel = {
source: virtualPaths.base ?? `virtual:/${name}/themes/base.css`,
declsBySelector: extractDeclModel(baseRoot),
assignmentsBySelector: extractHookAssignments(baseRoot),
motion: extractMotionSignals(baseRoot),
};
const themes: EffectiveNewModel['themes'] = {};
for (const theme of TRACKED_THEMES) {
const css = themeCss[theme];
if (!css) {
themes[theme] = null;
continue;
}
const root = await flattenCssString(css);
themes[theme] = {
source: virtualPaths.themes?.[theme] ?? `virtual:/${name}/themes/${theme}.css`,
assignmentsBySelector: extractHookAssignments(root),
motion: extractMotionSignals(root),
};
}
return { name, base, themes };
}
/**
* Given an effective-new model, return the set of hooks that at least one
* tracked theme assigns on any selector.
*/
export function hooksAssignedByAnyTheme(effective: EffectiveNewModel): Set<string> {
const set = new Set<string>();
for (const themeBlock of Object.values(effective.themes ?? {})) {
if (!themeBlock) continue;
for (const hookMap of Object.values(themeBlock.assignmentsBySelector ?? {})) {
for (const hook of Object.keys(hookMap)) set.add(hook);
}
}
return set;
}
/**
* Given an effective-new model, return the set of hooks that every tracked
* theme assigns (intersection).
*/
export function hooksAssignedByAllThemes(effective: EffectiveNewModel): Set<string> {
const themeEntries = Object.entries(effective.themes ?? {}).filter(
(entry): entry is [string, ThemeAssignmentsBlock] => entry[1] !== null,
);
if (themeEntries.length === 0) return new Set();
let intersection: Set<string> | null = null;
for (const [, themeBlock] of themeEntries) {
const hooks = new Set<string>();
for (const hookMap of Object.values(themeBlock.assignmentsBySelector ?? {})) {
for (const hook of Object.keys(hookMap)) hooks.add(hook);
}
if (intersection === null) {
intersection = hooks;
} else {
const next = new Set<string>();
for (const h of hooks) Eif (intersection.has(h)) next.add(h);
intersection = next;
}
}
return intersection ?? new Set<string>();
}
/** Every hook read anywhere in the effective-new model (base only). */
export function hooksReadByBase(effective: EffectiveNewModel): Set<string> {
const set = new Set<string>();
Iif (!effective.base) return set;
for (const decls of effective.base.declsBySelector.values()) {
for (const decl of decls) for (const h of decl.hooksRead) set.add(h);
}
return set;
}
/** Every hook read anywhere in the legacy model. */
export function hooksReadByLegacy(legacy: LegacyModel | null): Set<string> {
const set = new Set<string>();
Iif (!legacy) return set;
for (const decls of legacy.declsBySelector.values()) {
for (const decl of decls) for (const h of decl.hooksRead) set.add(h);
}
return set;
}
|