-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathtemplateAccessibilityElementsContentRule.ts
58 lines (50 loc) · 2.02 KB
/
templateAccessibilityElementsContentRule.ts
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
import { ElementAst } from '@angular/compiler';
import { sprintf } from 'sprintf-js';
import { IRuleMetadata, RuleFailure, Rules } 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 (Rule.ELEMENTS.indexOf(element.name) === -1) {
return;
}
const hasContent = element.children.length;
const hasInnerContent = element.inputs.some((input) => input.name === 'innerHTML' || input.name === 'innerText');
if (hasContent || hasInnerContent) {
return;
}
const {
sourceSpan: {
end: { offset: endOffset },
start: { offset: startOffset },
},
} = element;
this.addFailureFromStartToEnd(startOffset, endOffset, getErrorMessage(element.name));
}
}
export const getErrorMessage = (element: string): string => {
return sprintf(Rule.FAILURE_STRING, element);
};
export class Rule extends Rules.AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that the heading, anchor and button elements have content in it',
options: null,
optionsDescription: 'Not configurable.',
rationale: 'Heading, anchor and button elements should have content to be accessible by screen readers',
ruleName: 'template-accessibility-elements-content',
type: 'functionality',
typescriptOnly: true,
};
static readonly FAILURE_STRING = '<%s/> element should have content';
static readonly ELEMENTS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'button'];
apply(sourceFile: SourceFile): RuleFailure[] {
const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
return this.applyWithWalker(walker);
}
}