-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from 10 commits
ded3726
7d7c94b
21a0497
27c7e24
613795b
cfca999
924cdc4
b584b8a
ec3ca30
4f7c1d1
4435db4
be1595e
aaf53d2
773698b
d651470
0b3f2f7
1d187b9
5002f33
1bfbc8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' | ||
``` |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we need this line of code too? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>`, | ||
}, | ||
], | ||
}) |
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) | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Uh oh!
There was an error while loading. Please reload this page.