-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Add lint rule for Field component #108
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
Changes from 4 commits
832cb01
fc5981c
a2d6461
0744d3c
62b8a93
b42e2f0
f4cfee3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Accessibility: Field must have either label, validationMessage and hint attributes (`@microsoft/fluentui-jsx-a11y/field-needs-labelling`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Field must have `label` prop and either `validationMessage` or `hint` prop. | ||
|
||
<https://www.w3.org/TR/html-aria/> | ||
|
||
## Ways to fix | ||
|
||
- Make sure that Field component has following props: | ||
- `label` | ||
- `validationMessage` or `hint` | ||
|
||
## Rule Details | ||
|
||
This rule aims to make Field component accessible. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<Field | ||
label="Example field" | ||
validationState="success" | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field> | ||
``` | ||
|
||
```jsx | ||
<Field | ||
validationState="success" | ||
hint="This is a hint." | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
<Field | ||
label="Example field" | ||
validationState="success" | ||
validationMessage="This is a success message." | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field> | ||
``` | ||
|
||
```jsx | ||
<Field | ||
label="Example field" | ||
validationState="success" | ||
hint="This is a hint." | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field> | ||
``` |
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: { | ||
noUnlabelledField: "Accessibility: Field must have either label, validationMessage and hint 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: Field must have either label, validationMessage and hint 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) !== "Field") { | ||
return; | ||
} | ||
|
||
if ( | ||
hasNonEmptyProp(node.attributes, "label", true) && | ||
(hasNonEmptyProp(node.attributes, "validationMessage", true) || hasNonEmptyProp(node.attributes, "hint", true)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the rule looking for the label is universal to all components. The check for validationMessage or hint is not. Maybe you could also add if the Field has certain child components, then the check is required? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we also have a rule for no empty components, so that will work in this case. What do you say? |
||
) { | ||
return; | ||
} | ||
|
||
// if it has no visual labelling, report error | ||
context.report({ | ||
node, | ||
messageId: `noUnlabelledField` | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/field-needs-labelling"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run("field-needs-labelling", rule, { | ||
valid: [ | ||
`<Field | ||
label="Example field" | ||
validationState="success" | ||
validationMessage="This is a success message." | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field>`, | ||
`<Field | ||
label="Example field" | ||
validationState="success" | ||
hint="This is a hint." | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field>` | ||
], | ||
invalid: [ | ||
{ | ||
code: `<Field | ||
label="Example field" | ||
validationState="success" | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field>`, | ||
errors: [{ messageId: "noUnlabelledField" }] | ||
}, | ||
{ | ||
code: `<Field | ||
validationState="success" | ||
hint="This is a hint." | ||
> | ||
<ProgressBar value={0.5} max={1} /> | ||
</Field>`, | ||
errors: [{ messageId: "noUnlabelledField" }] | ||
} | ||
] | ||
}); | ||
|
Uh oh!
There was an error while loading. Please reload this page.