required-attributes
required-attributes rule
Enforce required attributes on React components with customizable ignore lists. This rule is part of eslint-plugin-react-features and provides LLM-optimized error messages with fix suggestions.
๐ก Provides suggestions | ๐ง Automatically fixable
Quick Summary
| Aspect | Details |
|---|---|
| Severity | Warning (accessibility best practice) |
| Auto-Fix | โ Yes (adds missing attributes) |
| Category | React / Accessibility |
| ESLint MCP | โ Optimized for ESLint MCP integration |
| Best For | React/Next.js applications, accessibility-focused projects |
Rule Details
Ensures React components have required attributes (e.g., accessibility attributes, data attributes).
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
attributes | AttributeRule[] | [] | List of required attributes |
ignoreComponents | string[] | [] | Components to ignore globally |
AttributeRule Object
| Property | Type | Required | Description |
|---|---|---|---|
attribute | string | Yes | Required attribute name |
ignoreTags | string[] | No | Tags to ignore for this attribute |
message | string | No | Custom error message |
suggestedValue | string | No | Suggested value for auto-fix |
Examples
โ Incorrect
// With { attributes: [{ attribute: 'data-testid' }] }
<button onClick={handleClick}>Click me</button>
<Card title="Profile" />Nothing is required by default. Each configured attribute is required on every
JSX element (intrinsic, component or member) unless ignoreTags names the tag
for that attribute or ignoreComponents names the element. React's Fragment
accepts no attribute but key, so nothing is required of it.
โ Correct
// With required attributes
<button onClick={handleClick} aria-label="Submit form">
Click me
</button>
<img src="/photo.jpg" alt="Profile photo" />Configuration Examples
Accessibility Enforcement
{
rules: {
'react-features/required-attributes': ['error', {
attributes: [
{
attribute: 'alt',
ignoreTags: ['div', 'span'],
message: 'Images must have alt text for accessibility',
suggestedValue: ''
},
{
attribute: 'aria-label',
ignoreTags: ['div', 'p'],
message: 'Interactive elements need aria-label'
}
],
ignoreComponents: ['Icon', 'Logo']
}]
}
}Data Tracking Attributes
{
rules: {
'react-features/required-attributes': ['warn', {
attributes: [
{
attribute: 'data-testid',
message: 'Add data-testid for E2E testing',
suggestedValue: 'element-name'
},
{
attribute: 'data-analytics',
ignoreTags: ['div', 'span'],
message: 'Trackable elements need data-analytics'
}
]
}]
}
}Why This Matters
| Issue | Impact | Solution |
|---|---|---|
| โฟ Accessibility | Screen readers can't interpret | Enforce ARIA attributes |
| ๐งช Testing | Hard to select elements in tests | Require data-testid |
| ๐ Analytics | Missing tracking data | Enforce data attributes |
| ๐จ Consistency | Inconsistent attribute usage | Standardize requirements |
Comparison with Alternatives
| Feature | required-attributes | eslint-plugin-jsx-a11y | eslint-plugin-react |
|---|---|---|---|
| Custom Attributes | โ Yes | โ No | โ No |
| Auto-Fix | โ Yes | โ ๏ธ Limited | โ No |
| LLM-Optimized | โ Yes | โ No | โ No |
| ESLint MCP | โ Optimized | โ No | โ No |
| Configurable Rules | โ Yes | โ ๏ธ Limited | โ ๏ธ Limited |
Related Rules
img-requires-alt(planned) - Specific img alt enforcementreact-no-inline-functions- React performance optimizationreact-class-to-hooks- React modernization
Further Reading
- WCAG 2.1 Guidelines - Web accessibility guidelines
- React Accessibility - React accessibility guide
- ARIA Attributes - ARIA documentation
- ESLint MCP Setup - Enable AI assistant integration
Known False Negatives
- An element with a spread (
<input {...rest} />) is reported even though the spread may carry the attribute. typeandnameare only required oninput,button,selectandtextarea.
Use this rule
npm install -D oxlint-plugin-react-features{ "jsPlugins": ["oxlint-plugin-react-features"], "rules": { "react-features/required-attributes": "error" }}import { defineConfig } from 'vite-plus';export default defineConfig({ lint: { jsPlugins: ['oxlint-plugin-react-features'], rules: { 'react-features/required-attributes': 'error', }, },});oxlint-plugin-react-features re-exports eslint-plugin-react-features/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.
Did this rule catch something? Star the repo to get new CWE coverage as we ship it โ or follow the AI-code-security benchmarks behind these rules.