Skip to content

Commit

Permalink
Allow title and aria-label attributes in the anchor-has-content rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gorohash committed May 3, 2021
1 parent 36102cd commit ff93739
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions __tests__/src/rules/anchor-has-content-test.js
Expand Up @@ -32,10 +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} 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),
});
3 changes: 3 additions & 0 deletions docs/rules/anchor-has-content.md
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` and `aria-label` props.

## Rule details

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

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

import { elementType } from 'jsx-ast-utils';
import { elementType, hasEveryProp } 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 (hasEveryProp(node.attributes, ['title', 'aria-label'])) {
return;
}

context.report({
node,
Expand Down

0 comments on commit ff93739

Please sign in to comment.