-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add require-input-label rule (#252)
* feat: add require-input-label * implement * impl * add word * add docs * add docs * add tests
- Loading branch information
Showing
9 changed files
with
227 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
"sourcecode", | ||
"espree", | ||
"nohtml", | ||
"tmpl" | ||
"tmpl", | ||
"labelledby" | ||
] | ||
} |
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,36 @@ | ||
# require-input-label | ||
|
||
This rule enforces the presence of accessible labels for input elements such as `<input type="text">`, `<select>` and `<textarea>`. | ||
|
||
## Why? | ||
|
||
- Labels improve accessibility for users of assistive technologies by ensuring that form controls are properly described. | ||
|
||
## How to use | ||
|
||
```js,.eslintrc.js | ||
module.exports = { | ||
rules: { | ||
"@html-eslint/require-input-label": "error", | ||
}, | ||
}; | ||
``` | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```html,incorrect | ||
<input type="text"> | ||
<input type="hidden"> | ||
<textarea></textarea> | ||
<select></select> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```html,correct | ||
<input id="fo"> | ||
<label>name: <input></label> | ||
<textarea aria-labelledby="foo"></textarea> | ||
``` |
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,77 @@ | ||
/** | ||
* @typedef { import("../types").RuleModule } RuleModule | ||
* @typedef { import("../types").Tag } Tag | ||
*/ | ||
|
||
const { RULE_CATEGORY } = require("../constants"); | ||
const { createVisitors } = require("./utils/visitors"); | ||
const { findParent, isTag } = require("./utils/node"); | ||
|
||
const MESSAGE_IDS = { | ||
MISSING: "missingLabel", | ||
}; | ||
|
||
const INPUT_TAGS = new Set(["input", "textarea", "select"]); | ||
|
||
const LABEL_ATTRIBUTES = new Set(["id", "aria-labelledby", "aria-label"]); | ||
|
||
/** | ||
* @type {RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: "code", | ||
|
||
docs: { | ||
description: | ||
"Enforces use of label for form elements(`input`, `textarea`, `select`)", | ||
category: RULE_CATEGORY.ACCESSIBILITY, | ||
recommended: false, | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
[MESSAGE_IDS.MISSING]: "Missing an associated label", | ||
}, | ||
}, | ||
create(context) { | ||
return createVisitors(context, { | ||
Tag(node) { | ||
if (!INPUT_TAGS.has(node.name.toLowerCase())) { | ||
return; | ||
} | ||
|
||
for (const attr of node.attributes) { | ||
if ( | ||
LABEL_ATTRIBUTES.has(attr.key.value.toLowerCase()) && | ||
attr.value && | ||
attr.value.value | ||
) { | ||
return; | ||
} | ||
|
||
if ( | ||
attr.key.value.toLowerCase() === "type" && | ||
attr.value && | ||
attr.value.value === "hidden" | ||
) { | ||
return; | ||
} | ||
} | ||
|
||
const label = findParent(node, (parent) => { | ||
return isTag(parent) && parent.name.toLowerCase() === "label"; | ||
}); | ||
|
||
if (label) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: "missingLabel", | ||
}); | ||
}, | ||
}); | ||
}, | ||
}; |
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
72 changes: 72 additions & 0 deletions
72
packages/eslint-plugin/tests/rules/require-input-label.test.js
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,72 @@ | ||
const createRuleTester = require("../rule-tester"); | ||
const rule = require("../../lib/rules/require-input-label"); | ||
|
||
const ruleTester = createRuleTester(); | ||
const templateRuleTester = createRuleTester("espree"); | ||
|
||
ruleTester.run("require-input-label", rule, { | ||
valid: [ | ||
{ | ||
code: `<input id="foo">`, | ||
}, | ||
{ | ||
code: `<textarea id="foo"></textarea>`, | ||
}, | ||
{ | ||
code: `<input type="hidden">`, | ||
}, | ||
{ | ||
code: `<label>name: <input></label>`, | ||
}, | ||
{ | ||
code: `<textarea aria-labelledby="foo" />`, | ||
}, | ||
{ | ||
code: `<textarea aria-label="foo" />`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `<input type="text">`, | ||
errors: [ | ||
{ | ||
messageId: "missingLabel", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<textarea></textarea>`, | ||
errors: [ | ||
{ | ||
messageId: "missingLabel", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `<label>name: </label><input type="text">`, | ||
errors: [ | ||
{ | ||
messageId: "missingLabel", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
templateRuleTester.run("[template] require-input-label", rule, { | ||
valid: [ | ||
{ | ||
code: `html\`<input id="foo">\``, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `html\`<input type="text">\``, | ||
errors: [ | ||
{ | ||
messageId: "missingLabel", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |