-
Notifications
You must be signed in to change notification settings - Fork 17
CSS Variable Linter
jsandland edited this page Jul 31, 2025
·
1 revision
Automatically enforces the use of CSS variables from :root instead of hardcoded values in CSS files. This ensures consistency across our design system and makes theme changes easier to manage.
- Blocks commits until violations are fixed
- Staged files only - only checks files being committed, not the entire codebase
- Clear feedback - shows exact file, line, and suggested fixes
- Automatic replacement of violations with CSS variables
- Batch processing - fixes multiple issues in a single command
- Safe operation - preserves file structure and formatting
-
Color values: Hex (
#fff,#ffffff), RGB, RGBA, HSL, named colors - Spacing values: Pixels, rem, em, percentages, viewport units
-
Shorthand properties:
background:,margin:,padding:, etc. -
Multiple values: Handles
padding: 8px 16px;with multiple replacements
/* β Before */
color: #ffffff;
background: #000;
border-color: #E9E9E9;
/* β
After */
color: var(--color-white);
background: var(--color-black);
border-color: var(--color-light-gray);/* β Before */
margin-top: 16px;
padding: 8px 16px;
gap: 24px;
/* β
After */
margin-top: var(--spacing-300);
padding: var(--spacing-100) var(--spacing-300);
gap: var(--spacing-400);npm run lint:css-varsnpm run lint:css-vars:fix# 1. Make changes to CSS files
git add express/code/features/table-of-contents-seo/table-of-contents-seo.css
# 2. Try to commit (pre-commit hook runs)
git commit -m "Update TOC styles"
# 3. If violations found, auto-fix
npm run lint:css-vars:fix
# 4. Re-stage and commit
git add express/code/features/table-of-contents-seo/table-of-contents-seo.css
git commit -m "Update TOC styles"β CSS Variable Linting Issues Found in Staged Files:
π express/code/features/table-of-contents-seo/table-of-contents-seo.css:110
Found: main .toc-content a { padding: 8px 16px; }
Replace:
8px β var(--spacing-100)
16px β var(--spacing-300)
π express/code/features/table-of-contents-seo/table-of-contents-seo.css:144
Found: .toc-container { background: #ffffff; }
Use: var(--color-white)
Total issues: 2
π‘ Update these hardcoded colors and spacing to use CSS variables from :root
π Applying fixes...
β
Fixes applied. Please re-stage your files.
-
scripts/lint-css-vars.js- Main linting script with auto-fix capability -
.husky/pre-commit- Pre-commit hook configuration
-
package.json- Added lint scripts:-
lint:css-vars- Check for violations -
lint:css-vars:fix- Auto-fix violations
-
- Ensures all colors and spacing use design system variables
- Prevents inconsistent hardcoded values across the codebase
- Maintains visual consistency across components
- Makes theme changes easier by centralizing values in
:root - Reduces manual search-and-replace when updating design tokens
- Provides clear audit trail of design system usage
- Prevents hardcoded values from reaching production
- Catches violations early in the development process
- Ensures design system compliance
- Clear, actionable feedback with exact file locations
- Auto-fix capability reduces manual work
- Pre-commit enforcement prevents forgotten fixes
-
Colors: All
--color-*variables fromexpress/code/styles/styles.css -
Spacing: All
--spacing-*variables fromexpress/code/styles/styles.css
-
Colors:
color,background-color,border-color,fill,stroke,background,border -
Spacing:
margin,padding,gap,top,right,bottom,left,width,height,min-width,max-width,min-height,max-height
-
Hex normalization:
#fffβ#ffffffβ#FFF - Case insensitive: Handles mixed case variations
- Whitespace handling: Trims and normalizes spacing
-
Complex values: Extracts colors from
background: #fff url(image.png) no-repeat;
None - this is an additive feature that only affects the development workflow.
The linter has been tested with:
- Various color formats (hex, rgb, rgba, hsl, named colors)
- Shorthand and longhand CSS properties
- Multiple values in single properties
- Edge cases like whitespace and case variations
- Existing code will continue to work normally
- The linter only affects new commits
- Use
npm run lint:css-vars:fixto automatically fix existing violations - No manual migration required
This linter ensures our CSS follows the design system consistently and makes theme changes easier to manage across the entire codebase.