Skip to content

Commit ff93739

Browse files
committed
Allow title and aria-label attributes in the anchor-has-content rule
1 parent 36102cd commit ff93739

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ 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} aria-label={ariaLabel} />' },
3536
].map(parserOptionsMapper),
3637
invalid: [
3738
{ code: '<a />', errors: [expectedError] },
3839
{ code: '<a><Bar aria-hidden /></a>', errors: [expectedError] },
3940
{ code: '<a>{undefined}</a>', errors: [expectedError] },
41+
{ code: '<a title={title} />', errors: [expectedError] },
42+
{ code: '<a aria-label={ariaLabel} />', errors: [expectedError] },
4043
].map(parserOptionsMapper),
4144
});

docs/rules/anchor-has-content.md

Lines changed: 3 additions & 0 deletions
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` and `aria-label` props.
6+
57
## Rule details
68

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

4649
### Fail

src/rules/anchor-has-content.js

Lines changed: 4 additions & 1 deletion
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, hasEveryProp } 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 (hasEveryProp(node.attributes, ['title', 'aria-label'])) {
41+
return;
42+
}
4043

4144
context.report({
4245
node,

0 commit comments

Comments
 (0)