-
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.
Add a rule that warn against removing
unsafeDisableTooltip
prop fro…
…m `IconButton` (#185) * add a rule to remove unsafeDisableTooltip prop * add changeset * Update src/rules/a11y-remove-disable-tooltip.js Co-authored-by: Kate Higa <16447748+khiga8@users.noreply.github.com> * unsafeDisableTooltip={false} is also invalid - the prop should be removed. * export the rule - duh --------- Co-authored-by: Kate Higa <16447748+khiga8@users.noreply.github.com>
- Loading branch information
1 parent
2ff10af
commit fea642d
Showing
7 changed files
with
130 additions
and
0 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": minor | ||
--- | ||
|
||
Add a rule that warns against removing `unsafeDisableTooltip` prop |
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,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" /> | ||
``` |
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,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, { | ||
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', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
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 @@ | ||
'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 | ||
] | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |