Skip to content

Commit 57dab5d

Browse files
committed
Add additional check to make sure accessible name fully contains visible label
1 parent de77ce7 commit 57dab5d

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/rules/no-generic-link-text.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ module.exports = {
2626
if (elementType(node) !== 'a') return
2727
if (getProp(node.attributes, 'aria-labelledby')) return
2828

29+
let cleanTextContent // text content we can reliably fetch
30+
31+
const parent = node.parent
32+
if (parent.children && parent.children.length > 0 && parent.children[0].type === 'JSXText') {
33+
cleanTextContent = stripAndDowncaseText(parent.children[0].value)
34+
}
35+
2936
const ariaLabel = getPropValue(getProp(node.attributes, 'aria-label'))
3037
const cleanAriaLabelValue = ariaLabel && stripAndDowncaseText(ariaLabel)
3138

@@ -37,11 +44,15 @@ module.exports = {
3744
'Avoid setting generic link text like `Here`, `Click here`, `Read more`. Make sure that your link text is both descriptive and concise.'
3845
})
3946
}
47+
if (cleanTextContent && !cleanAriaLabelValue.includes(cleanTextContent)) {
48+
context.report({
49+
node,
50+
message: 'When using ARIA to set a more descriptive text, it must fully contain the visible label.'
51+
})
52+
}
4053
} else {
41-
const parent = node.parent
42-
if (parent.children && parent.children.length === 1 && parent.children[0].type === 'JSXText') {
43-
const textContent = stripAndDowncaseText(parent.children[0].value)
44-
if (!bannedLinkText.includes(textContent)) return
54+
if (cleanTextContent) {
55+
if (!bannedLinkText.includes(cleanTextContent)) return
4556
context.report({
4657
node,
4758
message:

tests/no-generic-link-text.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ ruleTester.run('no-generic-link-text', rule, {
2626
{code: '<a>Learn more.</a>;', errors: [{message: errorMessage}]},
2727
{code: "<a aria-label='read more!!!'></a>;", errors: [{message: errorMessage}]},
2828
{code: "<a aria-label='click here.'></a>;", errors: [{message: errorMessage}]},
29-
{code: "<a aria-label='Here'></a>;", errors: [{message: errorMessage}]}
29+
{code: "<a aria-label='Here'></a>;", errors: [{message: errorMessage}]},
30+
{
31+
code: "<a aria-label='Does not include visible label'>Read more!</a>;",
32+
errors: [
33+
{
34+
message:
35+
'When setting ARIA to provide a more descriptive accessible name, it must fully include the visible label content.'
36+
}
37+
]
38+
}
3039
]
3140
})

0 commit comments

Comments
 (0)