Skip to content

Commit 1abfa34

Browse files
gorohashljharb
authored andcommitted
[New] anchor-has-content: Allow title attribute OR aria-label attribute
1 parent ecf7a27 commit 1abfa34

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ ruleTester.run('anchor-has-content', rule, {
3232
{ code: '<a>{foo.bar}</a>' },
3333
{ code: '<a dangerouslySetInnerHTML={{ __html: "foo" }} />' },
3434
{ code: '<a children={children} />' },
35+
{ code: '<a title={title} />' },
36+
{ code: '<a aria-label={ariaLabel} />' },
37+
{ code: '<a title={title} aria-label={ariaLabel} />' },
3538
].map(parserOptionsMapper),
3639
invalid: [
3740
{ code: '<a />', errors: [expectedError] },

docs/rules/anchor-has-content.md

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

33
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.
44

5+
Alternatively, you may use the `title` prop or the `aria-label` prop.
6+
57
## Rule details
68

79
This rule takes one optional object argument of type object:
@@ -41,6 +43,8 @@ return (
4143
<a>Anchor Content!</a>
4244
<a><TextWrapper /><a>
4345
<a dangerouslySetInnerHTML={{ __html: 'foo' }} />
46+
<a title='foo' />
47+
<a aria-label='foo' />
4448
```
4549

4650
### Fail

src/rules/anchor-has-content.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Rule Definition
88
// ----------------------------------------------------------------------------
99

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

@@ -37,6 +37,9 @@ module.exports = {
3737
if (hasAccessibleChild(node.parent)) {
3838
return;
3939
}
40+
if (hasAnyProp(node.attributes, ['title', 'aria-label'])) {
41+
return;
42+
}
4043

4144
context.report({
4245
node,

0 commit comments

Comments
 (0)