-
Notifications
You must be signed in to change notification settings - Fork 10
Add a rule that warn against removing unsafeDisableTooltip
prop from IconButton
#185
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 all commits
3f0eb9b
6c28b38
bd1b014
9172bd6
0999c99
9e7a3b3
8d1bca4
bccebfd
9f538d4
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,5 @@ | ||
--- | ||
"eslint-plugin-primer-react": minor | ||
--- | ||
|
||
Add a rule that warns against removing `unsafeDisableTooltip` prop |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
## Rule Details | ||
|
||
This rule enforces to remove the `unsafeDisableTooltip` from `IconButton` component so that they have a tooltip by default. `unsafeDisableTooltip` prop is created for an incremental migration and should be removed once all icon buttons have a tooltip. | ||
|
||
👎 Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
import {IconButton} from '@primer/react' | ||
|
||
const App = () => ( | ||
<IconButton icon={SearchIcon} aria-label="Search" unsafeDisableTooltip /> | ||
// OR | ||
<IconButton icon={SearchIcon} aria-label="Search" unsafeDisableTooltip={true} /> | ||
// OR | ||
<IconButton icon={SearchIcon} aria-label="Search" unsafeDisableTooltip={false} /> // This is incorrect because it should be removed | ||
) | ||
``` | ||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
import {IconButton} from '@primer/react' | ||
|
||
const App = () => <IconButton icon={SearchIcon} aria-label="Search" /> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
const {RuleTester} = require('eslint') | ||
const rule = require('../a11y-remove-disable-tooltip') | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('a11y-remove-disable-tooltip', rule, { | ||
khiga8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
valid: [ | ||
`import {IconButton} from '@primer/react'; | ||
<IconButton icon={SearchIcon} aria-label="Search" />`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<IconButton icon={SearchIcon} aria-label="Search" unsafeDisableTooltip />`, | ||
output: `<IconButton icon={SearchIcon} aria-label="Search" />`, | ||
errors: [ | ||
{ | ||
messageId: 'removeDisableTooltipProp', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<IconButton icon={SearchIcon} aria-label="Search" unsafeDisableTooltip={true} />`, | ||
output: `<IconButton icon={SearchIcon} aria-label="Search" />`, | ||
errors: [ | ||
{ | ||
messageId: 'removeDisableTooltipProp', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<IconButton icon={SearchIcon} aria-label="Search" unsafeDisableTooltip={false} />`, | ||
output: `<IconButton icon={SearchIcon} aria-label="Search" />`, | ||
errors: [ | ||
{ | ||
messageId: 'removeDisableTooltipProp', | ||
}, | ||
], | ||
}, | ||
], | ||
}) | ||
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. (After we change the default value of unsafeDisableTooltip), Do you have a preference for 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. To add to this, is the plan to deprecate the prop in the future once we know 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.
I really like the idea of adding it to the linter! I always imagined that we would remove the prop instead of
This is the hope and the plan 🙌🏻 - however there might be some rare cases that we need to hide the tooltip and I guess we are yet to discover these cases as product teams test and remove 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.
Actually, I just realised that the rule checks |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute') | ||
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name') | ||
|
||
/** | ||
* @type {import('eslint').Rule.RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: 'error', | ||
docs: { | ||
description: | ||
'Icon buttons should have tooltip by default. Please remove `unsafeDisableTooltip` prop from `IconButton` component to enable the tooltip and help making icon button more accessible.', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
removeDisableTooltipProp: | ||
'Please remove `unsafeDisableTooltip` prop from `IconButton` component to enable the tooltip and help make icon button more accessible.', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
const openingElName = getJSXOpeningElementName(node) | ||
if (openingElName !== 'IconButton') { | ||
return | ||
} | ||
const unsafeDisableTooltip = getJSXOpeningElementAttribute(node, 'unsafeDisableTooltip') | ||
if (unsafeDisableTooltip !== undefined) { | ||
context.report({ | ||
node, | ||
messageId: 'removeDisableTooltipProp', | ||
fix(fixer) { | ||
const start = unsafeDisableTooltip.range[0] | ||
const end = unsafeDisableTooltip.range[1] | ||
return [ | ||
fixer.removeRange([start - 1, end]), // remove the space before unsafeDisableTooltip as well | ||
] | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Uh oh!
There was an error while loading. Please reload this page.