/
accessibility-label-for.ts
134 lines (125 loc) · 3.71 KB
/
accessibility-label-for.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
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 Options = [
{
readonly controlComponents?: readonly string[];
readonly labelAttributes?: readonly string[];
readonly labelComponents?: readonly string[];
},
];
export type MessageIds = 'accessibilityLabelFor';
export const RULE_NAME = 'accessibility-label-for';
const OPTION_SCHEMA_VALUE = {
items: { type: 'string' },
type: 'array',
uniqueItems: true,
} as const;
const DEFAULT_ELEMENTS = [
'button',
'input',
'meter',
'output',
'progress',
'select',
'textarea',
] as const;
const DEFAULT_LABEL_ATTRIBUTES = ['for', 'htmlFor'] as const;
const DEFAULT_LABEL_COMPONENTS = ['label'] as const;
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
deprecated: true,
replacedBy: ['accessibility-label-has-associated-control'],
type: 'suggestion',
docs: {
description:
'Ensures that a label element/component is associated with a form element',
recommended: false,
},
schema: [
{
additionalProperties: false,
properties: {
// NOTE: These need to be unique objects for documentation generation purposes
controlComponents: { ...OPTION_SCHEMA_VALUE },
labelAttributes: { ...OPTION_SCHEMA_VALUE },
labelComponents: { ...OPTION_SCHEMA_VALUE },
},
type: 'object',
},
],
messages: {
accessibilityLabelFor:
'A label element/component must be associated with a form element',
},
},
defaultOptions: [
{
controlComponents: DEFAULT_ELEMENTS,
labelAttributes: DEFAULT_LABEL_ATTRIBUTES,
labelComponents: DEFAULT_LABEL_COMPONENTS,
},
],
create(context, [options]) {
const parserServices = getTemplateParserServices(context);
const { controlComponents, labelAttributes, labelComponents } =
getParsedOptions(options);
const labelComponentsPattern = toPattern([...labelComponents]);
return {
[`Element$1[name=${labelComponentsPattern}]`](node: TmplAstElement) {
const attributesInputs: ReadonlySet<string> = new Set(
[...node.attributes, ...node.inputs].map(({ name }) => name),
);
const hasLabelAttribute = [...labelAttributes].some((labelAttribute) =>
attributesInputs.has(labelAttribute),
);
if (
hasLabelAttribute ||
hasControlComponentIn(controlComponents, node)
) {
return;
}
const loc = parserServices.convertNodeSourceSpanToLoc(node.sourceSpan);
context.report({
loc,
messageId: 'accessibilityLabelFor',
});
},
};
},
});
function getParsedOptions({
controlComponents,
labelAttributes,
labelComponents,
}: Options[0]) {
return {
controlComponents: new Set([
...DEFAULT_ELEMENTS,
...(controlComponents ?? []),
]) as ReadonlySet<string>,
labelAttributes: new Set([
...DEFAULT_LABEL_ATTRIBUTES,
...(labelAttributes ?? []),
]) as ReadonlySet<string>,
labelComponents: new Set([
...DEFAULT_LABEL_COMPONENTS,
...(labelComponents ?? []),
]) as ReadonlySet<string>,
} as const;
}
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('|')})$`);
}