Skip to main content
interlace
Plugin: react-featuresRules

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

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

// 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

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

  • An element with a spread (<input {...rest} />) is reported even though the spread may carry the attribute.
  • type and name are only required on input, button, select and textarea.

Use this rule

npm install -D oxlint-plugin-react-features
.oxlintrc.json
{  "jsPlugins": ["oxlint-plugin-react-features"],  "rules": {    "react-features/required-attributes": "error"  }}
vite.config.ts
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.