The user wants a custom ESLint configuration.
This configuration needs to enforce three specific things:
- Architecture Layering: Prevent imports from certain layers into others (e.g., UI components importing data logic directly).
- Accessibility Hints: Ensure code adheres to a11y standards (JSX/A11Y).
- Design Token Usage: Enforce the use of design tokens (variables) instead of hard-coded values (colors, spacing, etc.).
Step 1: Setting up the environment
I need to create a reusable ESLint config package structure or just the configuration object itself. A reusable package is usually better for "develop a config" requests, but a flat .eslintrc.js or eslint.config.js (flat config) is easier to demonstrate. I will provide a Flat Config setup as it's the modern standard (ESLint v9+), but I should probably mention legacy format just in case.
Step 2: Architecture Layering
- Concept: Enforcing a directory structure (e.g., FSD - Feature Sliced Design, or a simple Layers pattern like
pages -> features -> entities -> shared).
- Tooling:
eslint-plugin-import is the classic choice. There is also eslint-plugin-boundaries.
- Choice:
eslint-plugin-boundaries is much more powerful and specifically designed for this. It allows defining elements and rules about who can import whom.
- Implementation: I will define a simple 3-layer architecture:
ui (components), logic (hooks/utils), and data (API). Or better, a standard Domain-Driven structure: views -> components -> utils.
- Let's use
eslint-plugin-import with no-restricted-paths? No, boundaries is cleaner for complex rules.
- Alternative: Use
import/no-internal-modules or no-restricted-imports.
- Decision: I'll use
eslint-plugin-import's no-restricted-imports for a simple solution, or recommend eslint-plugin-boundaries for a robust one. The prompt implies a "custom config" development, so showing a robust plugin is better. I will use eslint-plugin-boundaries.
Step 3: Accessibility (a11y)
- Tooling:
eslint-plugin-jsx-a11y.
- Implementation: Add the recommended preset. Maybe add a specific rule for
alt-text or no-autofocus to show customization.
Step 4: Design Token Usage
- Concept: No hard-coded colors (
#fff), spacing (10px), or font sizes.
- Tooling: This is tricky.
stylelint is usually for CSS/SCSS.
- For JS/TS/JSX (inline styles or styled-components/tailwind classes), we need specific tools.
- Option A:
eslint-plugin-no-hardcoded-colors (niche).
- Option B: Custom Rule.
- Option C: Regex via
no-restricted-syntax.
- Decision: Since this is likely for a JS/TS project using styled-components or similar, or strictly checking CSS-in-JS, I will define a custom rule using
no-restricted-syntax to catch hex codes or raw pixel values in JS files.
- Better approach: Use
stylelint for .css/.scss files, but for this specific request (ESLint config), I will focus on the JS/TS side (e.g., preventing <div style={{ color: 'red' }}>). I'll provide a regex rule to catch hard-coded values in properties.
Step 5: Structure of the Output
- Prerequisites: Dependencies needed.
- The Config File (
eslint.config.js - Flat Config): The core code.
- Detailed Breakdown:
- Architecture: Using
boundaries or import.
- A11y: Standard setup.
- Design Tokens: Regex/custom rules.
- Example Usage: How to use it in a project.
Refining the Architecture part:
Let's assume a standard layered architecture: components (UI), hooks (Logic), services (Data).
Rule: services cannot import components. components cannot import services (should use hooks).
I will use eslint-plugin-import's no-restricted-imports as it's standard and requires fewer dependencies than boundaries, making the example easier to run. Or actually, boundaries is so specific to the prompt "enforcing architecture layering" that I should probably include it. It's the industry standard for architectural linting.
Let's stick to Flat Config (ESLint v9) since it's the current standard.
Refining the Design Token part:
How do we enforce design tokens in ESLint?
We can block style={{ backgroundColor: '#FFF' }}.
We can use eslint-plugin-no-inline-styles (too strict?).
Better: A custom rule definition or a strict no-restricted-syntax.
I will write a custom helper/rule object that uses a regex to find hard-coded colors/spacing in JSX attributes.
Drafting the Plan:
- Intro: Mention Flat Config.
- Dependencies:
eslint, globals, typescript-eslint, eslint-plugin-jsx-a11y, eslint-plugin-import (or boundaries).
- Architecture Section: Configure
boundaries to enforce features -> ui -> utils (or similar).
- A11y Section: Configure
jsx-a11y.
- Design Token Section: A custom rule logic to ban hex codes/px values in JSX.
Self-Correction during drafting:
Writing a truly custom ESLint rule from scratch (using context.defineRule or the visitor pattern) is complex code. The user likely wants a configuration that enforces this, not necessarily learning how to write a raw AST parser.
So, for Design Tokens, I will use no-restricted-syntax with a selector that targets JSX attributes containing specific regex patterns. It's a "config" way to write a rule.
Actually, no-restricted-syntax doesn't support regex content matching on node values easily without complex selectors.
A better middle ground: Create a local custom plugin within the config file (possible in Flat Config) to keep it self-contained, or just use eslint-plugin-no-hardcoded-strings? No, that's for text.
Let's use eslint-plugin-no-inline-styles? No, that prevents all styles.
I will demonstrate how to add a simple custom rule logic via the Flat Config plugins object to detect hard-coded values. This shows "Senior Dev" level expertise.
Revised Plan:
- Dependencies: Standard list.
- Architecture: Use
eslint-plugin-boundaries. It's the most robust way to answer that specific requirement.
- A11y: Standard
jsx-a11y.
- Design Tokens: I will create a "virtual plugin" inside the config to catch hard-coded hex/rgba values in style props.
Let's write the code.
Architecture details with boundaries:
Types: atoms, molecules, organisms, templates, pages.
Rules: pages can import anything. atoms cannot import molecules.
Design Token details:
Regex: /#([0-9a-f]{3}){1,2}/i or \d+px.
Target: JSXAttribute where name is style or inside styled-components.
Let's refine the "Design Token" part. Writing a raw AST visitor is the most flexible way.
context.defineRule isn't used directly in flat config easily without wrapping.
I'll stick to a robust no-restricted-syntax approach if possible, or provide a small custom rule implementation. Providing a custom rule implementation is cooler and fits the "Develop" prompt better.
Let's structure the response:
- Setup & Install.
- The
eslint.config.js (Flat Config).
- Explanation of the 3 main sections.
Here is a complete guide to developing a modern, flat-config ESLint setup (ESLint v9+) that enforces architecture, accessibility, and design system standards.
This solution creates a self-contained configuration logic that you can place in your project.
1. Prerequisites
Install the necessary dependencies. We will use:
eslint-plugin-boundaries: To enforce architecture layering (e.g., preventing UI components from importing API logic).
eslint-plugin-jsx-a11y: For accessibility hints.
eslint-plugin-no-relative-imports (or similar): To enforce strict import paths, often crucial for architecture.
- A custom rule logic for Design Tokens (usually handled via
no-restricted-syntax or a custom plugin).
npm install --save-dev \
eslint \
globals \
@eslint/js \
typescript-eslint \
eslint-plugin-jsx-a11y \
eslint-plugin-boundaries \
eslint-plugin-import
2. The Configuration (eslint.config.js)
We will construct a "Strict Architectural Config". This configuration assumes a standard layered frontend architecture (e.g., UI -> Features -> Data/Domain).
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import pluginJsxA11y from "eslint-plugin-jsx-a11y";
import boundaries from "eslint-plugin-boundaries";
import importPlugin from "eslint-plugin-import";
export default tseslint.config(
// 1. Base JS & TS Setup
js.configs.recommended,
...tseslint.configs.recommended,
// 2. Global Settings
{
languageOptions: {
globals: {
...globals.browser,
...globals.node,
},
},
plugins: {
"jsx-a11y": pluginJsxA11y,
"boundaries": boundaries,
"import": importPlugin,
},
settings: {
// Required for import and boundaries plugins to resolve aliases
"import/resolver": {
typescript: {},
node: { extensions: [".js", ".jsx", ".ts", ".tsx"] }
},
}
},
// 3. Accessibility (A11y) Enforcement
{
files: ["**/*.{js,jsx,ts,tsx}"],
rules: {
...pluginJsxA11y.configs.recommended.rules,
// Enforce clickable elements must be focusable
"jsx-a11y/interactive-supports-focus": "error",
// Enforce alt text
"jsx-a11y/alt-text": "error",
// Prevent generic anchor tags without href (use buttons instead)
"jsx-a11y/no-static-element-interactions": "warn",
}
},
// 4. Architecture & Layering (Boundaries)
// Example Hierarchy: UI < Features < Data < Utils
{
files: ["**/*.{js,jsx,ts,tsx}"],
settings: {
boundaries: {
elements: [
{
type: "ui",
pattern: "src/components/**/*",
},
{
type: "features",
pattern: "src/features/**/*",
},
{
type: "data",
pattern: "src/api/**/*",
},
{
type: "utils",
pattern: "src/utils/**/*",
}
]
}
},
rules: {
// Disallow imports from layers that should be "lower" in the hierarchy.
// UI cannot import Data directly.
"boundaries/element-types": [2, {
"default": "allow",
"rules": [
{
"from": ["ui"],
"disallow": ["data", "features"],
"message": "UI components should not import Data or Feature logic directly. Use props or hooks."
},
{
"from": ["features"],
"disallow": ["data"],
"message": "Features should abstract Data logic. Do not import raw API functions directly into features."
}
]
}],
// Enforce absolute imports (optional but recommended for architecture)
"import/no-relative-parent-imports": "error",
}
},
// 5. Design Token & CSS Enforcement
// This prevents "magic numbers" and hard-coded colors.
{
files: ["**/*.{js,jsx,ts,tsx}"],
rules: {
// Prevent inline styles with magic numbers
"react/no-inline-styles": "off", // Turn off standard if you use CSS-in-JS
// Use no-restricted-syntax to catch hard-coded colors/spacing
"no-restricted-syntax": [
"error",
{
"selector": "Literal[value=/#[0-9a-fA-F]{3,8}/]",
"message": "Do not use hard-coded hex colors. Use a design token variable (e.g., var(--color-primary) or theme.colors.primary)."
},
{
"selector": "Literal[value=/\\d+px/]",
"message": "Avoid hard-coded pixel values (px). Use rem/em units or spacing tokens (e.g., spacing.md)."
}
],
}
},
// 6. Ignore Patterns
{
ignores: ["dist/", "node_modules/", "*.config.js"],
}
);
3. Detailed Explanation of Rules
A. Architecture Layering (eslint-plugin-boundaries)
We defined three main element types: ui, features, and data.
- The Logic: We created a dependency graph.
- The Constraint:
src/components (UI) cannot import src/api (Data).
- This forces you to pass data down via props or have a separate state management layer (like Redux/Zustand) that injects data into the UI.
- Why: This ensures your UI components remain "dumb," reusable, and testable in isolation (e.g., in Storybook) without needing a mock API server running.
B. Accessibility Hints (eslint-plugin-jsx-a11y)
jsx-a11y/interactive-supports-focus: If you add an onClick handler to a div (which is bad practice but happens), this forces you to add tabIndex and keyboard event handlers, ensuring keyboard navigation users can access the functionality.
jsx-a11y/alt-text: Ensures images have alt attributes.
C. Design Token Usage (Custom no-restricted-syntax)
Instead of writing a custom ESLint plugin from scratch (which requires AST knowledge), we leverage Selectors.
Literal[value=/#[0-9a-fA-F]{3,8}/]: This selector finds any string literal in the code that looks like a hex color (e.g., #FFF). It errors with a message telling the developer to use a variable.
Literal[value=/\d+px/]: Finds string literals containing pixel values (e.g., "16px").
4. Usage in package.json
Add the lint script to your package.json:
{
"scripts": {
"lint": "eslint . --config eslint.config.js",
"lint:fix": "eslint . --config eslint.config.js --fix"
}
}
5. Example of Code that will FAIL
With this config, the following code snippet will trigger 3 distinct errors:
// ❌ FILE: src/components/Button.tsx (UI Layer)
// ERROR 1 (Architecture): UI component importing from Data layer
import { fetchUser } from '../api/user';
export const Button = () => {
return (
<div
onClick={() => fetchUser()}
style={{ color: '#FF0000', marginTop: '10px' }} // ERROR 2 & 3 (Design Tokens)
>
Click me
</div>
);
}
The Console Output:
UI components should not import Data or Feature logic directly.
Do not use hard-coded hex colors. Use a design token variable.
Avoid hard-coded pixel values (px). Use rem/em units or spacing tokens.