Skip to content

Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly #270

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 8 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/gorgeous-frogs-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-primer-react": major
---

Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ ESLint rules for Primer React
- [a11y-explicit-heading](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-explicit-heading.md)
- [a11y-link-in-text-block](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-link-in-text-block.md)
- [a11y-remove-disable-tooltip](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-remove-disable-tooltip.md)
- [a11y-use-next-tooltip](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-use-next-tooltip.md)
- [a11y-use-accessible-tooltip](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-use-accessible-tooltip.md)
47 changes: 47 additions & 0 deletions docs/rules/a11y-use-accessible-tooltip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Recommends to use the new accessible tooltip instead of the deprecated one.

## Rule details

This rule suggests switching to the new accessible tooltip from @primer/react instead of the deprecated version. Deprecated props like wrap, noDelay, and align should also be removed.

Note that the new tooltip is intended for interactive elements only, such as buttons and links, whereas the deprecated tooltip could be applied to any element, though it lacks screen reader accessibility. As a result, the autofix for this rule will only work if the deprecated tooltip is on an interactive element. If it is applied to a non-interactive element, please consult your design team for [an alternative approach](https://primer.style/guides/accessibility/tooltip-alternatives).

👎 Examples of **incorrect** code for this rule:

```jsx
import {Tooltip} from '@primer/react/deprecated'

const App = () => (
<Tooltip aria-label="This change cannot be undone" direction="w" wrap={true} noDelay={true} align="left">
<Button onClick={onClick}>Delete</Button>
</Tooltip>
)
```

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

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

const App = () => (
<Tooltip text="This change cannot be undone" direction="w">
<Button onClick={onClick}>Delete</Button>
</Tooltip>
)
```

## Icon buttons and tooltips

Even though the below code is perfectly valid, since icon buttons now come with tooltips by default, it is not required to explicitly use the Tooltip component on icon buttons.

```jsx
import {IconButton, Tooltip} from '@primer/react'

function ExampleComponent() {
return (
<Tooltip text="Search" direction="e">
<IconButton icon={SearchIcon} aria-label="Search" />
</Tooltip>
)
}
```
33 changes: 0 additions & 33 deletions docs/rules/a11y-use-next-tooltip.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
'primer-react/a11y-explicit-heading': 'error',
'primer-react/no-deprecated-props': 'warn',
'primer-react/a11y-remove-disable-tooltip': 'error',
'primer-react/a11y-use-next-tooltip': 'error',
'primer-react/a11y-use-accessible-tooltip': 'error',
'primer-react/no-unnecessary-components': 'error',
'primer-react/prefer-action-list-item-onselect': 'error',
'primer-react/enforce-css-module-identifier-casing': 'error',
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
'no-deprecated-props': require('./rules/no-deprecated-props'),
'a11y-link-in-text-block': require('./rules/a11y-link-in-text-block'),
'a11y-remove-disable-tooltip': require('./rules/a11y-remove-disable-tooltip'),
'a11y-use-next-tooltip': require('./rules/a11y-use-next-tooltip'),
'a11y-use-accessible-tooltip': require('./rules/a11y-use-accessible-tooltip'),
'use-deprecated-from-deprecated': require('./rules/use-deprecated-from-deprecated'),
'no-wildcard-imports': require('./rules/no-wildcard-imports'),
'no-unnecessary-components': require('./rules/no-unnecessary-components'),
Expand Down
59 changes: 59 additions & 0 deletions src/rules/__tests__/a11y-use-accessible-tooltip.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const rule = require('../a11y-use-accessible-tooltip')
const {RuleTester} = require('eslint')

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

ruleTester.run('a11y-use-accessible-tooltip', rule, {
valid: [`import {Tooltip} from '@primer/react';`],
invalid: [
// Single import from deprecated
{
code: `import {Tooltip} from '@primer/react/deprecated';`,
errors: [{messageId: 'useAccessibleTooltip'}],
output: `import {Tooltip} from '@primer/react';`,
},
// Multiple imports from deprecated
{
code: `import {Tooltip, Button} from '@primer/react/deprecated';`,
errors: [{messageId: 'useAccessibleTooltip'}],
output: `import {Button} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';`,
},
// Multiple imports from deprecated
{
code: `import {Button, Tooltip, Stack} from '@primer/react/deprecated';`,
errors: [{messageId: 'useAccessibleTooltip'}],
output: `import {Button, Stack} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';`,
},
// Single import from deprecated with an existing import from @primer/react
{
code: `import {Tooltip} from '@primer/react/deprecated';import {ActionList, ActionMenu} from '@primer/react';`,
errors: [{messageId: 'useAccessibleTooltip', line: 1}],
output: `import {ActionList, ActionMenu, Tooltip} from '@primer/react';`,
},
// Multiple imports from deprecated with an existing import from @primer/react
{
code: `import {Dialog, Tooltip} from '@primer/react/deprecated';import {ActionList, ActionMenu} from '@primer/react';`,
errors: [{messageId: 'useAccessibleTooltip', line: 1}],
output: `import {Dialog} from '@primer/react/deprecated';import {ActionList, ActionMenu, Tooltip} from '@primer/react';`,
},
{
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react/deprecated';\n<Tooltip aria-label="tooltip text" noDelay={true} wrap={true} align="left"><Button>Button</Button></Tooltip>`,
errors: [
{messageId: 'useAccessibleTooltip', 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/deprecated';\nimport {Tooltip} from '@primer/react';\n<Tooltip text="tooltip text" ><Button>Button</Button></Tooltip>`,
},
],
})
61 changes: 0 additions & 61 deletions src/rules/__tests__/a11y-use-next-tooltip.test.js

This file was deleted.

148 changes: 148 additions & 0 deletions src/rules/a11y-use-accessible-tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
'use strict'
const url = require('../url')
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 Tooltip component',
category: 'Best Practices',
recommended: true,
url: url(module),
},
fixable: true,
schema: [],
messages: {
useAccessibleTooltip: 'Please use @primer/react 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/deprecated') {
return
}
const hasTooltip = node.specifiers.some(
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
)

if (!hasTooltip) {
return
}

const hasOtherImports = node.specifiers.length > 1

const sourceCode = context.getSourceCode()
// Checking to see if there is an existing root (@primer/react) import
// Assuming there is one root import per file
const rootImport = sourceCode.ast.body.find(statement => {
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
})

const tooltipSpecifier = node.specifiers.find(
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
)

const hasRootImport = rootImport !== undefined

context.report({
node,
messageId: 'useAccessibleTooltip',
fix(fixer) {
const fixes = []
if (!hasOtherImports) {
// If Tooltip is the only import and no existing @primer/react import, replace the whole import statement
if (!hasRootImport) fixes.push(fixer.replaceText(node.source, `'@primer/react'`))
if (hasRootImport) {
// remove the entire import statement
fixes.push(fixer.remove(node))
// find the last specifier in the existing @primer/react import and insert Tooltip after that
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
}
} else {
// There are other imports from the deprecated bundle but no existing @primer/react import, so remove the Tooltip import and add a new import statement with the correct path.
const previousToken = sourceCode.getTokenBefore(tooltipSpecifier)
const nextToken = sourceCode.getTokenAfter(tooltipSpecifier)
const hasTrailingComma = nextToken && nextToken.value === ','
const hasLeadingComma = previousToken && previousToken.value === ','

let rangeToRemove

if (hasTrailingComma) {
rangeToRemove = [tooltipSpecifier.range[0], nextToken.range[1] + 1]
} else if (hasLeadingComma) {
rangeToRemove = [previousToken.range[0], tooltipSpecifier.range[1]]
} else {
rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]]
}
// Remove Tooltip from the import statement
fixes.push(fixer.removeRange(rangeToRemove))

if (!hasRootImport) {
fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`))
} else {
// find the last specifier in the existing @primer/react import and insert Tooltip after that
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
}
}
return fixes
},
})
},
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)
},
})
}
},
}
},
}
Loading