Skip to content

Commit 3aeb486

Browse files
committed
Add fix for passed values in href
1 parent e008dbc commit 3aeb486

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/rules/__tests__/a11y-tooltip-interactive-trigger.test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,22 @@ ruleTester.run('non-interactive-tooltip-trigger', rule, {
5151
`import {Tooltip, Link} from '@primer/react';
5252
<Tooltip aria-label="Supplementary text" direction="e">
5353
<Link href="https://github.com">Link</Link>
54-
</Tooltip>`
54+
</Tooltip>`,
55+
`
56+
import {Tooltip, Link} from '@primer/react';
57+
<Tooltip aria-label={avatar.avatarName} direction="e">
58+
<Link href={avatar.avatarLink} underline={true}>
59+
User avatar
60+
</Link>
61+
</Tooltip>` ,
62+
`
63+
import {Tooltip, Link} from '@primer/react';
64+
<Tooltip aria-label="product" direction="e">
65+
<Link href={productLink}>
66+
Product
67+
</Link>
68+
</Tooltip>
69+
`
5570
],
5671
invalid: [
5772
{

src/rules/a11y-tooltip-interactive-trigger.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const {getPropValue, propName} = require('jsx-ast-utils')
12
const {isPrimerComponent} = require('../utils/is-primer-component')
23
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
34
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
@@ -21,11 +22,21 @@ const isAnchorTag = el => {
2122
return openingEl === 'a' || openingEl.toLowerCase() === 'link'
2223
}
2324

25+
const isJSXValue = (attributes) => {
26+
const node = attributes.find(attribute => propName(attribute) === 'href');
27+
const isJSXExpression = node.value.type === 'JSXExpressionContainer' && node
28+
&& typeof getPropValue(node) === 'string';
29+
30+
return isJSXExpression
31+
}
32+
2433
const isInteractiveAnchor = child => {
2534
const hasHref = getJSXOpeningElementAttribute(child.openingElement, 'href')
2635
if (!hasHref) return false
2736
const href = getJSXOpeningElementAttribute(child.openingElement, 'href').value.value
28-
const isAnchorInteractive = typeof href === 'string' && href !== ''
37+
const hasJSXValue = isJSXValue(child.openingElement.attributes);
38+
const isAnchorInteractive = (typeof href === 'string' && href !== '' || hasJSXValue)
39+
2940
return isAnchorInteractive
3041
}
3142

0 commit comments

Comments
 (0)