Skip to content

Commit

Permalink
Clean up regex
Browse files Browse the repository at this point in the history
  • Loading branch information
khiga8 committed Mar 23, 2023
1 parent 381f2bc commit bff637e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 4 additions & 2 deletions docs/rules/a11y-aria-label-is-well-formatted.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

## Rule Details

`[aria-label]` content should be formatted in the same way you would visual text.
`[aria-label]` content should be formatted in the same way you would visual text. Please use sentence case.

Please use sentence case, and avoid using hyphens like you would an ID.
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 👎
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/a11y-aria-label-is-well-formatted.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ module.exports = {
if (propValue.type !== 'Literal') return

const ariaLabel = propValue.value
if (ariaLabel.match(/^[a-z]+[a-z\-\s]*$/)) {
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 and make sure you are not using hyphens.',
message: '[aria-label] text should be formatted the same as you would visual text. Use sentence case.',
})
}
},
Expand Down
6 changes: 4 additions & 2 deletions tests/a11y-aria-label-is-well-formatted.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ const ruleTester = new RuleTester({
},
})

const errorMessage =
'[aria-label] text should be formatted the same as you would visual text. Use sentence case and make sure you are not using hyphens.'
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}]},
],
})

0 comments on commit bff637e

Please sign in to comment.