Skip to content

Commit e6bfd5c

Browse files
gorohashljharb
authored andcommittedAug 12, 2023
[New] anchor-has-content: Allow title attribute OR aria-label attribute
1 parent 7b3492b commit e6bfd5c

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed
 

‎__tests__/src/rules/anchor-has-content-test.js

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ ruleTester.run('anchor-has-content', rule, {
3636
code: '<Link>foo</Link>',
3737
settings: { 'jsx-a11y': { components: { Link: 'a' } } },
3838
},
39+
{ code: '<a title={title} />' },
40+
{ code: '<a aria-label={ariaLabel} />' },
41+
{ code: '<a title={title} aria-label={ariaLabel} />' },
3942
].map(parserOptionsMapper),
4043
invalid: [
4144
{ code: '<a />', errors: [expectedError] },

‎docs/rules/anchor-has-content.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
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.
88

9+
Alternatively, you may use the `title` prop or the `aria-label` prop.
10+
911
## Rule options
1012

1113
This rule takes one optional object argument of type object:
@@ -45,6 +47,8 @@ return (
4547
<a>Anchor Content!</a>
4648
<a><TextWrapper /></a>
4749
<a dangerouslySetInnerHTML={{ __html: 'foo' }} />
50+
<a title='foo' />
51+
<a aria-label='foo' />
4852
```
4953

5054
### Fail

‎src/rules/anchor-has-content.js

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// Rule Definition
88
// ----------------------------------------------------------------------------
99

10+
import { hasAnyProp } from 'jsx-ast-utils';
11+
1012
import getElementType from '../util/getElementType';
1113
import { arraySchema, generateObjSchema } from '../util/schemas';
1214
import hasAccessibleChild from '../util/hasAccessibleChild';
@@ -40,6 +42,9 @@ export default {
4042
if (hasAccessibleChild(node.parent, elementType)) {
4143
return;
4244
}
45+
if (hasAnyProp(node.attributes, ['title', 'aria-label'])) {
46+
return;
47+
}
4348

4449
context.report({
4550
node,

0 commit comments

Comments
 (0)
Please sign in to comment.