Skip to content

Commit c859fa0

Browse files
committed
All tests pass
1 parent 214cd9f commit c859fa0

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,41 @@ ruleTester.run('a11y-link-in-text-block', rule, {
3333
<Link>Link level 1</Link>;
3434
`,
3535
`import {Heading, Link} from '@primer/react';
36-
<Heading>
37-
<Link>Link level 1</Link>
38-
</Heading>,
36+
<Heading>
37+
<Link>Link level 1</Link>
38+
</Heading>
3939
`,
4040
`import {Heading, Link} from '@primer/react';
4141
<Heading as="h2">
42-
<Link href={somePath}>
43-
Breadcrumb
44-
</Link>
45-
&nbsp;/ Create a thing
46-
</Heading>
42+
<Link href={somePath}>
43+
Breadcrumb
44+
</Link>
45+
Create a thing
46+
</Heading>
4747
`,
4848
`import {Link} from '@primer/react';
49+
<div>
4950
<h2>
5051
<Link href={somePath}>
5152
Breadcrumb
5253
</Link>
5354
</h2>
54-
&nbsp;/ Create a thing
55-
</Heading>
55+
Create a thing
56+
</div>
5657
`,
5758
`import {Link} from '@primer/react';
59+
<div>
5860
<Link href={somePath}>
5961
<SomeAvatar></SomeAvatar>
6062
</Link>
61-
last edited{' '}
63+
last edited
64+
</div>
6265
`,
6366
`import {Link} from '@primer/react';
6467
<span>
65-
by{' '}
68+
by
6669
<Link href="something" sx={{fontWeight: 'bold'}}>
67-
{listing.owner_login}
70+
Blah blah
6871
</Link>
6972
</span>
7073
`,

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ module.exports = {
2828
node.parent.children
2929
) {
3030
let siblings = node.parent.children
31+
const parentName = node.parent.openingElement?.name?.name
32+
const parentsToSkip = ['Heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
33+
if (parentsToSkip.includes(parentName)) return
3134
if (siblings.length > 0) {
3235
siblings = siblings.filter(childNode => {
3336
return (
@@ -46,7 +49,24 @@ module.exports = {
4649
const prevSibling = siblings[index - 1]
4750
const nextSibling = siblings[index + 1]
4851
if ((prevSibling && prevSibling.type === 'JSXText') || (nextSibling && nextSibling.type === 'JSXText')) {
52+
const sxAttribute = getJSXOpeningElementAttribute(node.openingElement, 'sx')
4953
const inlineAttribute = getJSXOpeningElementAttribute(node.openingElement, 'inline')
54+
55+
// Skip is Link child is a JSX element.
56+
const jsxElementChildren = node.children.filter(child => {
57+
return child.type === 'JSXElement'
58+
})
59+
if (jsxElementChildren.length > 0) return
60+
// Skip if fontWeight is set via the sx prop since that may be sufficient.
61+
if (
62+
sxAttribute &&
63+
sxAttribute?.value?.expression &&
64+
sxAttribute.value.expression.type === 'ObjectExpression' &&
65+
sxAttribute.value.expression.properties &&
66+
sxAttribute.value.expression.properties.length > 0 &&
67+
sxAttribute.value.expression.properties[0].key.name === 'fontWeight'
68+
)
69+
return
5070
if (inlineAttribute) {
5171
if (!inlineAttribute.value) {
5272
return

0 commit comments

Comments
 (0)