Skip to content

Commit

Permalink
[New] anchor-has-content: Allow title attribute OR aria-label attri…
Browse files Browse the repository at this point in the history
…bute
  • Loading branch information
gorohash authored and ljharb committed Jul 6, 2020
1 parent ecf7a27 commit 1abfa34
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions __tests__/src/rules/anchor-has-content-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ ruleTester.run('anchor-has-content', rule, {
{ code: '<a>{foo.bar}</a>' },
{ code: '<a dangerouslySetInnerHTML={{ __html: "foo" }} />' },
{ code: '<a children={children} />' },
{ code: '<a title={title} />' },
{ code: '<a aria-label={ariaLabel} />' },
{ code: '<a title={title} aria-label={ariaLabel} />' },
].map(parserOptionsMapper),
invalid: [
{ code: '<a />', errors: [expectedError] },
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/anchor-has-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforce that anchors have content and that the content is accessible to screen readers. Accessible means that it is not hidden using the `aria-hidden` prop. Refer to the references to learn about why this is important.

Alternatively, you may use the `title` prop or the `aria-label` prop.

## Rule details

This rule takes one optional object argument of type object:
Expand Down Expand Up @@ -41,6 +43,8 @@ return (
<a>Anchor Content!</a>
<a><TextWrapper /><a>
<a dangerouslySetInnerHTML={{ __html: 'foo' }} />
<a title='foo' />
<a aria-label='foo' />
```

### Fail
Expand Down
5 changes: 4 additions & 1 deletion src/rules/anchor-has-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Rule Definition
// ----------------------------------------------------------------------------

import { elementType } from 'jsx-ast-utils';
import { elementType, hasAnyProp } from 'jsx-ast-utils';
import { arraySchema, generateObjSchema } from '../util/schemas';
import hasAccessibleChild from '../util/hasAccessibleChild';

Expand Down Expand Up @@ -37,6 +37,9 @@ module.exports = {
if (hasAccessibleChild(node.parent)) {
return;
}
if (hasAnyProp(node.attributes, ['title', 'aria-label'])) {
return;
}

context.report({
node,
Expand Down

0 comments on commit 1abfa34

Please sign in to comment.