Skip to content

Commit 873d8c1

Browse files
CopilotTylerJDev
andcommitted
Update a11y-link-in-text-block rule to skip HTML anchors with ID attributes
Co-authored-by: TylerJDev <[email protected]>
1 parent a4ad769 commit 873d8c1

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

.changeset/spicy-pans-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-primer-react": patch
3+
---
4+
5+
Update a11y-link-in-text-block rule to ignore HTML anchor elements with ID attributes

docs/rules/a11y-link-in-text-block.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The lint rule will flag:
1818
There are certain edge cases that the linter skips to avoid false positives including:
1919

2020
- `<Link className="...">` or `<a className="...">` because there may be distinguishing styles applied.
21+
- `<a id="...">` because these may have a unique purpose or special behavior.
2122
- `<Link sx={{fontWeight:...}}>` or `<Link sx={{fontFamily:...}}>` because these technically may provide sufficient distinguishing styling.
2223
- `<Link>` or `<a>` where the only adjacent text is a period, since that can't really be considered a text block.
2324
- `<Link>` or `<a>` where the children is a JSX component, rather than a string literal, because then it might be an icon link rather than a text link.

src/rules/__tests__/a11y-link-in-text-block.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ ruleTester.run('a11y-link-in-text-block', rule, {
132132
`<p>
133133
<a href="/about" className="custom-link">About us</a>
134134
</p>`,
135+
`<p>
136+
<a href="/about" id="about-link">About us</a>
137+
</p>`,
135138
`<div>
136139
<a href="/contact"><CustomIcon /> Contact</a>
137140
</div>`,

src/rules/a11y-link-in-text-block.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ module.exports = {
8080
// Skip if anchor is nested inside of a heading
8181
if (parentsToSkip.includes(parentName)) return
8282

83-
// Skip if anchor has className (might have distinguishing styles)
83+
// Skip if anchor has className or id (might have distinguishing styles or unique purpose)
8484
const classNameAttribute = getJSXOpeningElementAttribute(node.openingElement, 'className')
85-
if (classNameAttribute) return
85+
const idAttribute = getJSXOpeningElementAttribute(node.openingElement, 'id')
86+
if (classNameAttribute || idAttribute) return
8687

8788
// Check for anchor in text block
8889
if (isNodeInTextBlock(node)) {

0 commit comments

Comments
 (0)