Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/cli/src/linter/linter/rules/orphaned-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,36 @@ describe('orphanedTokens', () => {
const findings = orphanedTokens(state);
expect(findings).toEqual([]);
});

it('does not flag unreferenced numeric palette scales', () => {
const state = buildState({
colors: {
primary: '#1A1C1E',
'green-50': '#e7f6ee',
'green-900': '#04361d',
},
components: {
button: { backgroundColor: '{colors.primary}' },
},
});

const findings = orphanedTokens(state);
expect(findings.find(d => d.path === 'colors.green-50')).toBeUndefined();
expect(findings.find(d => d.path === 'colors.green-900')).toBeUndefined();
});

it('still flags a lone numeric-suffix custom token', () => {
const state = buildState({
colors: {
primary: '#1A1C1E',
'brand-100': '#e7f6ee',
},
components: {
button: { backgroundColor: '{colors.primary}' },
},
});

const findings = orphanedTokens(state);
expect(findings.find(d => d.path === 'colors.brand-100')).toBeDefined();
});
});
28 changes: 28 additions & 0 deletions packages/cli/src/linter/linter/rules/orphaned-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,39 @@ const MD3_STANDARD_FAMILIES = new Set([
'outline',
]);

/**
* Identify numeric palette scales such as `green-50`, `green-500`, and
* `green-900`. Require at least two shades with the same prefix so isolated
* custom tokens like `brand-100` continue to be reported as orphaned.
*/
function numericPaletteFamilies(names: Iterable<string>): Set<string> {
const counts = new Map<string, number>();

for (const name of names) {
const match = /^(.+)-(\d{2,3})$/.exec(name);
if (!match) continue;

const family = match[1];
counts.set(family, (counts.get(family) ?? 0) + 1);
}

return new Set(
[...counts.entries()]
.filter(([, count]) => count >= 2)
.map(([family]) => family),
);
}
/**
* Orphaned tokens — tokens defined but never referenced by any component or
* any sibling token in the same MD3 family.
*/
export function orphanedTokens(state: DesignSystemState): RuleFinding[] {
if (state.components.size === 0) return [];

const paletteFamilies = numericPaletteFamilies(
[...state.colors.keys()],
);

const referencedPaths = new Set<string>();
for (const [, comp] of state.components) {
for (const [, value] of comp.properties) {
Expand Down Expand Up @@ -88,6 +114,8 @@ export function orphanedTokens(state: DesignSystemState): RuleFinding[] {
for (const [name] of state.colors) {
const path = `colors.${name}`;
if (referencedPaths.has(path)) continue;
const paletteFamily = /^(.+)-\d{2,3}$/.exec(name)?.[1];
if (paletteFamily && paletteFamilies.has(paletteFamily)) continue;
const family = colorFamily(name);
if (referencedFamilies.has(family)) continue;
if (MD3_STANDARD_FAMILIES.has(family)) continue;
Expand Down