-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathtemplateAccessibilityTableScopeRule.ts
More file actions
54 lines (47 loc) · 1.89 KB
/
templateAccessibilityTableScopeRule.ts
File metadata and controls
54 lines (47 loc) · 1.89 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
import { ElementAst } from '@angular/compiler';
import { IRuleMetadata, RuleFailure, Rules, Utils } from 'tslint/lib';
import { SourceFile } from 'typescript';
import { BasicTemplateAstVisitor } from './angular';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
class TemplateVisitorCtrl extends BasicTemplateAstVisitor {
visitElement(ast: ElementAst, context: any) {
this.validateElement(ast);
super.visitElement(ast, context);
}
validateElement(element: ElementAst) {
if (element.name === 'th') {
return;
}
const hasScopeInput = element.inputs.some((input) => input.name === 'scope');
const hasScopeAttr = element.attrs.some((attr) => attr.name === 'scope');
if (hasScopeInput || hasScopeAttr) {
const {
sourceSpan: {
end: { offset: endOffset },
start: { offset: startOffset },
},
} = element;
this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_MESSAGE);
}
}
}
export class Rule extends Rules.AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that scope is not used on any element except th',
options: null,
optionsDescription: 'Not configurable.',
rationale: Utils.dedent`
The scope attribute makes table navigation much easier for screen reader users, provided that it is used correctly.
If used incorrectly, it can make table navigation much harder and less efficient. (aXe)
`,
ruleName: 'template-accessibility-table-scope',
type: 'functionality',
typescriptOnly: true,
};
static readonly FAILURE_MESSAGE = 'Scope attribute can only be on <th> element';
apply(sourceFile: SourceFile): RuleFailure[] {
const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
return this.applyWithWalker(walker);
}
}