-
Notifications
You must be signed in to change notification settings - Fork 58
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
New rule to flag invalid aria-label format #418
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c0e476
New rule to flag invalid aria-label format
khiga8 9614192
Update tests
khiga8 ede8536
Update docs and preset
khiga8 d286fa5
Update README
khiga8 a083b52
Fix lint
khiga8 440f6f3
Rename rule
khiga8 980be83
Update text
khiga8 381f2bc
Run prettier
khiga8 bff637e
Clean up regex
khiga8 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 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,37 @@ | ||
# [aria-label] text should be formatted as you would visible text, in a human-readable format (`github/a11y-aria-label-is-well-formatted`) | ||
|
||
💼 This rule is enabled in the ⚛️ `react` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Rule Details | ||
|
||
`[aria-label]` content should be formatted in the same way you would visible text. | ||
|
||
Please use sentence case, and avoid using hyphens like you would an ID. | ||
|
||
## Resources | ||
|
||
## Examples | ||
|
||
### **Incorrect** code for this rule 👎 | ||
|
||
```html | ||
<a href="..." aria-label="learn more"></a> | ||
``` | ||
|
||
```html | ||
<a href="..." aria-label="go-to-link"></a> | ||
``` | ||
|
||
### **Correct** code for this rule 👍 | ||
|
||
```html | ||
<a href="..." aria-label="Learn more"></a> | ||
``` | ||
|
||
```html | ||
<a href="..." aria-label="Homepage"></a> | ||
``` | ||
|
||
## Version |
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,32 @@ | ||
const {getProp} = require('jsx-ast-utils') | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: '[aria-label] text should be formatted as you would visible text, in a human-readable format.', | ||
url: require('../url')(module), | ||
}, | ||
schema: [], | ||
}, | ||
|
||
create(context) { | ||
return { | ||
JSXOpeningElement: node => { | ||
const prop = getProp(node.attributes, 'aria-label') | ||
if (!prop) return | ||
|
||
const propValue = prop.value | ||
if (propValue.type !== 'Literal') return | ||
|
||
const ariaLabel = propValue.value | ||
if (ariaLabel.match(/^[a-z]+[a-z\-\s]*$/)) { | ||
context.report({ | ||
node, | ||
message: | ||
'[aria-label] text should be formatted the same as you would visible text. Use sentence case and make sure you are not using hyphens.', | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,29 @@ | ||
const rule = require('../lib/rules/a11y-aria-label-is-well-formatted') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
const errorMessage = | ||
'[aria-label] text should be formatted the same as you would visible text. Use sentence case and make sure you are not using hyphens.' | ||
|
||
ruleTester.run('a11y-aria-label-is-well-formatted', rule, { | ||
valid: [ | ||
{code: "<a aria-labelledby='someId' href='#'>Read more</a>;"}, | ||
{code: "<a aria-label={someName} href='#'>Read more</a>;"}, | ||
{code: "<a aria-label='This is a label'></a>;"}, | ||
{code: '<Link aria-label="Valid" href="#">Read more</Link>'}, | ||
], | ||
invalid: [ | ||
{code: "<a aria-label='close modal'></a>;", errors: [{message: errorMessage}]}, | ||
{code: "<a aria-label='submit'></a>;", errors: [{message: errorMessage}]}, | ||
{code: "<a aria-label='this-is-not-an-id'></a>;", errors: [{message: errorMessage}]}, | ||
], | ||
}) |
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.
this auto-generator is so cool!