anchor-is-valid
Configuration
Rule Details
This rule enforces that <a> elements (and any configured custom anchor
components) function as proper hyperlinks. An anchor without a usable
href, with a non-navigable href such as "", "#", or javascript:…,
or used purely for click handling, is reported.
The rule runs three independent aspects, all enabled by default:
- noHref — reports when no usable href is provided.
- invalidHref — reports when the
hrefvalue is empty, the literal"#", or matches^\W*?javascript:(e.g."javascript:void(0)"). - preferButton — when an
onClickis present, reports that the element should be a<button>instead of an anchor.
A spread attribute ({...props}, including a literal-object spread
without a usable href) suppresses the noHref and preferButton reports
when no explicit href is found, because the spread may carry one at
runtime.
When more than one aspect would trigger on the same element,
preferButton takes precedence over invalidHref (and preferButton
takes precedence over noHref in the no-href branch).
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Rule Options
The rule accepts an options object with the following properties:
components— array of additional component names (besides the built-ina) that should be checked.specialLink— array of additional prop names (besides the built-inhref) that should be treated as href-like for the purposes of this rule.aspects— array of which sub-checks to run; one or more ofnoHref,invalidHref,preferButton. When omitted, all three are active.
Examples of incorrect code with { "components": ["Anchor", "Link"] }:
Examples of incorrect code with { "specialLink": ["hrefLeft"] }:
Examples of incorrect code with { "aspects": ["noHref"] }:
With aspects set to ["noHref"], an anchor that carries an onClick
but no href is still reported as noHref — preferButton is off, so
the onClick does not redirect the report.