require-attrs
This rule enforces the use of elements with specified attributes in React/JSX.
How to use
// eslint.config.js (flat config)
import htmlReact from "@html-eslint/eslint-plugin-react";
export default [
{
files: ["**/*.jsx", "**/*.tsx"],
plugins: {
"@html-eslint/react": htmlReact,
},
rules: {
"@html-eslint/react/require-attrs": [
"error",
{ tag: "img", attr: "alt" },
],
},
},
];
Rule Details
This rule requires specified attributes to be present on matching elements. Only lowercase (native HTML) elements are checked; custom components are ignored.
Attributes that cannot be resolved statically are not reported. When an element has a spread attribute ({...props}), the check is skipped unless the attribute is written after the last spread, since a spread can add or override attributes at runtime:
<img {...props} /> {/* skipped: props may provide alt */}
<img alt="" {...props} /> {/* skipped: props may override alt */}
<img {...props} alt="" /> {/* checked: alt wins over props */}
Options
This rule takes an array of option objects:
tag(string, required): the HTML tag name to check.attr(string, required): the attribute name that must be present.value(string | boolean, optional): if specified, the attribute must have this exact value.- A string requires that exact attribute value.
truerequires the boolean form: a bare attribute (disabled) ordisabled={true}.falserequiresdisabled={false}.- Dynamic values (
disabled={someVar}) are not compared.
message(string, optional): custom error message.conditions(array, optional): conditions that must all be true before the attribute is enforced.
Examples of incorrect code for this rule:
<img />
<svg></svg>
Examples of correct code for this rule:
<img alt="" />
<svg viewBox="0 0 100 100"></svg>
With { tag: "input", attr: "disabled", value: true }:
<input />
<input disabled={false} />
<input disabled="true" />
<input disabled />
<input disabled={true} />