-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Add lint rule for Spinner component #90
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
aubreyquinn
merged 8 commits into
microsoft:main
from
Harsh-Modi278:user/harshmodi/SpinnerLintRule
Sep 17, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e247009
add rule for spinner
Harsh-Modi278 fe58ceb
update docs
Harsh-Modi278 1b1cf03
logic change
Harsh-Modi278 9dc2270
Merge branch 'main' into user/harshmodi/SpinnerLintRule
Harsh-Modi278 121e235
add to index.js recommended
Harsh-Modi278 d0c6f55
address comments: aria-busy, label
Harsh-Modi278 c5525e8
refactor hasNonEmptyPro logic
Harsh-Modi278 c3a18ba
Merge branch 'main' into user/harshmodi/SpinnerLintRule
Harsh-Modi278 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
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,89 @@ | ||
# Accessibility: Spinner must have either aria-label or label, aria-live and aria-busy attributes (`@microsoft/fluentui-jsx-a11y/spinner-needs-labelling`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Spinner must have either aria-label or label, aria-live and aria-busy attributes. | ||
|
||
<https://www.w3.org/TR/html-aria/> | ||
|
||
<https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-live> | ||
|
||
<https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-label> | ||
|
||
## Ways to fix | ||
|
||
- Make sure that Spinner component has following attributes: | ||
- aria-live | ||
- aria-busy | ||
- either label or aria-label | ||
|
||
## Rule Details | ||
|
||
This rule aims to make Spinners accessible. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<Spinner {...props} /> | ||
``` | ||
|
||
```jsx | ||
<Spinner | ||
{...props} | ||
aria-label="some text" | ||
/> | ||
``` | ||
|
||
```jsx | ||
<Spinner | ||
{...props} | ||
aria-live="polite" | ||
/> | ||
``` | ||
|
||
```jsx | ||
<Spinner | ||
size="large" | ||
label="Large Spinner" | ||
aria-live="polite" | ||
/> | ||
``` | ||
|
||
```jsx | ||
<Spinner | ||
size="large" | ||
label="Large Spinner" | ||
aria-busy="true" | ||
/> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
<Spinner | ||
{...props} | ||
aria-label="my screen reader text" | ||
aria-live="polite" | ||
aria-busy="false" | ||
/> | ||
``` | ||
|
||
```jsx | ||
<Spinner | ||
{...props} | ||
aria-label="my screen reader text" | ||
aria-live="polite" | ||
aria-busy="true" | ||
/> | ||
``` | ||
|
||
```jsx | ||
<Spinner | ||
{...props} | ||
label="my text" | ||
aria-live="polite" | ||
aria-busy="true" | ||
/> | ||
``` |
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,55 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
"use strict"; | ||
|
||
const { hasNonEmptyProp } = require("../util/hasNonEmptyProp"); | ||
const elementType = require("jsx-ast-utils").elementType; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
// possible error messages for the rule | ||
messages: { | ||
noUnlabelledSpinner: "Accessibility: Spinner must have either aria-label or label, aria-live and aria-busy attributes" | ||
}, | ||
// "problem" means the rule is identifying code that either will cause an error or may cause a confusing behavior: https://eslint.org/docs/latest/developer-guide/working-with-rules | ||
type: "problem", | ||
// docs for the rule | ||
docs: { | ||
description: "Accessibility: Spinner must have either aria-label or label, aria-live and aria-busy attributes", | ||
recommended: true, | ||
url: "https://www.w3.org/TR/html-aria/" // URL to the documentation page for this rule | ||
}, | ||
schema: [] | ||
}, | ||
// create (function) returns an object with methods that ESLint calls to “visit” nodes while traversing the abstract syntax tree | ||
create(context) { | ||
return { | ||
// visitor functions for different types of nodes | ||
JSXOpeningElement(node) { | ||
// if it is not a Spinner, return | ||
if (elementType(node) !== "Spinner") { | ||
return; | ||
} | ||
|
||
if (hasNonEmptyProp(node.attributes, "aria-busy") | ||
&& hasNonEmptyProp(node.attributes, "aria-live") | ||
&& (hasNonEmptyProp(node.attributes, "label") || hasNonEmptyProp(node.attributes, "aria-label")) | ||
) { | ||
return; | ||
} | ||
|
||
// if it has no visual labelling, report error | ||
context.report({ | ||
node, | ||
messageId: `noUnlabelledSpinner` | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
|
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,52 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/spinner-needs-labelling"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run("spinner-needs-labelling", rule, { | ||
valid: [ | ||
`<Spinner aria-label="my screen reader text" aria-live="polite" aria-busy="false" />`, | ||
`<Spinner appearance="primary" label="Primary Spinner" aria-label="his screen reader text" aria-live="polite" aria-busy="true" />`, | ||
`<Spinner appearance="primary" label="Secondary Spinner" aria-live="polite" aria-busy="false" />`, | ||
`<Spinner appearance="primary" aria-label="your screen reader text" aria-live="polite" aria-busy="true" />`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<Spinner />`, | ||
errors: [{ messageId: "noUnlabelledSpinner" }] | ||
}, | ||
{ | ||
code: `<Spinner appearance="primary" label="Primary Spinner" />`, | ||
errors: [{ messageId: "noUnlabelledSpinner" }] | ||
}, | ||
{ | ||
code: `<Spinner size="large" aria-label="some text" />`, | ||
errors: [{ messageId: "noUnlabelledSpinner" }] | ||
}, | ||
{ | ||
code: `<Spinner size="large" label="Large Spinner" aria-live="polite" />`, | ||
errors: [{ messageId: "noUnlabelledSpinner" }] | ||
}, | ||
{ | ||
code: `<Spinner size="large" label="Large Spinner" aria-busy="true" />`, | ||
errors: [{ messageId: "noUnlabelledSpinner" }] | ||
}, | ||
{ | ||
code : `<Spinner aria-label="my screen reader text" aria-live="polite" />`, | ||
errors: [{ messageId: "noUnlabelledSpinner" }] | ||
} | ||
] | ||
}); | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.