forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-require-valid-form-groups.js
More file actions
96 lines (79 loc) · 2.44 KB
/
template-require-valid-form-groups.js
File metadata and controls
96 lines (79 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/** @type {import('eslint').Rule.RuleModule} */
const FORM_ELEMENTS = new Set(['input']);
function hasRoleGroup(node) {
const roleAttr = node.attributes?.find((attr) => attr.name === 'role');
return roleAttr && roleAttr.value?.type === 'GlimmerTextNode' && roleAttr.value.chars === 'group';
}
function hasAriaLabel(node) {
return node.attributes?.some((attr) => attr.name === 'aria-labelledby');
}
function isValidFormGroup(node) {
if (node.tag === 'fieldset' || node.tag === 'legend') {
return true;
}
return hasRoleGroup(node) && hasAriaLabel(node);
}
function hasMultipleFormElementsInParentScope(node) {
const parent = node.parent;
if (!parent || parent.type !== 'GlimmerElementNode') {
return false;
}
const elementChildren =
parent.children?.filter((child) => child.type === 'GlimmerElementNode') || [];
const formElements = elementChildren.filter((child) => FORM_ELEMENTS.has(child.tag));
return formElements.length > 1;
}
function hasValidGroupingAncestor(node) {
let parent = node.parent;
while (parent) {
if (parent.type === 'GlimmerElementNode' && isValidFormGroup(parent)) {
return true;
}
parent = parent.parent;
}
return false;
}
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'require grouped form controls to have fieldset/legend or WAI-ARIA group labeling',
category: 'Accessibility',
recommendedGjs: false,
recommendedGts: false,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-valid-form-groups.md',
templateMode: 'both',
},
schema: [],
messages: {
requireValidFormGroups:
'Grouped form controls should have appropriate semantics such as fieldset and legend or WAI-ARIA labels',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/require-valid-form-groups.js',
docs: 'docs/rule/require-valid-form-groups.md',
tests: 'test/unit/rules/require-valid-form-groups-test.js',
},
},
create(context) {
return {
GlimmerElementNode(node) {
if (!FORM_ELEMENTS.has(node.tag)) {
return;
}
if (!hasMultipleFormElementsInParentScope(node)) {
return;
}
if (hasValidGroupingAncestor(node)) {
return;
}
context.report({
node,
messageId: 'requireValidFormGroups',
});
},
};
},
};