Skip to content

Commit 3449de2

Browse files
Add a new error code for icon buttons that are wrapped with tooltip
1 parent d651470 commit 3449de2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/rules/__tests__/a11y-use-next-tooltip.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ ruleTester.run('a11y-use-next-tooltip', rule, {
2525
errors: [{messageId: 'useNextTooltip'}],
2626
output: `import {Tooltip} from '@primer/react/next';`,
2727
},
28+
{
29+
code: `import {Tooltip, IconButton} from '@primer/react';\n<Tooltip aria-label="some text"><IconButton aria-label="some-text" /></Tooltip>`,
30+
errors: [
31+
{messageId: 'useNextTooltip', line: 1},
32+
{messageId: 'iconButtonsHaveDefaultTooltip', line: 2},
33+
{messageId: 'useTextProp', line: 2},
34+
],
35+
output: `import {IconButton} from '@primer/react';\nimport {Tooltip} from '@primer/react/next';\n<Tooltip text="some text"><IconButton aria-label="some-text" /></Tooltip>`,
36+
},
2837
{
2938
code: `import {Tooltip, Button} from '@primer/react';\n<Tooltip aria-label="tooltip text"><Button>Button</Button></Tooltip>`,
3039
errors: [{messageId: 'useNextTooltip'}, {messageId: 'useTextProp'}],

src/rules/a11y-use-next-tooltip.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ module.exports = {
1313
fixable: true,
1414
schema: [],
1515
messages: {
16-
useNextTooltip: 'Please use @primer/react/next Tooltip component that has accessibility improvements',
16+
useNextTooltip:
17+
'Please use @primer/react/next Tooltip component that has accessibility improvements. Please refer to the migration documentation for more information.',
1718
useTextProp: 'Please use the text prop instead of aria-label',
1819
noDelayRemoved: 'noDelay prop is removed. Tooltip now has no delay by default',
1920
wrapRemoved: 'wrap prop is removed. Tooltip now wraps by default',
2021
alignRemoved: 'align prop is removed. Please use the direction prop instead.',
22+
iconButtonsHaveDefaultTooltip:
23+
'IconButton components come with a default tooltip. You may be able to remove the wrapper Tooltip component completely. Please refer to the documentation for more information.',
2124
},
2225
},
2326
create(context) {
@@ -121,6 +124,24 @@ module.exports = {
121124
})
122125
}
123126
},
127+
ExpressionStatement(node) {
128+
if (!Array.isArray(node.expression.children)) {
129+
return
130+
}
131+
132+
const hasIconbutton = node.expression.children.some(
133+
child => child.type === 'JSXElement' && child.openingElement.name.name === 'IconButton',
134+
)
135+
136+
if (!hasIconbutton) {
137+
return
138+
}
139+
140+
context.report({
141+
node,
142+
messageId: 'iconButtonsHaveDefaultTooltip',
143+
})
144+
},
124145
}
125146
},
126147
}

0 commit comments

Comments
 (0)