-
Notifications
You must be signed in to change notification settings - Fork 10
Add new rule a11y-no-title-usage
#316
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e55163d
Add new rule `a11y-no-title-usage`
TylerJDev 199637a
Small clean up
TylerJDev 8c05f27
Fix test, format
TylerJDev 8c38359
Add to index, recommended
TylerJDev c982ad6
Add alternative to docs
TylerJDev 295fa79
Add changeset
TylerJDev 0c278b7
Update docs/rules/a11y-no-title-usage.md
TylerJDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||
--- | ||
|
||
Add `a11y-no-title-usage` that warns against using `title` in some components |
This file contains hidden or 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,40 @@ | ||
## Rule Details | ||
|
||
This rule aims to prevent the use of the `title` attribute with some components from `@primer/react`. The `title` attribute is not keyboard accessible, which results in accessibility issues. Instead, we should utilize alternatives that are accessible. | ||
|
||
👎 Examples of **incorrect** code for this rule | ||
|
||
```jsx | ||
import {RelativeTime} from '@primer/react' | ||
|
||
const App = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={false} /> | ||
``` | ||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
import {RelativeTime} from '@primer/react' | ||
|
||
const App = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} /> | ||
``` | ||
|
||
The noTitle attribute is true by default, so it can be omitted. | ||
|
||
## With alternative tooltip | ||
|
||
If you want to still utilize a tooltip in a similar way to how the `title` attribute works, you can use the [Primer `Tooltip`](https://primer.style/components/tooltip/react/beta). If you use the `Tooltip` component, you must use it with an interactive element, such as with a button or a link. | ||
|
||
```jsx | ||
import {RelativeTime, Tooltip} from '@primer/react' | ||
|
||
const App = () => { | ||
const date = new Date('2020-01-01T00:00:00Z') | ||
return ( | ||
<Tooltip text={date.toString()}> | ||
<Link href="#"> | ||
<RelativeTime date={date} noTitle={true} /> | ||
</Link> | ||
</Tooltip> | ||
) | ||
} | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
const rule = require('../a11y-no-title-usage') | ||
const {RuleTester} = require('eslint') | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('a11y-no-title-usage', rule, { | ||
valid: [ | ||
`<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />`, | ||
`<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle />`, | ||
`<RelativeTime date={new Date('2020-01-01T00:00:00Z')} />`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={false} />`, | ||
output: `<RelativeTime date={new Date('2020-01-01T00:00:00Z')} />`, | ||
errors: [{messageId: 'noTitleOnRelativeTime'}], | ||
}, | ||
], | ||
}) |
This file contains hidden or 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,37 @@ | ||
const url = require('../url') | ||
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'error', | ||
docs: { | ||
description: 'Disallow usage of title attribute on some components', | ||
recommended: true, | ||
url: url(module), | ||
}, | ||
messages: { | ||
noTitleOnRelativeTime: 'Avoid using the title attribute on RelativeTime.', | ||
}, | ||
fixable: 'code', | ||
}, | ||
|
||
create(context) { | ||
return { | ||
JSXOpeningElement(jsxNode) { | ||
const title = getJSXOpeningElementAttribute(jsxNode, 'noTitle') | ||
|
||
if (title && title.value && title.value.expression && title.value.expression.value !== true) { | ||
context.report({ | ||
node: title, | ||
messageId: 'noTitleOnRelativeTime', | ||
fix(fixer) { | ||
const start = title.range[0] - 1 | ||
const end = title.range[1] | ||
return fixer.removeRange([start, end]) | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to have this example ❤️