forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateClickEventsHaveKeyEventsRule.ts
More file actions
55 lines (47 loc) · 1.86 KB
/
templateClickEventsHaveKeyEventsRule.ts
File metadata and controls
55 lines (47 loc) · 1.86 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
import { ElementAst } from '@angular/compiler';
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';
export class Rule extends Rules.AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that the click event is accompanied with at least one key event keyup, keydown or keypress',
options: null,
optionsDescription: 'Not configurable.',
rationale: 'Keyboard is important for users with physical disabilities who cannot use mouse.',
ruleName: 'template-click-events-have-key-events',
type: 'functionality',
typescriptOnly: true
};
static readonly FAILURE_STRING = 'click must be accompanied by either keyup, keydown or keypress event for accessibility';
apply(sourceFile: SourceFile): RuleFailure[] {
return this.applyWithWalker(
new NgWalker(sourceFile, this.getOptions(), {
templateVisitorCtrl: TemplateClickEventsHaveKeyEventsVisitor
})
);
}
}
class TemplateClickEventsHaveKeyEventsVisitor extends BasicTemplateAstVisitor {
visitElement(el: ElementAst, context: any) {
this.validateElement(el);
super.visitElement(el, context);
}
private validateElement(el: ElementAst): void {
const hasClick = el.outputs.some(output => output.name === 'click');
if (!hasClick) {
return;
}
const hasKeyEvent = el.outputs.some(output => output.name === 'keyup' || output.name === 'keydown' || output.name === 'keypress');
if (hasKeyEvent) {
return;
}
const {
sourceSpan: {
end: { offset: endOffset },
start: { offset: startOffset }
}
} = el;
this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_STRING);
}
}