Skip to content

Commit 21c4540

Browse files
committed
Format
1 parent 67629e9 commit 21c4540

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Additionally, HTML anchor elements (`<a>`) in text blocks should be converted to
1111
Related: [WCAG 1.4.1 Use of Color issues](https://www.w3.org/WAI/WCAG21/Understanding/use-of-color.html)
1212

1313
The lint rule will flag:
14+
1415
- Any `<Link>` without the `inline` property (equal to `true`) detected with string nodes on either side.
1516
- Any HTML `<a>` elements detected within a text block, with an autofix to convert them to `Link` components.
1617

@@ -110,7 +111,11 @@ import {Link} from '@primer/react'
110111
function ExampleComponent() {
111112
return (
112113
<p>
113-
Learn more about <Link href="https://github.com/pricing" inline>GitHub plans</Link> and pricing options.
114+
Learn more about{' '}
115+
<Link href="https://github.com/pricing" inline>
116+
GitHub plans
117+
</Link>{' '}
118+
and pricing options.
114119
</p>
115120
)
116121
}

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ module.exports = {
2828
},
2929
create(context) {
3030
const sourceCode = context.sourceCode ?? context.getSourceCode()
31-
31+
3232
// Helper function to check if a node is in a text block
33-
const isNodeInTextBlock = (node) => {
33+
const isNodeInTextBlock = node => {
3434
let siblings = node.parent.children
3535
if (!siblings || siblings.length === 0) return false
36-
36+
3737
// Filter out whitespace nodes
3838
siblings = siblings.filter(childNode => {
3939
return (
@@ -46,17 +46,17 @@ module.exports = {
4646
!(childNode.type === 'Literal' && /^\s+$/.test(childNode.value))
4747
)
4848
})
49-
49+
5050
const index = siblings.findIndex(childNode => {
5151
return childNode.range === node.range
5252
})
53-
53+
5454
const prevSibling = siblings[index - 1]
5555
const nextSibling = siblings[index + 1]
56-
56+
5757
const prevSiblingIsText = prevSibling && prevSibling.type === 'JSXText'
5858
const nextSiblingIsText = nextSibling && nextSibling.type === 'JSXText'
59-
59+
6060
// If there's text on either side
6161
if (prevSiblingIsText || nextSiblingIsText) {
6262
// Skip if the only text adjacent to the link is a period
@@ -65,55 +65,49 @@ module.exports = {
6565
}
6666
return true
6767
}
68-
68+
6969
return false
7070
}
71-
71+
7272
return {
7373
JSXElement(node) {
7474
const name = getJSXOpeningElementName(node.openingElement)
7575
const parentName = node.parent.openingElement?.name?.name
7676
const parentsToSkip = ['Heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
77-
77+
7878
// Check for HTML anchor elements
79-
if (
80-
isHTMLElement(node.openingElement) &&
81-
name === 'a' &&
82-
node.parent.children
83-
) {
79+
if (isHTMLElement(node.openingElement) && name === 'a' && node.parent.children) {
8480
// Skip if anchor is nested inside of a heading
8581
if (parentsToSkip.includes(parentName)) return
86-
82+
8783
// Skip if anchor has className (might have distinguishing styles)
8884
const classNameAttribute = getJSXOpeningElementAttribute(node.openingElement, 'className')
8985
if (classNameAttribute) return
90-
86+
9187
// Check for anchor in text block
9288
if (isNodeInTextBlock(node)) {
9389
// Skip if anchor child is a JSX element
9490
const jsxElementChildren = node.children.filter(child => child.type === 'JSXElement')
9591
if (jsxElementChildren.length > 0) return
96-
92+
9793
// Report and autofix
9894
context.report({
9995
node,
10096
messageId: 'htmlAnchorInTextBlock',
10197
fix(fixer) {
10298
// Get all attributes from the anchor to transfer to Link
103-
const attributes = node.openingElement.attributes
104-
.map(attr => sourceCode.getText(attr))
105-
.join(' ')
106-
99+
const attributes = node.openingElement.attributes.map(attr => sourceCode.getText(attr)).join(' ')
100+
107101
// Create the Link component opening and closing tags
108102
const openingTag = `<Link ${attributes}>`
109103
const closingTag = '</Link>'
110-
104+
111105
// Apply fixes to the opening and closing tags
112106
const openingFix = fixer.replaceText(node.openingElement, openingTag)
113107
const closingFix = fixer.replaceText(node.closingElement, closingTag)
114-
108+
115109
return [openingFix, closingFix]
116-
}
110+
},
117111
})
118112
}
119113
}

0 commit comments

Comments
 (0)