Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow title attribute or aria-label attribute instead of accessible child in the "anchor-has-content" rule #727

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions __tests__/src/rules/anchor-has-content-test.js
Expand Up @@ -36,6 +36,9 @@ ruleTester.run('anchor-has-content', rule, {
code: '<Link>foo</Link>',
settings: { 'jsx-a11y': { components: { Link: 'a' } } },
},
{ 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
Expand Up @@ -6,6 +6,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 options

This rule takes one optional object argument of type object:
Expand Down Expand Up @@ -45,6 +47,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: 5 additions & 0 deletions src/rules/anchor-has-content.js
Expand Up @@ -7,6 +7,8 @@
// Rule Definition
// ----------------------------------------------------------------------------

import { hasAnyProp } from 'jsx-ast-utils';

import getElementType from '../util/getElementType';
import { arraySchema, generateObjSchema } from '../util/schemas';
import hasAccessibleChild from '../util/hasAccessibleChild';
Expand Down Expand Up @@ -40,6 +42,9 @@ export default {
if (hasAccessibleChild(node.parent, elementType)) {
return;
}
if (hasAnyProp(node.attributes, ['title', 'aria-label'])) {
return;
}

context.report({
node,
Expand Down