forked from angular-eslint/angular-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessibility-label-has-associated-control.ts
135 lines (125 loc) · 3.82 KB
/
accessibility-label-has-associated-control.ts
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
import type { TmplAstElement } from '@angular-eslint/bundled-angular-compiler';
import { getTemplateParserServices } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
import { isChildNodeOf } from '../utils/is-child-node-of';
type LabelComponent = {
readonly inputs?: readonly string[];
readonly selector: string;
};
type Options = [
{
readonly controlComponents?: readonly string[];
readonly labelComponents?: readonly LabelComponent[];
},
];
export type MessageIds = 'accessibilityLabelHasAssociatedControl';
export const RULE_NAME = 'accessibility-label-has-associated-control';
const DEFAULT_CONTROL_COMPONENTS = [
'input',
'meter',
'output',
'progress',
'select',
'textarea',
];
const DEFAULT_LABEL_COMPONENTS: readonly LabelComponent[] = [
{ inputs: ['for', 'htmlFor'], selector: 'label' },
];
const DEFAULT_OPTIONS: Options[0] = {
controlComponents: DEFAULT_CONTROL_COMPONENTS,
labelComponents: DEFAULT_LABEL_COMPONENTS,
};
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Ensures that a label element/component is associated with a form element',
recommended: false,
},
schema: [
{
additionalProperties: false,
properties: {
controlComponents: {
items: { type: 'string' },
type: 'array',
uniqueItems: true,
},
labelComponents: {
items: {
additionalProperties: false,
properties: {
inputs: {
items: { type: 'string' },
type: 'array',
uniqueItems: true,
},
selector: { type: 'string' },
},
required: ['selector'],
type: 'object',
},
type: 'array',
uniqueItems: true,
},
},
type: 'object',
},
],
messages: {
accessibilityLabelHasAssociatedControl:
'A label component must be associated with a form element',
},
},
defaultOptions: [DEFAULT_OPTIONS],
create(context, [{ controlComponents, labelComponents }]) {
const parserServices = getTemplateParserServices(context);
const allControlComponents: ReadonlySet<string> = new Set([
...DEFAULT_CONTROL_COMPONENTS,
...(controlComponents ?? []),
]);
const allLabelComponents = [
...DEFAULT_LABEL_COMPONENTS,
...(labelComponents ?? []),
] as const;
const labelSelectors = allLabelComponents.map(({ selector }) => selector);
const labelComponentsPattern = toPattern(labelSelectors);
return {
[`Element$1[name=${labelComponentsPattern}]`](node: TmplAstElement) {
const element = allLabelComponents.find(
({ selector }) => selector === node.name,
);
if (!element) return;
const attributesInputs: ReadonlySet<string> = new Set(
[...node.attributes, ...node.inputs].map(({ name }) => name),
);
const hasInput = element.inputs?.some((input) =>
attributesInputs.has(input),
);
if (hasInput || hasControlComponentIn(allControlComponents, node)) {
return;
}
const loc = parserServices.convertNodeSourceSpanToLoc(node.sourceSpan);
context.report({
loc,
messageId: 'accessibilityLabelHasAssociatedControl',
});
},
};
},
});
function hasControlComponentIn(
controlComponents: ReadonlySet<string>,
element: TmplAstElement,
): boolean {
return Boolean(
[...controlComponents].some((controlComponent) =>
isChildNodeOf(element, controlComponent),
),
);
}
function toPattern(value: readonly unknown[]): RegExp {
return RegExp(`^(${value.join('|')})$`);
}