Skip to content

Commit

Permalink
Allow title attribute OR aria-label attribute in the anchor-has-conte…
Browse files Browse the repository at this point in the history
…nt rule
  • Loading branch information
gorohash committed May 3, 2021
1 parent ff93739 commit 2a36fa5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions __tests__/src/rules/anchor-has-content-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ 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] },
{ code: '<a><Bar aria-hidden /></a>', errors: [expectedError] },
{ code: '<a>{undefined}</a>', errors: [expectedError] },
{ code: '<a title={title} />', errors: [expectedError] },
{ code: '<a aria-label={ariaLabel} />', errors: [expectedError] },
].map(parserOptionsMapper),
});
5 changes: 3 additions & 2 deletions docs/rules/anchor-has-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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` and `aria-label` props.
Alternatively, you may use the `title` prop or the `aria-label` prop.

## Rule details

Expand Down Expand Up @@ -43,7 +43,8 @@ return (
<a>Anchor Content!</a>
<a><TextWrapper /><a>
<a dangerouslySetInnerHTML={{ __html: 'foo' }} />
<a title='foo' aria-label='foo' />
<a title='foo' />
<a aria-label='foo' />
```

### Fail
Expand Down
4 changes: 2 additions & 2 deletions 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, hasEveryProp } 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,7 +37,7 @@ module.exports = {
if (hasAccessibleChild(node.parent)) {
return;
}
if (hasEveryProp(node.attributes, ['title', 'aria-label'])) {
if (hasAnyProp(node.attributes, ['title', 'aria-label'])) {
return;
}

Expand Down

0 comments on commit 2a36fa5

Please sign in to comment.