Skip to content
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

Add a11y-use-next-tooltip rule #103

Merged
merged 19 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
broccolinisoup marked this conversation as resolved.
Show resolved Hide resolved
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'
```
39 changes: 39 additions & 0 deletions src/rules/__tests__/use-next-tooltip.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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/next';`,
`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/next';`,
`import {UnderlineNav, Button} from '@primer/react';
import {Tooltip, SelectPanel} from '@primer/react/next';`,
],
invalid: [
{
code: `import {Tooltip} from '@primer/react';`,
errors: [{messageId: 'useNextTooltip'}],
output: `import {Tooltip} from '@primer/react/next';`,
},
{
code: `import {Tooltip, Button} from '@primer/react';`,
errors: [{messageId: 'useNextTooltip'}],
output: `import {Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/next';`,
},
{
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react';`,
errors: [{messageId: 'useNextTooltip'}],
output: `import {ActionList, ActionMenu, Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/next';`,
},
],
})
60 changes: 60 additions & 0 deletions src/rules/use-next-tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'recommends the use of @primer/react/next Tooltip component',
category: 'Best Practices',
recommended: true,
},
fixable: true,
schema: [],
messages: {
useNextTooltip: 'Please use @primer/react/next Tooltip component that has accessibility improvements',
},
},
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.some(
specifier => specifier.imported && specifier.imported.name !== 'Tooltip',
)
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/next'`)
} 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',
)
return [
// remove tooltip specifier and the space and comma after it
fixer.removeRange([tooltipSpecifier.range[0], tooltipSpecifier.range[1] + 2]),
// fixer.remove(tooltipSpecifier),
fixer.insertTextAfterRange(
[node.range[1], node.range[1]],
`\nimport {Tooltip} from '@primer/react/next';`,
),
]
}
},
})
},
}
},
}