Accessibility (A11y) Thresholds: Checking contrast ratios (especially for colors) and other a11y constraints.
Semantic Mappings: Ensuring primitive tokens are mapped to semantic tokens correctly (or that semantic tokens use valid primitives).
The output of this pipeline is a package published to an NPM registry.
High-Level Architecture:
The pipeline will likely run in a CI/CD environment (like GitHub Actions).
It needs a source of truth for tokens (JSON, YAML, or token studio files).
It needs tools to parse and validate these tokens.
Key Tools:
Style Dictionary: The industry standard for transforming tokens, but it doesn't have built-in "linting" rules in the strict sense, though it can validate JSON schemas.
Stylelint: A CSS linter that has plugins for design tokens (e.g., stylelint-design-tokens). This is good for usage in CSS but maybe not for the raw JSON token files themselves if they aren't CSS.
Theo (Salesforce): Another token engine.
Specific Token Linters: There are newer tools like token-transformer, design-tokens-lint, or custom scripts using Style Dictionary's pre-processing hooks.
Custom Scripts/Schemas: JSON Schema is a strong candidate for structural and naming validation.
WCAG Validation: Libraries like color or wcag-contrast for checking contrast.
Pipeline Stages:
Install & Setup: Get dependencies.
Token Source: Load JSON/YAML files.
Validation Stage 1: Naming Convention:
How? Regular expressions or JSON Schema.
Example: color.background.primary.default.
Validation Stage 2: Accessibility (Contrast):
This is tricky. To check contrast, you need a foreground and a background.
Self-validation: A token file usually defines one color. You can't validate contrast of a single token.
Pair validation: You need to define pairs (e.g., "Button Text" vs "Button Background").
Alternative: Validation that semantic text colors meet AA standards against specific background colors defined elsewhere.
Validation Stage 3: Semantic Mapping:
Ensuring color-text-primary references color-blue-900 (a primitive) and not a raw hex code (usually) or another semantic token (unless allowed).
Detecting "magic numbers" or raw values in semantic layers.
Build/Transform: Use Style Dictionary to generate CSS/JS/Swift/Kotlin.
Publish: NPM publish.
Drafting the Plan:
Phase 1: Tooling Selection
Recommend Style Dictionary as the core engine because it's extensible.
Recommend JSON Schema for structure/naming.
Recommend a custom Node.js script for A11y checks (contrast).
Refining the Accessibility Part:
Checking accessibility in a raw token file is the hardest part. Most design token files are flat or nested objects.
If I have:
color-bg-primary: #fffcolor-text-primary: #000
The linter needs to know these go together.
Solution: Create a "Context" or "Usage" file that maps semantic tokens to specific accessibility requirements. OR, enforce a naming convention where a background implies a text color (brittle).
Better Solution: Use a specific "Composition" layer in the token structure that the linter reads to verify contrast.
Refining the Semantic Mapping:
Goal: Prevent hardcoding hex codes in semantic tokens.
Logic: If token path includes semantic, its value must be { ... } (reference), not #hex.
Structuring the Response:
Architecture Overview: Visualizing the flow.
Tool Stack: What to use.
Implementation Details:
Naming (JSON Schema).
Semantic (Custom Style Dictionary Filter/Formatter or Script).
A11y (Custom Script using wcag-contrast).
CI/CD Pipeline (YAML): The actual automation.
Publishing Strategy: NPM setup.
Self-Correction during drafting:
Don't just say "use a linter." Be specific. stylelint works great for CSS variables, but for source JSON tokens, a custom Node script or ajv (JSON Schema validator) is better for strict control.
I will recommend Style Dictionary with custom preprocessors or a separate "lint" script that runs before the build.
Detailed Plan Content:
Naming: Use ct (Design Tokens Community Group) spec structure? Or standard kebab-case. Let's suggest Regex via JSON Schema.
Semantic: Distinguish "Primitive" (Global) vs "Semantic" (Alias). Validate that Semantic only references Primitive.
A11y: This needs a "Usage Schema". You can't validate contrast in isolation. I will propose a a11y-pairs.json config file that the pipeline consumes.
Let's write the response.
A robust design token pipeline ensures that your design system is consistent, accessible, and developer-friendly before it ever reaches the codebase.
Here is a comprehensive plan for a Design Token Linting Pipeline, focusing on three pillars: Naming, Accessibility, and Semantic Mapping.
1. Architecture Overview
We will use Style Dictionary as the core build engine, extended with custom logic. While tools like stylelint are great for CSS, we need to validate the source of truth (JSON/YAML) before compilation.
Input: JSON/YAML token files.
Core Engine: Style Dictionary (with custom pre-processors and file headers).
Validation Libraries:
ajv (JSON Schema Validator) for structure/naming.
color2k or polished for color math.
wcag-contrast for accessibility math.
Output: Validated artifacts ready for NPM.
2. Phase 1: Naming Conventions (Governance)
We need to enforce a strict taxonomy (e.g., [prefix]-[namespace]-[system]-[variant]) to prevent ad-hoc naming like blue-darker-2.
Implementation Strategy:
Define a JSON Schema that enforces naming patterns using Regular Expressions. We will use a pre-build script to validate all tokens against this schema.
You cannot guarantee accessibility just by looking at a color; you must look at color pairs. Since tokens are often defined in isolation, we need to define "Usage Contexts" to validate contrast.
Strategy:
Primitives: Validate that color values are valid hex/rgba.
Pairs (The "A11y Contract"): Create a configuration file that defines which background token expects which text token.
Example Config (a11y-contracts.json):
json
[
{
"background": "bg-primary-default",
"foreground": "text-on-primary",
"expectedRatio": 4.5 // WCAG AA
},
{
"background": "bg-secondary-default",
"foreground": "text-on-secondary",
"expectedRatio": 3.0 // Large text allowance or specific UI
}
]
This ensures that semantic tokens (e.g., color-text-link) correctly reference primitive tokens (e.g., color-blue-500) and do not contain "Magic Values" (raw hex codes).
Strategy:
We inspect the value property of semantic tokens.
Enforce Referencing: Semantic tokens must reference a primitive using the {path.to.token} syntax.
Enforce Layering:
Primitives Folder: Can contain raw values (#FF0000).
Semantic Folder: Must contain references only ({color.red.500}). If a raw hex is found here, the pipeline fails.
The Validation Script:
javascript
const rawHexRegex = /#[0-9a-f]{3,6}/i;
const referenceRegex = /^{.+}$/;
function validateSemantics(tokenObj, path = '') {
for (const [key, value] of Object.entries(tokenObj)) {
const currentPath = path ? `${path}.${key}` : key;
if (value.value) {
// If we are in the 'semantic' folder, check for raw values
if (currentPath.includes('semantic') && rawHexRegex.test(value.value)) {
console.error(`❌ Hardcoded Value found in Semantic token: ${currentPath}`);
console.error(` Value: ${value.value}. Use a reference like {color.primitive.blue}`);
process.exit(1);
}
// Ensure references point to existing keys (Style Dictionary usually handles this,
// but we can fail early here)
} else if (typeof value === 'object') {
validateSemantics(value, currentPath);
}
}
}
5. The CI/CD Pipeline (GitHub Actions)
We wrap the above scripts into a unified pipeline that runs on Pull Requests and before NPM publishing.
This pipeline moves the burden of manual QA to automated checks, ensuring that when a developer pulls your NPM package, the tokens are mathematically accessible and syntactically correct.