UNPKG

3.69 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 JsxAttribute_1 = require("./utils/JsxAttribute");
16var ROLE_SCHEMA = require('./utils/attributes/roleSchema.json');
17var ROLES = ROLE_SCHEMA.roles;
18var VALID_ROLES = Object.keys(ROLES).filter(function (role) { return ROLES[role].isAbstract === false; });
19function getFailureStringUndefinedRole() {
20 return '\'role\' attribute empty. Either select a role from https://www.w3.org/TR/wai-aria/roles#role_definitions, ' +
21 'or simply remove this attribute';
22}
23exports.getFailureStringUndefinedRole = getFailureStringUndefinedRole;
24function getFailureStringInvalidRole(invalidRoleName) {
25 return "Invalid role attribute value '" + invalidRoleName + "', elements with ARIA roles must use a valid, non-abstract ARIA role. A reference to role definitions can be found at https://www.w3.org/TR/wai-aria/roles#role_definitions.";
26}
27exports.getFailureStringInvalidRole = getFailureStringInvalidRole;
28var Rule = (function (_super) {
29 __extends(Rule, _super);
30 function Rule() {
31 return _super !== null && _super.apply(this, arguments) || this;
32 }
33 Rule.prototype.apply = function (sourceFile) {
34 return sourceFile.languageVariant === ts.LanguageVariant.JSX
35 ? this.applyWithWalker(new A11yRoleRuleWalker(sourceFile, this.getOptions()))
36 : [];
37 };
38 Rule.metadata = {
39 ruleName: 'react-a11y-role',
40 type: 'maintainability',
41 description: 'Elements with aria roles must use a **valid**, **non-abstract** aria role.',
42 options: null,
43 optionsDescription: '',
44 typescriptOnly: true,
45 issueClass: 'Non-SDL',
46 issueType: 'Warning',
47 severity: 'Important',
48 level: 'Opportunity for Excellence',
49 group: 'Accessibility'
50 };
51 return Rule;
52}(Lint.Rules.AbstractRule));
53exports.Rule = Rule;
54var A11yRoleRuleWalker = (function (_super) {
55 __extends(A11yRoleRuleWalker, _super);
56 function A11yRoleRuleWalker() {
57 return _super !== null && _super.apply(this, arguments) || this;
58 }
59 A11yRoleRuleWalker.prototype.visitJsxAttribute = function (node) {
60 var name = JsxAttribute_1.getPropName(node);
61 if (!name || name.toLowerCase() !== 'role') {
62 return;
63 }
64 var roleValue = JsxAttribute_1.getStringLiteral(node);
65 if (roleValue) {
66 var normalizedValues = roleValue.toLowerCase().split(' ');
67 if (normalizedValues.some(function (value) { return value && VALID_ROLES.indexOf(value) === -1; })) {
68 this.addFailureAt(node.getStart(), node.getWidth(), getFailureStringInvalidRole(roleValue));
69 }
70 }
71 else if (roleValue === '' || JsxAttribute_1.isEmpty(node)) {
72 this.addFailureAt(node.getStart(), node.getWidth(), getFailureStringUndefinedRole());
73 }
74 _super.prototype.visitJsxAttribute.call(this, node);
75 };
76 return A11yRoleRuleWalker;
77}(Lint.RuleWalker));
78//# sourceMappingURL=reactA11yRoleRule.js.map
\No newline at end of file