1 | "use strict";
|
2 | var __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 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var ts = require("typescript");
|
17 | var Lint = require("tslint");
|
18 | var tsutils = require("tsutils");
|
19 | var getImplicitRole_1 = require("./utils/getImplicitRole");
|
20 | var JsxAttribute_1 = require("./utils/JsxAttribute");
|
21 | var ROLES_SCHEMA = require('./utils/attributes/roleSchema.json');
|
22 | var ROLES = ROLES_SCHEMA.roles;
|
23 | var ARIA_ATTRIBUTES = require('./utils/attributes/ariaSchema.json');
|
24 | var ROLE_STRING = 'role';
|
25 | var TAGS_WITH_ARIA_LEVEL = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
|
26 | function getFailureStringForNotImplicitRole(roleNamesInElement, missingProps) {
|
27 | return "Element with ARIA role(s) '" + roleNamesInElement.join(', ') + "' are missing required attribute(s): " + missingProps.join(', ') + ". A reference to role definitions can be found at https://www.w3.org/TR/wai-aria/roles#role_definitions.";
|
28 | }
|
29 | exports.getFailureStringForNotImplicitRole = getFailureStringForNotImplicitRole;
|
30 | function getFailureStringForImplicitRole(tagName, roleNamesInElement, missingProps) {
|
31 | return "Tag '" + tagName + "' has implicit role '" + roleNamesInElement + "'. It requires aria-* attributes: " + missingProps.join(', ') + " that are missing in the element. A reference to role definitions can be found at https://www.w3.org/TR/wai-aria/roles#role_definitions.";
|
32 | }
|
33 | exports.getFailureStringForImplicitRole = getFailureStringForImplicitRole;
|
34 | var Rule = (function (_super) {
|
35 | __extends(Rule, _super);
|
36 | function Rule() {
|
37 | return _super !== null && _super.apply(this, arguments) || this;
|
38 | }
|
39 | Rule.prototype.apply = function (sourceFile) {
|
40 | return sourceFile.languageVariant === ts.LanguageVariant.JSX ? this.applyWithFunction(sourceFile, walk) : [];
|
41 | };
|
42 | Rule.metadata = {
|
43 | ruleName: 'react-a11y-role-has-required-aria-props',
|
44 | type: 'maintainability',
|
45 | description: 'Elements with aria roles must have all required attributes according to the role.',
|
46 | rationale: "References:\n <ul>\n <li><a href=\"https://www.w3.org/TR/wai-aria/roles#role_definitions\">ARIA Definition of Roles</a></li>\n <li><a href=\"http://oaa-accessibility.org/wcag20/rule/90\">WCAG Rule 90: Required properties and states should be defined</a></li>\n <li><a href=\"http://oaa-accessibility.org/wcag20/rule/91\">WCAG Rule 91: Required properties and states must not be empty</a></li>\n </ul>",
|
47 | options: null,
|
48 | optionsDescription: '',
|
49 | typescriptOnly: true,
|
50 | issueClass: 'Non-SDL',
|
51 | issueType: 'Warning',
|
52 | severity: 'Important',
|
53 | level: 'Opportunity for Excellence',
|
54 | group: 'Accessibility'
|
55 | };
|
56 | return Rule;
|
57 | }(Lint.Rules.AbstractRule));
|
58 | exports.Rule = Rule;
|
59 | function walk(ctx) {
|
60 | function checkJsxElement(node) {
|
61 | var tagName = node.tagName.getText();
|
62 | var attributesInElement = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
|
63 | var roleProp = attributesInElement[ROLE_STRING];
|
64 | var roleValue = roleProp ? JsxAttribute_1.getStringLiteral(roleProp) : getImplicitRole_1.getImplicitRole(node);
|
65 | var isImplicitRole = !roleProp && !!roleValue;
|
66 | var normalizedRoles = (roleValue || '')
|
67 | .toLowerCase()
|
68 | .split(' ')
|
69 | .filter(function (role) { return !!ROLES[role]; });
|
70 | if (normalizedRoles.length === 0) {
|
71 | return;
|
72 | }
|
73 | var requiredAttributeNames = [];
|
74 | normalizedRoles.forEach(function (role) {
|
75 | requiredAttributeNames = requiredAttributeNames.concat(ROLES[role].requiredProps || []);
|
76 | });
|
77 | var attributeNamesInElement = Object.keys(attributesInElement)
|
78 | .filter(function (attributeName) { return !!ARIA_ATTRIBUTES[attributeName.toLowerCase()]; })
|
79 | .concat(TAGS_WITH_ARIA_LEVEL.indexOf(tagName) === -1 ? [] : ['aria-level']);
|
80 | var missingAttributes = requiredAttributeNames.filter(function (attributeName) { return attributeNamesInElement.indexOf(attributeName) === -1; });
|
81 | if (missingAttributes.length > 0) {
|
82 | ctx.addFailureAt(node.getStart(), node.getWidth(), isImplicitRole
|
83 | ? getFailureStringForImplicitRole(node.tagName.getText(), normalizedRoles[0], missingAttributes)
|
84 | : getFailureStringForNotImplicitRole(normalizedRoles, missingAttributes));
|
85 | }
|
86 | }
|
87 | function cb(node) {
|
88 | if (tsutils.isJsxElement(node)) {
|
89 | checkJsxElement(node.openingElement);
|
90 | }
|
91 | else if (tsutils.isJsxSelfClosingElement(node)) {
|
92 | checkJsxElement(node);
|
93 | }
|
94 | return ts.forEachChild(node, cb);
|
95 | }
|
96 | return ts.forEachChild(ctx.sourceFile, cb);
|
97 | }
|
98 |
|
\ | No newline at end of file |