UNPKG

3.94 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var JsxAttribute_1 = require("./utils/JsxAttribute");
20var ROLE_SCHEMA = require('./utils/attributes/roleSchema.json');
21var ROLES = ROLE_SCHEMA.roles;
22var VALID_ROLES = Object.keys(ROLES).filter(function (role) { return ROLES[role].isAbstract === false; });
23function getFailureStringUndefinedRole() {
24 return ("'role' attribute empty. Either select a role from https://www.w3.org/TR/wai-aria/roles#role_definitions, " +
25 'or simply remove this attribute');
26}
27exports.getFailureStringUndefinedRole = getFailureStringUndefinedRole;
28function getFailureStringInvalidRole(invalidRoleName) {
29 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.";
30}
31exports.getFailureStringInvalidRole = getFailureStringInvalidRole;
32var Rule = (function (_super) {
33 __extends(Rule, _super);
34 function Rule() {
35 return _super !== null && _super.apply(this, arguments) || this;
36 }
37 Rule.prototype.apply = function (sourceFile) {
38 return sourceFile.languageVariant === ts.LanguageVariant.JSX ? this.applyWithFunction(sourceFile, walk) : [];
39 };
40 Rule.metadata = {
41 ruleName: 'react-a11y-role',
42 type: 'maintainability',
43 description: 'Elements with aria roles must use a **valid**, **non-abstract** aria role. ' +
44 'A reference to role definitions can be found at [WAI-ARIA roles](https://www.w3.org/TR/wai-aria/roles#role_definitions).',
45 rationale: "References:\n <ul>\n <li><a href=\"http://oaa-accessibility.org/wcag20/rule/92\">WCAG Rule 92: Role value must be valid</a></li>\n </ul>",
46 options: null,
47 optionsDescription: '',
48 typescriptOnly: true,
49 issueClass: 'Non-SDL',
50 issueType: 'Warning',
51 severity: 'Important',
52 level: 'Opportunity for Excellence',
53 group: 'Accessibility'
54 };
55 return Rule;
56}(Lint.Rules.AbstractRule));
57exports.Rule = Rule;
58function walk(ctx) {
59 function cb(node) {
60 if (tsutils.isJsxAttribute(node)) {
61 var name_1 = JsxAttribute_1.getPropName(node);
62 if (!name_1 || name_1.toLowerCase() !== 'role') {
63 return;
64 }
65 var roleValue = JsxAttribute_1.getStringLiteral(node);
66 if (roleValue) {
67 var normalizedValues = roleValue.toLowerCase().split(' ');
68 if (normalizedValues.some(function (value) { return !!(value && VALID_ROLES.indexOf(value) === -1); })) {
69 ctx.addFailureAt(node.getStart(), node.getWidth(), getFailureStringInvalidRole(roleValue));
70 }
71 }
72 else if (roleValue === '' || JsxAttribute_1.isEmpty(node)) {
73 ctx.addFailureAt(node.getStart(), node.getWidth(), getFailureStringUndefinedRole());
74 }
75 }
76 return ts.forEachChild(node, cb);
77 }
78 return ts.forEachChild(ctx.sourceFile, cb);
79}
80//# sourceMappingURL=reactA11yRoleRule.js.map
\No newline at end of file