UNPKG

4.82 kBJavaScriptView Raw
1"use strict";
2var __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})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var FAILURE_MISSING_LANG = 'An html element is missing the lang attribute';
17var FAILURE_WRONG_LANG_CODE = 'Lang attribute does not have a valid value. Found: ';
18var LANGUAGE_CODES = [
19 'ab', 'aa', 'af', 'sq', 'am', 'ar', 'an', 'hy', 'as', 'ay', 'az', 'ba', 'eu', 'bn',
20 'dz', 'bh', 'bi', 'br', 'bg', 'my', 'be', 'km', 'ca', 'zh', 'zh-Hans', 'zh-Hant',
21 'co', 'hr', 'cs', 'da', 'nl', 'en', 'eo', 'et', 'fo', 'fa', 'fj', 'fi', 'fr', 'fy',
22 'gl', 'gd', 'gv', 'ka', 'de', 'el', 'kl', 'gn', 'gu', 'ht', 'ha', 'he', 'iw', 'hi',
23 'hu', 'is', 'io', 'id', 'in', 'ia', 'ie', 'iu', 'ik', 'ga', 'it', 'ja', 'jv', 'kn',
24 'ks', 'kk', 'rw', 'ky', 'rn', 'ko', 'ku', 'lo', 'la', 'lv', 'li', 'ln', 'lt', 'mk',
25 'mg', 'ms', 'ml', 'mt', 'mi', 'mr', 'mo', 'mn', 'na', 'ne', 'no', 'oc', 'or', 'om',
26 'ps', 'pl', 'pt', 'pa', 'qu', 'rm', 'ro', 'ru', 'sm', 'sg', 'sa', 'sr', 'sh', 'st',
27 'tn', 'sn', 'ii', 'sd', 'si', 'ss', 'sk', 'sl', 'so', 'es', 'su', 'sw', 'sv', 'tl',
28 'tg', 'ta', 'tt', 'te', 'th', 'bo', 'ti', 'to', 'ts', 'tr', 'tk', 'tw', 'ug', 'uk',
29 'ur', 'uz', 'vi', 'vo', 'wa', 'cy', 'wo', 'xh', 'yi', 'ji', 'yo', 'zu'
30];
31var Rule = (function (_super) {
32 __extends(Rule, _super);
33 function Rule() {
34 return _super !== null && _super.apply(this, arguments) || this;
35 }
36 Rule.prototype.apply = function (sourceFile) {
37 if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
38 return this.applyWithWalker(new ReactA11yLangRuleWalker(sourceFile, this.getOptions()));
39 }
40 else {
41 return [];
42 }
43 };
44 Rule.metadata = {
45 ruleName: 'react-a11y-lang',
46 type: 'functionality',
47 description: 'For accessibility of your website, html elements must have a valid lang attribute.',
48 options: null,
49 optionsDescription: '',
50 typescriptOnly: true,
51 issueClass: 'Non-SDL',
52 issueType: 'Warning',
53 severity: 'Low',
54 level: 'Opportunity for Excellence',
55 group: 'Accessibility'
56 };
57 return Rule;
58}(Lint.Rules.AbstractRule));
59exports.Rule = Rule;
60var ReactA11yLangRuleWalker = (function (_super) {
61 __extends(ReactA11yLangRuleWalker, _super);
62 function ReactA11yLangRuleWalker() {
63 return _super !== null && _super.apply(this, arguments) || this;
64 }
65 ReactA11yLangRuleWalker.prototype.visitJsxSelfClosingElement = function (node) {
66 this.validateOpeningElement(node, node);
67 _super.prototype.visitJsxSelfClosingElement.call(this, node);
68 };
69 ReactA11yLangRuleWalker.prototype.visitJsxElement = function (node) {
70 this.validateOpeningElement(node, node.openingElement);
71 _super.prototype.visitJsxElement.call(this, node);
72 };
73 ReactA11yLangRuleWalker.prototype.validateOpeningElement = function (parent, openingElement) {
74 var _this = this;
75 if (openingElement.tagName.getText() === 'html') {
76 var attributes = openingElement.attributes;
77 var langFound_1 = false;
78 attributes.properties.forEach(function (attribute) {
79 if (attribute.kind === ts.SyntaxKind.JsxAttribute) {
80 if (attribute.name.getText() === 'lang') {
81 langFound_1 = true;
82 if (attribute.initializer.kind === ts.SyntaxKind.StringLiteral) {
83 var langText = attribute.initializer.text;
84 if ((LANGUAGE_CODES.indexOf(langText)) === -1) {
85 _this.addFailureAt(parent.getStart(), parent.getWidth(), FAILURE_WRONG_LANG_CODE + langText);
86 }
87 }
88 }
89 }
90 });
91 if (!langFound_1) {
92 this.addFailureAt(parent.getStart(), parent.getWidth(), FAILURE_MISSING_LANG);
93 }
94 }
95 };
96 return ReactA11yLangRuleWalker;
97}(ErrorTolerantWalker_1.ErrorTolerantWalker));
98//# sourceMappingURL=reactA11yLangRule.js.map
\No newline at end of file