1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var ts = require("typescript");
|
14 | var Lint = require("tslint");
|
15 | var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
|
16 | var Utils_1 = require("./utils/Utils");
|
17 | var EMPTY_TITLE_FAILURE_STRING = 'Title elements must not be empty';
|
18 | var LONG_TITLE_FAILURE_STRING = 'Title length must not be longer than 60 characters';
|
19 | var WORD_TITLE_FAILURE_STRING = 'Title must contain more than one word';
|
20 | var MAX_TITLE_LENGTH = 60;
|
21 | var Rule = (function (_super) {
|
22 | __extends(Rule, _super);
|
23 | function Rule() {
|
24 | return _super !== null && _super.apply(this, arguments) || this;
|
25 | }
|
26 | Rule.prototype.apply = function (sourceFile) {
|
27 | if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
|
28 | return this.applyWithWalker(new ReactA11yTitlesRuleWalker(sourceFile, this.getOptions()));
|
29 | }
|
30 | else {
|
31 | return [];
|
32 | }
|
33 | };
|
34 | Rule.metadata = {
|
35 | ruleName: 'react-a11y-titles',
|
36 | type: 'functionality',
|
37 | description: 'For accessibility of your website, HTML title elements must be concise and non-empty.',
|
38 | options: null,
|
39 | optionsDescription: '',
|
40 | typescriptOnly: true,
|
41 | issueClass: 'Non-SDL',
|
42 | issueType: 'Warning',
|
43 | severity: 'Moderate',
|
44 | level: 'Opportunity for Excellence',
|
45 | group: 'Accessibility'
|
46 | };
|
47 | return Rule;
|
48 | }(Lint.Rules.AbstractRule));
|
49 | exports.Rule = Rule;
|
50 | var ReactA11yTitlesRuleWalker = (function (_super) {
|
51 | __extends(ReactA11yTitlesRuleWalker, _super);
|
52 | function ReactA11yTitlesRuleWalker() {
|
53 | return _super !== null && _super.apply(this, arguments) || this;
|
54 | }
|
55 | ReactA11yTitlesRuleWalker.prototype.visitJsxSelfClosingElement = function (node) {
|
56 | if (node.tagName.getText() === 'title') {
|
57 | this.addFailureAt(node.getStart(), node.getWidth(), EMPTY_TITLE_FAILURE_STRING);
|
58 | }
|
59 | _super.prototype.visitJsxSelfClosingElement.call(this, node);
|
60 | };
|
61 | ReactA11yTitlesRuleWalker.prototype.visitJsxElement = function (node) {
|
62 | var openingElement = node.openingElement;
|
63 | if (openingElement.tagName.getText() === 'title') {
|
64 | if (node.children.length === 0) {
|
65 | this.addFailureAt(node.getStart(), node.getWidth(), EMPTY_TITLE_FAILURE_STRING);
|
66 | }
|
67 | else if (node.children.length === 1) {
|
68 | if (node.children[0].kind === ts.SyntaxKind.JsxText) {
|
69 | var value = node.children[0];
|
70 | this.validateTitleText(value.getText(), node);
|
71 | }
|
72 | else if (node.children[0].kind === ts.SyntaxKind.JsxExpression) {
|
73 | var exp = node.children[0];
|
74 | if (exp.expression.kind === ts.SyntaxKind.StringLiteral) {
|
75 | this.validateTitleText(exp.expression.text, node);
|
76 | }
|
77 | }
|
78 | }
|
79 | }
|
80 | _super.prototype.visitJsxElement.call(this, node);
|
81 | };
|
82 | ReactA11yTitlesRuleWalker.prototype.validateTitleText = function (text, titleNode) {
|
83 | if (text.length > MAX_TITLE_LENGTH) {
|
84 | this.addFailureAt(titleNode.getStart(), titleNode.getWidth(), LONG_TITLE_FAILURE_STRING + ': ' + Utils_1.Utils.trimTo(text, 20));
|
85 | }
|
86 | else if (!(text.indexOf(' ') > 0)) {
|
87 | this.addFailureAt(titleNode.getStart(), titleNode.getWidth(), WORD_TITLE_FAILURE_STRING + ': ' + Utils_1.Utils.trimTo(text, 20));
|
88 | }
|
89 | };
|
90 | return ReactA11yTitlesRuleWalker;
|
91 | }(ErrorTolerantWalker_1.ErrorTolerantWalker));
|
92 |
|
\ | No newline at end of file |