Skip to content

Add a11y-use-next-tooltip rule #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/rules/use-next-tooltip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Recommends to use the `next` tooltip instead of the current stable one.

## Rule details

This rule recommends using the tooltip that is imported from `@primer/react/next` instead of the main entrypoint. The components that are exported from `@primer/react/next` are recommended over the main entrypoint ones because `next` components are reviewed and remediated for accessibility issues.
👎 Examples of **incorrect** code for this rule:

```tsx
import {Tooltip} from '@primer/react'
```

👍 Examples of **correct** code for this rule:

```tsx
import {Tooltip} from '@primer/react/next'
```
1 change: 1 addition & 0 deletions src/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
'primer-react/new-color-css-vars': 'error',
'primer-react/a11y-explicit-heading': 'error',
'primer-react/new-color-css-vars-have-fallback': 'error',
'primer-react/use-next-tooltip': 'error',
},
settings: {
github: {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
'new-color-css-vars': require('./rules/new-color-css-vars'),
'a11y-explicit-heading': require('./rules/a11y-explicit-heading'),
'new-color-css-vars-have-fallback': require('./rules/new-color-css-vars-have-fallback'),
'use-next-tooltip': require('./rules/use-next-tooltip'),
},
configs: {
recommended: require('./configs/recommended'),
Expand Down
61 changes: 61 additions & 0 deletions src/rules/__tests__/use-next-tooltip.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const rule = require('../use-next-tooltip')
const {RuleTester} = require('eslint')

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
})

ruleTester.run('use-next-tooltip', rule, {
valid: [
`import {Tooltip} from '@primer/react/experimental';`,
`import {UnderlineNav, Button} from '@primer/react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need this line of code too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not 😆 I was just adding examples to replicate real life cases but if it is confusing or you think it doesn't seem very related, I can totally remove it.

import {Tooltip} from '@primer/react/experimental';`,
`import {UnderlineNav, Button} from '@primer/react';
import {Tooltip, SelectPanel} from '@primer/react/experimental';`,
],
invalid: [
{
code: `import {Tooltip} from '@primer/react';`,
errors: [{messageId: 'useNextTooltip'}],
output: `import {Tooltip} from '@primer/react/experimental';`,
},
{
code: `import {Tooltip, Button} from '@primer/react';\n<Tooltip aria-label="tooltip text"><Button>Button</Button></Tooltip>`,
errors: [{messageId: 'useNextTooltip'}, {messageId: 'useTextProp'}],
output: `import {Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/experimental';\n<Tooltip text="tooltip text"><Button>Button</Button></Tooltip>`,
},
{
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react';\n<Tooltip aria-label="tooltip text"><Button>Button</Button></Tooltip>`,
errors: [
{messageId: 'useNextTooltip', line: 1},
{messageId: 'useTextProp', line: 2},
],
output: `import {ActionList, ActionMenu, Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/experimental';\n<Tooltip text="tooltip text"><Button>Button</Button></Tooltip>`,
},
{
code: `import {ActionList, ActionMenu, Button, Tooltip} from '@primer/react';\n<Tooltip aria-label="tooltip text"><Button aria-label="test">Button</Button></Tooltip>`,
errors: [
{messageId: 'useNextTooltip', line: 1},
{messageId: 'useTextProp', line: 2},
],
output: `import {ActionList, ActionMenu, Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/experimental';\n<Tooltip text="tooltip text"><Button aria-label="test">Button</Button></Tooltip>`,
},
{
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react';\n<Tooltip aria-label="tooltip text" noDelay={true} wrap={true} align="left"><Button>Button</Button></Tooltip>`,
errors: [
{messageId: 'useNextTooltip', line: 1},
{messageId: 'useTextProp', line: 2},
{messageId: 'noDelayRemoved', line: 2},
{messageId: 'wrapRemoved', line: 2},
{messageId: 'alignRemoved', line: 2},
],
output: `import {ActionList, ActionMenu, Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/experimental';\n<Tooltip text="tooltip text" ><Button>Button</Button></Tooltip>`,
},
],
})
126 changes: 126 additions & 0 deletions src/rules/use-next-tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict'
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'recommends the use of @primer/react/experimental Tooltip component',
category: 'Best Practices',
recommended: true,
},
fixable: true,
schema: [],
messages: {
useNextTooltip: 'Please use @primer/react/experimental Tooltip component that has accessibility improvements',
useTextProp: 'Please use the text prop instead of aria-label',
noDelayRemoved: 'noDelay prop is removed. Tooltip now has no delay by default',
wrapRemoved: 'wrap prop is removed. Tooltip now wraps by default',
alignRemoved: 'align prop is removed. Please use the direction prop instead.',
},
},
create(context) {
return {
ImportDeclaration(node) {
if (node.source.value !== '@primer/react') {
return
}
const hasTooltip = node.specifiers.some(
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
)

const hasOtherImports = node.specifiers.length > 1
if (!hasTooltip) {
return
}
context.report({
node,
messageId: 'useNextTooltip',
fix(fixer) {
// If Tooltip is the only import, replace the whole import statement
if (!hasOtherImports) {
return fixer.replaceText(node.source, `'@primer/react/experimental'`)
} else {
// Otherwise, remove Tooltip from the import statement and add a new import statement with the correct path
const tooltipSpecifier = node.specifiers.find(
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
)

const tokensToRemove = [' ', ',']
const tooltipIsFirstImport = tooltipSpecifier === node.specifiers[0]
const tooltipIsLastImport = tooltipSpecifier === node.specifiers[node.specifiers.length - 1]
const tooltipIsNotFirstOrLastImport = !tooltipIsFirstImport && !tooltipIsLastImport

const sourceCode = context.getSourceCode()
const canRemoveBefore = tooltipIsNotFirstOrLastImport
? false
: tokensToRemove.includes(sourceCode.getTokenBefore(tooltipSpecifier).value)
const canRemoveAfter = tokensToRemove.includes(sourceCode.getTokenAfter(tooltipSpecifier).value)
const start = canRemoveBefore
? sourceCode.getTokenBefore(tooltipSpecifier).range[0]
: tooltipSpecifier.range[0]
const end = canRemoveAfter
? sourceCode.getTokenAfter(tooltipSpecifier).range[1] + 1
: tooltipSpecifier.range[1]
return [
// remove tooltip specifier and the space and comma after it
fixer.removeRange([start, end]),
fixer.insertTextAfterRange(
[node.range[1], node.range[1]],
`\nimport {Tooltip} from '@primer/react/experimental';`,
),
]
}
},
})
},
JSXOpeningElement(node) {
const openingElName = getJSXOpeningElementName(node)
if (openingElName !== 'Tooltip') {
return
}
const ariaLabel = getJSXOpeningElementAttribute(node, 'aria-label')
if (ariaLabel !== undefined) {
context.report({
node,
messageId: 'useTextProp',
fix(fixer) {
return fixer.replaceText(ariaLabel.name, 'text')
},
})
}
const noDelay = getJSXOpeningElementAttribute(node, 'noDelay')
if (noDelay !== undefined) {
context.report({
node,
messageId: 'noDelayRemoved',
fix(fixer) {
return fixer.remove(noDelay)
},
})
}
const wrap = getJSXOpeningElementAttribute(node, 'wrap')
if (wrap !== undefined) {
context.report({
node,
messageId: 'wrapRemoved',
fix(fixer) {
return fixer.remove(wrap)
},
})
}
const align = getJSXOpeningElementAttribute(node, 'align')
if (align !== undefined) {
context.report({
node,
messageId: 'alignRemoved',
fix(fixer) {
return fixer.remove(align)
},
})
}
},
}
},
}