This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
/
reactA11yTitlesRule.ts
80 lines (71 loc) · 3.72 KB
/
reactA11yTitlesRule.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';
import { Utils } from './utils/Utils';
const EMPTY_TITLE_FAILURE_STRING: string = 'Title elements must not be empty';
const LONG_TITLE_FAILURE_STRING: string = 'Title length must not be longer than 60 characters';
const WORD_TITLE_FAILURE_STRING: string = 'Title must contain more than one word';
const MAX_TITLE_LENGTH: number = 60;
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: 'react-a11y-titles',
type: 'functionality',
description: 'For accessibility of your website, HTML title elements must be concise and non-empty.',
rationale: `References:
<ul>
<li><a href="http://www.w3.org/TR/WCAG20/#navigation-mechanisms-title">WCAG 2.0 - Requirement 2.4.2 Page Titled (Level A)</a></li>
<li><a href="http://oaa-accessibility.org/wcag20/rule/13">OAA-Accessibility Rule 13: Title element should not be empty</a></li>
<li><a href="http://oaa-accessibility.org/wcag20/rule/24">OAA-Accessibility Rule 24: Title content should be concise</a></li>
<li><a href="http://oaa-accessibility.org/wcag20/rule/25">OAA-Accessibility Rule 25: Title text must contain more than one word</a></li>
</ul>`,
options: null, // tslint:disable-line:no-null-keyword
optionsDescription: '',
typescriptOnly: true,
issueClass: 'Non-SDL',
issueType: 'Warning',
severity: 'Moderate',
level: 'Opportunity for Excellence',
group: 'Accessibility'
};
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
return this.applyWithFunction(sourceFile, walk);
}
return [];
}
}
function walk(ctx: Lint.WalkContext<void>) {
function validateTitleText(text: string, titleNode: ts.Node): void {
if (text.length > MAX_TITLE_LENGTH) {
ctx.addFailureAt(titleNode.getStart(), titleNode.getWidth(), LONG_TITLE_FAILURE_STRING + ': ' + Utils.trimTo(text, 20));
} else if (!(text.indexOf(' ') > 0)) {
ctx.addFailureAt(titleNode.getStart(), titleNode.getWidth(), WORD_TITLE_FAILURE_STRING + ': ' + Utils.trimTo(text, 20));
}
}
function cb(node: ts.Node): void {
if (tsutils.isJsxSelfClosingElement(node)) {
if (node.tagName.getText() === 'title') {
ctx.addFailureAt(node.getStart(), node.getWidth(), EMPTY_TITLE_FAILURE_STRING);
}
} else if (tsutils.isJsxElement(node)) {
if (node.openingElement.tagName.getText() === 'title') {
if (node.children.length === 0) {
ctx.addFailureAt(node.getStart(), node.getWidth(), EMPTY_TITLE_FAILURE_STRING);
} else if (node.children.length === 1) {
if (node.children[0].kind === ts.SyntaxKind.JsxText) {
const value: ts.JsxText = <ts.JsxText>node.children[0];
validateTitleText(value.getText(), node);
} else if (node.children[0].kind === ts.SyntaxKind.JsxExpression) {
const exp: ts.JsxExpression = <ts.JsxExpression>node.children[0];
if (exp.expression !== undefined && exp.expression.kind === ts.SyntaxKind.StringLiteral) {
validateTitleText((<ts.StringLiteral>exp.expression).text, node);
}
}
}
}
}
return ts.forEachChild(node, cb);
}
return ts.forEachChild(ctx.sourceFile, cb);
}