Skip to main content
ESLint Interlace
Plugin: react-featuresRules

required-attributes

required-attributes rule

Keywords: React, accessibility, ESLint rule, required attributes, aria attributes, React props, auto-fix, LLM-optimized, React accessibility

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

AspectDetails
SeverityWarning (accessibility best practice)
Auto-Fixโœ… Yes (adds missing attributes)
CategoryReact / Accessibility
ESLint MCPโœ… Optimized for ESLint MCP integration
Best ForReact/Next.js applications, accessibility-focused projects

Rule Details

Ensures React components have required attributes (e.g., accessibility attributes, data attributes).

Configuration

OptionTypeDefaultDescription
attributesAttributeRule[][]List of required attributes
ignoreComponentsstring[][]Components to ignore globally

AttributeRule Object

PropertyTypeRequiredDescription
attributestringYesRequired attribute name
ignoreTagsstring[]NoTags to ignore for this attribute
messagestringNoCustom error message
suggestedValuestringNoSuggested value for auto-fix

Examples

โŒ Incorrect

Awaiting a tested example. The previous snippet was removed because the rule does not behave as the doc claimed; track the regression in benchmarks/FP_FN_REMEDIATION_TRACKER.md.

โœ… 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

IssueImpactSolution
โ™ฟ AccessibilityScreen readers can't interpretEnforce ARIA attributes
๐Ÿงช TestingHard to select elements in testsRequire data-testid
๐Ÿ“Š AnalyticsMissing tracking dataEnforce data attributes
๐ŸŽจ ConsistencyInconsistent attribute usageStandardize requirements

Comparison with Alternatives

Featurerequired-attributeseslint-plugin-jsx-a11yeslint-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

Further Reading

Known False Negatives

The following patterns are not detected due to static analysis limitations:

Dynamic Variable References

Why: Static analysis cannot trace values stored in variables or passed through function parameters.

// โŒ NOT DETECTED - Prop from variable
const propValue = computedValue;
<Component prop={propValue} /> // Computation not analyzed

Mitigation: Implement runtime validation and review code manually. Consider using TypeScript branded types for validated inputs.

Cross-Module Data Flow

Why: ESLint rules analyze one file at a time. Values imported from other modules cannot be traced.

// โŒ NOT DETECTED - Value from import
import { getValue } from './helpers';
processValue(getValue()); // Cross-file not tracked

Mitigation: Apply the same rule to imported modules. Use module boundaries and explicit exports.