Skip to content

Commit 40f008b

Browse files
committed
Support deeply nested input controls for form-control-has-label
1 parent 0eab373 commit 40f008b

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- Support deeply nested `input` controls for the `form-control-has-label` rule.
12+
913
## [0.1.2] - 2020-05-11
1014

1115
### Changed

bin/lint

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
3+
const { CLIEngine } = require("eslint");
4+
const a11yPlugin = require("../src");
5+
6+
(async function () {
7+
const cli = new CLIEngine({ baseConfig: a11yPlugin.configs.recommended });
8+
cli.addPlugin("eslint-plugin-vuejs-accessibility", a11yPlugin);
9+
10+
const report = cli.executeOnFiles(process.argv.slice(2));
11+
console.log(cli.getFormatter()(report.results));
12+
})().catch((error) => {
13+
process.exitCode = 1;
14+
console.error(error);
15+
});

docs/label-has-for.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ The `required` option (defaults to `"required": { "every": ["nesting", "id"] }`)
4040
The `allowChildren` option (defaults to `false`) determines whether default slot content is allowed to be passed into a `label` element. For example, the following pattern, by default, is not allowed:
4141

4242
```vue
43-
<label>Foo</label>
43+
<label>
44+
<slot />
45+
</label>
4446
```
4547

4648
However, if `allowChildren` is set to `true`, no error will be raised. If you want to pass in default slot content without raising an error because you cannot be sure what the default slot will render, then set `allowChildren` to `true`.

src/rules/__tests__/form-control-has-label.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ makeRuleTester("form-control-has-label", rule, {
66
"<label for=''><input type='text' /></label>",
77
"<input type='text' aria-label='test' />",
88
"<label for=''>text</label><input type='text' />",
9-
"<input type='button'>"
9+
"<input type='button'>",
10+
`
11+
<label>
12+
<div>
13+
<input type="radio" />
14+
</div>
15+
<div>
16+
<slot />
17+
</div>
18+
</label>
19+
`
1020
],
1121
invalid: ["<input type='text' />", "<textarea type='text'></textarea>"]
1222
});

src/rules/form-control-has-label.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ const isLabelElement = (node) =>
1111

1212
const hasLabelElement = (node) => {
1313
const { parent } = node;
14-
return [parent, ...parent.children].some(isLabelElement);
14+
15+
return (
16+
[parent, ...parent.children].some(isLabelElement) ||
17+
(parent && parent.type === "VElement" && hasLabelElement(parent))
18+
);
1519
};
1620

1721
module.exports = {

0 commit comments

Comments
 (0)