Skip to content
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

feat: Add lint rule for Field component #108

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ We currently cover the following components:
- [N/A] Divider
- [] Drawer
- [X] Dropdown
- [] Field
- [x] Field
Harsh-Modi278 marked this conversation as resolved.
Show resolved Hide resolved
- [N/A] FluentProvider
- [] Image
- [] InfoLabel
Expand Down
61 changes: 61 additions & 0 deletions docs/rules/field-needs-labelling.md
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>
```
55 changes: 55 additions & 0 deletions lib/rules/field-needs-labelling.js
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))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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`
});
}
};
}
};

56 changes: 56 additions & 0 deletions tests/lib/rules/field-needs-labelling.js
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" }]
}
]
});

Loading