-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathcontrol-has-associated-label.js
115 lines (101 loc) · 3.45 KB
/
control-has-associated-label.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
/**
* @fileoverview Enforce controls are associated with a text label.
* @author Jesse Beach
*
* @flow
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { elementType, getProp, getLiteralPropValue } from 'jsx-ast-utils';
import type { JSXElement } from 'ast-types-flow';
import includes from 'array-includes';
import { generateObjSchema, arraySchema } from '../util/schemas';
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
import isDOMElement from '../util/isDOMElement';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
import isInteractiveElement from '../util/isInteractiveElement';
import isInteractiveRole from '../util/isInteractiveRole';
import mayHaveAccessibleLabel from '../util/mayHaveAccessibleLabel';
const errorMessage = 'A control must be associated with a text label.';
const ignoreList = ['link'];
const schema = generateObjSchema({
labelAttributes: arraySchema,
controlComponents: arraySchema,
ignoreElements: arraySchema,
ignoreRoles: arraySchema,
depth: {
description: 'JSX tree depth limit to check for accessible label',
type: 'integer',
minimum: 0,
},
});
module.exports = ({
meta: {
docs: {},
schema: [schema],
},
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
const options = context.options[0] || {};
const {
labelAttributes = [],
controlComponents = [],
ignoreElements = [],
ignoreRoles = [],
} = options;
const newIgnoreElements = new Set([...ignoreElements, ...ignoreList]);
const rule = (node: JSXElement): void => {
const tag = elementType(node.openingElement);
const role = getLiteralPropValue(getProp(node.openingElement.attributes, 'role'));
// Ignore interactive elements that might get their label from a source
// that cannot be discerned from static analysis, like
// <label><input />Save</label>
if (newIgnoreElements.has(tag)) {
return;
}
// Ignore roles that are "interactive" but should not require a label.
if (includes(ignoreRoles, role)) {
return;
}
const props = node.openingElement.attributes;
const nodeIsDOMElement = isDOMElement(tag);
const nodeIsHiddenFromScreenReader = isHiddenFromScreenReader(tag, props);
const nodeIsInteractiveElement = isInteractiveElement(tag, props);
const nodeIsInteractiveRole = isInteractiveRole(tag, props);
const nodeIsControlComponent = controlComponents.indexOf(tag) > -1;
if (nodeIsHiddenFromScreenReader) {
return;
}
let hasAccessibleLabel = true;
if (
nodeIsInteractiveElement
|| (
nodeIsDOMElement
&& nodeIsInteractiveRole
)
|| nodeIsControlComponent
) {
// Prevent crazy recursion.
const recursionDepth = Math.min(
options.depth === undefined ? 2 : options.depth,
25,
);
hasAccessibleLabel = mayHaveAccessibleLabel(
node,
recursionDepth,
labelAttributes,
);
}
if (!hasAccessibleLabel) {
context.report({
node: node.openingElement,
message: errorMessage,
});
}
};
// Create visitor selectors.
return {
JSXElement: rule,
};
},
}: ESLintConfig);