-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule to flag invalid aria-label format (#418)
- Loading branch information
Showing
6 changed files
with
126 additions
and
22 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
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,39 @@ | ||
# [aria-label] text should be formatted as you would visual text (`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 visual text. Please use sentence case. | ||
|
||
Do not connect the words like you would an ID. An `aria-label` is not an ID, and should be formatted as human-friendly text. | ||
|
||
## Resources | ||
|
||
- [Using aria-label](https://www.w3.org/WAI/tutorials/forms/labels/#using-aria-label) | ||
|
||
## 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,31 @@ | ||
const {getProp} = require('jsx-ast-utils') | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: '[aria-label] text should be formatted as you would visual text.', | ||
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]+.*$/)) { | ||
context.report({ | ||
node, | ||
message: '[aria-label] text should be formatted the same as you would visual text. Use sentence case.', | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,31 @@ | ||
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 visual text. Use sentence case.' | ||
|
||
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: "<a aria-label='Valid'></a>;"}, | ||
{code: "<a aria-label='VALID'></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='submit.yml'></a>;", errors: [{message: errorMessage}]}, | ||
{code: "<a aria-label='this-is-not-an-id'></a>;", errors: [{message: errorMessage}]}, | ||
], | ||
}) |