-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathtemplateAccessibilityLabelForRule.ts
More file actions
174 lines (144 loc) · 6.23 KB
/
templateAccessibilityLabelForRule.ts
File metadata and controls
174 lines (144 loc) · 6.23 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
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
166
167
168
169
170
171
172
173
174
import { ElementAst } from '@angular/compiler';
import { IOptions, IRuleMetadata, RuleFailure } from 'tslint/lib';
import { AbstractRule } from 'tslint/lib/rules';
import { dedent } from 'tslint/lib/utils';
import { SourceFile } from 'typescript/lib/typescript';
import { ComponentMetadata } from './angular/metadata';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';
import { isChildNodeOf } from './util/isChildNodeOf';
import { objectKeys } from './util/objectKeys';
const OPTION_CONTROL_COMPONENTS = 'controlComponents';
const OPTION_LABEL_ATTRIBUTES = 'labelAttributes';
const OPTION_LABEL_COMPONENTS = 'labelComponents';
const OPTION_SCHEMA_VALUE = {
properties: {
items: {
type: 'string',
},
type: 'array',
uniqueItems: true,
},
type: 'object',
};
const DEFAULT_CONTROL_COMPONENTS = ['button', 'input', 'meter', 'output', 'progress', 'select', 'textarea'];
const DEFAULT_LABEL_ATTRIBUTES = ['for', 'htmlFor'];
const DEFAULT_LABEL_COMPONENTS = ['label'];
type OptionKeys = typeof OPTION_CONTROL_COMPONENTS | typeof OPTION_LABEL_ATTRIBUTES | typeof OPTION_LABEL_COMPONENTS;
type OptionDictionary = Readonly<Record<OptionKeys, ReadonlyArray<string>>>;
const getReadableItems = (items: ReadonlyArray<string>): string => {
const { length: itemsLength } = items;
if (itemsLength === 1) return `"${items[0]}"`;
return `${items
.map((x) => `"${x}"`)
.slice(0, itemsLength - 1)
.join(', ')} and "${[...items].pop()}"`;
};
export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Checks if a label component is associated with a form element',
optionExamples: [
true,
[
true,
{
[OPTION_CONTROL_COMPONENTS]: ['app-input'],
},
],
[
true,
{
[OPTION_CONTROL_COMPONENTS]: ['app-input', 'app-select'],
[OPTION_LABEL_ATTRIBUTES]: ['id'],
[OPTION_LABEL_COMPONENTS]: ['app-label'],
},
],
],
options: {
additionalProperties: false,
properties: {
[OPTION_CONTROL_COMPONENTS]: OPTION_SCHEMA_VALUE,
[OPTION_LABEL_ATTRIBUTES]: OPTION_SCHEMA_VALUE,
[OPTION_LABEL_COMPONENTS]: OPTION_SCHEMA_VALUE,
},
type: 'object',
},
optionsDescription: dedent`
An optional object with optional \`${OPTION_CONTROL_COMPONENTS}\`, \`${OPTION_LABEL_ATTRIBUTES}\` and \`${OPTION_LABEL_COMPONENTS}\` properties.
* \`${OPTION_CONTROL_COMPONENTS}\` - components that must be inside a label component. Default and non overridable values are
${getReadableItems(DEFAULT_CONTROL_COMPONENTS)}.
* \`${OPTION_LABEL_ATTRIBUTES}\` - attributes that must be set on label components. Default and non overridable values are
${getReadableItems(DEFAULT_LABEL_ATTRIBUTES)}.
* \`${OPTION_LABEL_COMPONENTS}\` - components that act like a label. Default and non overridable values are
${getReadableItems(DEFAULT_LABEL_COMPONENTS)}.
`,
ruleName: 'template-accessibility-label-for',
type: 'functionality',
typescriptOnly: true,
};
static readonly FAILURE_STRING = 'A label component must be associated with a form element';
apply(sourceFile: SourceFile): RuleFailure[] {
const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
return this.applyWithWalker(walker);
}
isEnabled(): boolean {
return super.isEnabled() && this.areOptionsValid();
}
private areOptionsValid(): boolean {
const { length: ruleArgumentsLength } = this.ruleArguments;
if (ruleArgumentsLength === 0) return true;
if (ruleArgumentsLength > 1) return false;
const {
metadata: { options: ruleOptions },
} = Rule;
const [ruleArgument] = this.ruleArguments as ReadonlyArray<OptionDictionary>;
const ruleArgumentsKeys = objectKeys(ruleArgument);
const propertiesKeys = objectKeys(ruleOptions.properties as OptionDictionary);
return (
ruleArgumentsKeys.every((argumentKey) => propertiesKeys.includes(argumentKey)) &&
ruleArgumentsKeys
.map((argumentKey) => ruleArgument[argumentKey])
.every((argumentValue) => Array.isArray(argumentValue) && argumentValue.length > 0)
);
}
}
class TemplateVisitorCtrl extends BasicTemplateAstVisitor {
private readonly controlComponents: ReadonlySet<string>;
private readonly labelAttributes: ReadonlySet<string>;
private readonly labelComponents: ReadonlySet<string>;
constructor(sourceFile: SourceFile, options: IOptions, context: ComponentMetadata, templateStart: number) {
super(sourceFile, options, context, templateStart);
const { controlComponents, labelAttributes, labelComponents } = (options.ruleArguments[0] || {}) as OptionDictionary;
this.controlComponents = new Set([...DEFAULT_CONTROL_COMPONENTS.concat(controlComponents)]);
this.labelAttributes = new Set([...DEFAULT_LABEL_ATTRIBUTES.concat(labelAttributes)]);
this.labelComponents = new Set([...DEFAULT_LABEL_COMPONENTS.concat(labelComponents)]);
}
visitElement(element: ElementAst, context: any): any {
this.validateElement(element);
super.visitElement(element, context);
}
private hasControlComponentInsideElement(element: ElementAst): boolean {
return Array.from(this.controlComponents).some((controlComponentName) => isChildNodeOf(element, controlComponentName));
}
private hasValidAttrOrInput(element: ElementAst): boolean {
return [...element.attrs, ...element.inputs]
.map((attrOrInput) => attrOrInput.name)
.some((attrOrInputName) => this.labelAttributes.has(attrOrInputName));
}
private isLabelComponent(element: ElementAst): boolean {
return this.labelComponents.has(element.name);
}
private validateElement(element: ElementAst): void {
if (!this.isLabelComponent(element) || this.hasValidAttrOrInput(element) || this.hasControlComponentInsideElement(element)) {
return;
}
const {
sourceSpan: {
end: { offset: endOffset },
start: { offset: startOffset },
},
} = element;
this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_STRING);
}
}