diff --git a/packages/cli/src/linter/linter/rules/orphaned-tokens.test.ts b/packages/cli/src/linter/linter/rules/orphaned-tokens.test.ts index fb3777d..021f9f1 100644 --- a/packages/cli/src/linter/linter/rules/orphaned-tokens.test.ts +++ b/packages/cli/src/linter/linter/rules/orphaned-tokens.test.ts @@ -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(); + }); }); diff --git a/packages/cli/src/linter/linter/rules/orphaned-tokens.ts b/packages/cli/src/linter/linter/rules/orphaned-tokens.ts index 55697f6..4443c2d 100644 --- a/packages/cli/src/linter/linter/rules/orphaned-tokens.ts +++ b/packages/cli/src/linter/linter/rules/orphaned-tokens.ts @@ -53,6 +53,28 @@ 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): Set { + const counts = new Map(); + + 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. @@ -60,6 +82,10 @@ const MD3_STANDARD_FAMILIES = new Set([ export function orphanedTokens(state: DesignSystemState): RuleFinding[] { if (state.components.size === 0) return []; + const paletteFamilies = numericPaletteFamilies( + [...state.colors.keys()], + ); + const referencedPaths = new Set(); for (const [, comp] of state.components) { for (const [, value] of comp.properties) { @@ -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;