UNPKG

3.53 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 getImplicitRole_1 = require("./utils/getImplicitRole");
17var DOM_SCHEMA = require('./utils/attributes/domSchema.json');
18var FAILURE_STRING = 'Elements with event handlers must have role attribute.';
19var ROLE_STRING = 'role';
20var TARGET_EVENTS = ['click', 'keyup', 'keydown', 'keypress', 'mousedown', 'mouseup',
21 'mousemove', 'mouseout', 'mouseover', 'onclick', 'onkeyup', 'onkeydown', 'onkeypress', 'onmousedown',
22 'onmouseup', 'onmousemove', 'onmouseout', 'onmouseover'];
23var Rule = (function (_super) {
24 __extends(Rule, _super);
25 function Rule() {
26 return _super !== null && _super.apply(this, arguments) || this;
27 }
28 Rule.prototype.apply = function (sourceFile) {
29 return sourceFile.languageVariant === ts.LanguageVariant.JSX
30 ? this.applyWithWalker(new ReactA11yEventHasRoleWalker(sourceFile, this.getOptions()))
31 : [];
32 };
33 Rule.metadata = {
34 ruleName: 'react-a11y-event-has-role',
35 type: 'maintainability',
36 description: 'Elements with event handlers must have role attribute.',
37 options: null,
38 optionsDescription: '',
39 typescriptOnly: true,
40 issueClass: 'Non-SDL',
41 issueType: 'Warning',
42 severity: 'Important',
43 level: 'Opportunity for Excellence',
44 group: 'Accessibility'
45 };
46 return Rule;
47}(Lint.Rules.AbstractRule));
48exports.Rule = Rule;
49var ReactA11yEventHasRoleWalker = (function (_super) {
50 __extends(ReactA11yEventHasRoleWalker, _super);
51 function ReactA11yEventHasRoleWalker() {
52 return _super !== null && _super.apply(this, arguments) || this;
53 }
54 ReactA11yEventHasRoleWalker.prototype.visitJsxElement = function (node) {
55 this.checkJsxOpeningElement(node.openingElement);
56 _super.prototype.visitJsxElement.call(this, node);
57 };
58 ReactA11yEventHasRoleWalker.prototype.visitJsxSelfClosingElement = function (node) {
59 this.checkJsxOpeningElement(node);
60 _super.prototype.visitJsxSelfClosingElement.call(this, node);
61 };
62 ReactA11yEventHasRoleWalker.prototype.checkJsxOpeningElement = function (node) {
63 var tagName = node.tagName.getText();
64 if (!DOM_SCHEMA[tagName]) {
65 return;
66 }
67 var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
68 var events = TARGET_EVENTS.filter(function (eventName) { return !!attributes[eventName]; });
69 var hasAriaRole = !!attributes[ROLE_STRING] || !!getImplicitRole_1.getImplicitRole(node);
70 if (events.length > 0 && !hasAriaRole) {
71 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
72 }
73 };
74 return ReactA11yEventHasRoleWalker;
75}(Lint.RuleWalker));
76//# sourceMappingURL=reactA11yEventHasRoleRule.js.map
\No newline at end of file