-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and ma…
…ke the changes accordingly (#270) * Update the tooltip eslint rule as we now export the new one from main bundle and the old one from deprecated * oops * just make the code a lil better * Create gorgeous-frogs-cover.md * reuse rootImport and fix method * Update .changeset/gorgeous-frogs-cover.md * improve the rule docs so that it can be used as a migration guide
- Loading branch information
1 parent
90134bc
commit 17814a2
Showing
10 changed files
with
262 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`, | ||
}, | ||
], | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Oops, something went wrong.