forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateMouseEventsHaveKeyEventsRule.ts
More file actions
60 lines (50 loc) · 2.25 KB
/
templateMouseEventsHaveKeyEventsRule.ts
File metadata and controls
60 lines (50 loc) · 2.25 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
import { ElementAst } from '@angular/compiler';
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';
export class Rule extends Rules.AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that the Mouse Events mouseover and mouseout are accompanied with Key Events focus and blur',
options: null,
optionsDescription: 'Not configurable.',
rationale: 'Keyboard is important for users with physical disabilities who cannot use mouse.',
ruleName: 'template-mouse-events-have-key-events',
type: 'functionality',
typescriptOnly: true,
};
static readonly FAILURE_STRING_MOUSE_OVER = 'mouseover must be accompanied by focus event for accessibility';
static readonly FAILURE_STRING_MOUSE_OUT = 'mouseout must be accompanied by blur event for accessibility';
apply(sourceFile: SourceFile): RuleFailure[] {
const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
return this.applyWithWalker(walker);
}
}
class TemplateVisitorCtrl extends BasicTemplateAstVisitor {
visitElement(el: ElementAst, context: any) {
this.validateElement(el);
super.visitElement(el, context);
}
private validateElement(el: ElementAst): void {
const hasMouseOver = el.outputs.some((output) => output.name === 'mouseover');
const hasMouseOut = el.outputs.some((output) => output.name === 'mouseout');
const hasFocus = el.outputs.some((output) => output.name === 'focus');
const hasBlur = el.outputs.some((output) => output.name === 'blur');
if (!hasMouseOver && !hasMouseOut) {
return;
}
const {
sourceSpan: {
end: { offset: endOffset },
start: { offset: startOffset },
},
} = el;
if (hasMouseOver && !hasFocus) {
this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_STRING_MOUSE_OVER);
}
if (hasMouseOut && !hasBlur) {
this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_STRING_MOUSE_OUT);
}
}
}