forked from jsx-eslint/eslint-plugin-jsx-a11y
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel-has-associated-control.js
165 lines (151 loc) · 5.05 KB
/
label-has-associated-control.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @fileoverview Enforce label tags have an associated control.
* @author Jesse Beach
*
* @flow
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { hasProp, getProp, getPropValue } from 'jsx-ast-utils';
import type { JSXElement } from 'ast-types-flow';
import minimatch from 'minimatch';
import { generateObjSchema, arraySchema } from '../util/schemas';
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
import getElementType from '../util/getElementType';
import mayContainChildComponent from '../util/mayContainChildComponent';
import mayHaveAccessibleLabel from '../util/mayHaveAccessibleLabel';
const errorMessages = {
accessibleLabel: 'A form label must have accessible text.',
htmlFor: 'A form label must have a valid htmlFor attribute.',
nesting: 'A form label must have an associated control as a descendant.',
either: 'A form label must either have a valid htmlFor attribute or a control as a descendant.',
both: 'A form label must have a valid htmlFor attribute and a control as a descendant.',
};
const schema = generateObjSchema({
labelComponents: arraySchema,
labelAttributes: arraySchema,
controlComponents: arraySchema,
assert: {
description: 'Assert that the label has htmlFor, a nested label, both or either',
type: 'string',
enum: ['htmlFor', 'nesting', 'both', 'either'],
},
depth: {
description: 'JSX tree depth limit to check for accessible label',
type: 'integer',
minimum: 0,
},
});
const validateHtmlFor = (node, context) => {
const { settings } = context;
const htmlForAttributes = settings['jsx-a11y']?.attributes?.for ?? ['htmlFor'];
for (let i = 0; i < htmlForAttributes.length; i += 1) {
const attribute = htmlForAttributes[i];
if (hasProp(node.attributes, attribute)) {
const htmlForAttr = getProp(node.attributes, attribute);
const htmlForValue = getPropValue(htmlForAttr);
return htmlForAttr !== false && !!htmlForValue;
}
}
return false;
};
export default ({
meta: {
docs: {
description: 'Enforce that a `label` tag has a text label and an associated control.',
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/label-has-associated-control.md',
},
schema: [schema],
},
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
const options = context.options[0] || {};
const labelComponents = options.labelComponents || [];
const assertType = options.assert || 'either';
const labelComponentNames = ['label'].concat(labelComponents);
const elementType = getElementType(context);
const rule = (node: JSXElement) => {
const isLabelComponent = labelComponentNames.some((name) => minimatch(elementType(node.openingElement), name));
if (!isLabelComponent) {
return;
}
const controlComponents = [].concat(
'input',
'meter',
'output',
'progress',
'select',
'textarea',
options.controlComponents || [],
);
// Prevent crazy recursion.
const recursionDepth = Math.min(
options.depth === undefined ? 2 : options.depth,
25,
);
const hasHtmlFor = validateHtmlFor(node.openingElement, context);
// Check for multiple control components.
const hasNestedControl = controlComponents.some((name) => mayContainChildComponent(
node,
name,
recursionDepth,
elementType,
));
const hasAccessibleLabel = mayHaveAccessibleLabel(
node,
recursionDepth,
options.labelAttributes,
elementType,
controlComponents,
);
// Bail out immediately if we don't have an accessible label.
if (!hasAccessibleLabel) {
context.report({
node: node.openingElement,
message: errorMessages.accessibleLabel,
});
return;
}
switch (assertType) {
case 'htmlFor':
if (!hasHtmlFor) {
context.report({
node: node.openingElement,
message: errorMessages.htmlFor,
});
}
break;
case 'nesting':
if (!hasNestedControl) {
context.report({
node: node.openingElement,
message: errorMessages.nesting,
});
}
break;
case 'both':
if (!hasHtmlFor || !hasNestedControl) {
context.report({
node: node.openingElement,
message: errorMessages.both,
});
}
break;
case 'either':
if (!hasHtmlFor && !hasNestedControl) {
context.report({
node: node.openingElement,
message: errorMessages.either,
});
}
break;
default:
break;
}
};
// Create visitor selectors.
return {
JSXElement: rule,
};
},
}: ESLintConfig);