-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathtemplateAccessibilityTabindexNoPositiveRule.ts
More file actions
50 lines (44 loc) · 1.83 KB
/
Copy pathtemplateAccessibilityTabindexNoPositiveRule.ts
File metadata and controls
50 lines (44 loc) · 1.83 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
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';
import { getAttributeValue } from './util/getAttributeValue';
export class Rule extends Rules.AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that the tabindex attribute is not positive',
options: null,
optionsDescription: 'Not configurable.',
rationale: 'positive values for tabindex attribute should be avoided because they mess up with the order of focus (AX_FOCUS_03)',
ruleName: 'template-accessibility-tabindex-no-positive',
type: 'functionality',
typescriptOnly: true,
};
static readonly FAILURE_MESSAGE = 'tabindex attribute cannot be positive';
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(ast: ElementAst, context: any): any {
this.validateElement(ast);
super.visitElement(ast, context);
}
private validateElement(element: ElementAst) {
let tabIndexValue = getAttributeValue(element, 'tabindex');
if (tabIndexValue) {
tabIndexValue = parseInt(tabIndexValue, 10);
if (tabIndexValue > 0) {
const {
sourceSpan: {
end: { offset: endOffset },
start: { offset: startOffset },
},
} = element;
this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_MESSAGE);
}
}
}
}