UNPKG

3.64 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 getImplicitRole_1 = require("./utils/getImplicitRole");
21var DOM_SCHEMA = require('./utils/attributes/domSchema.json');
22var FAILURE_STRING = 'Elements with event handlers must have role attribute.';
23var ROLE_STRING = 'role';
24var TARGET_EVENTS = [
25 'click',
26 'keyup',
27 'keydown',
28 'keypress',
29 'mousedown',
30 'mouseup',
31 'mousemove',
32 'mouseout',
33 'mouseover',
34 'onclick',
35 'onkeyup',
36 'onkeydown',
37 'onkeypress',
38 'onmousedown',
39 'onmouseup',
40 'onmousemove',
41 'onmouseout',
42 'onmouseover'
43];
44var Rule = (function (_super) {
45 __extends(Rule, _super);
46 function Rule() {
47 return _super !== null && _super.apply(this, arguments) || this;
48 }
49 Rule.prototype.apply = function (sourceFile) {
50 return sourceFile.languageVariant === ts.LanguageVariant.JSX ? this.applyWithFunction(sourceFile, walk) : [];
51 };
52 Rule.metadata = {
53 ruleName: 'react-a11y-event-has-role',
54 type: 'maintainability',
55 description: 'Elements with event handlers must have role attribute.',
56 rationale: "References:\n <ul>\n <li><a href=\"http://oaa-accessibility.org/wcag20/rule/94\">WCAG Rule 94</a></li>\n <li><a href=\"https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role\">\n Using the button role\n </a></li>\n </ul>\n ",
57 options: null,
58 optionsDescription: '',
59 typescriptOnly: true,
60 issueClass: 'Non-SDL',
61 issueType: 'Warning',
62 severity: 'Important',
63 level: 'Opportunity for Excellence',
64 group: 'Accessibility'
65 };
66 return Rule;
67}(Lint.Rules.AbstractRule));
68exports.Rule = Rule;
69function walk(ctx) {
70 function checkJsxOpeningElement(node) {
71 var tagName = node.tagName.getText();
72 if (!DOM_SCHEMA[tagName]) {
73 return;
74 }
75 var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
76 var events = TARGET_EVENTS.filter(function (eventName) { return !!attributes[eventName]; });
77 var hasAriaRole = !!attributes[ROLE_STRING] || !!getImplicitRole_1.getImplicitRole(node);
78 if (events.length > 0 && !hasAriaRole) {
79 ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING);
80 }
81 }
82 function cb(node) {
83 if (tsutils.isJsxElement(node)) {
84 checkJsxOpeningElement(node.openingElement);
85 }
86 else if (tsutils.isJsxSelfClosingElement(node)) {
87 checkJsxOpeningElement(node);
88 }
89 return ts.forEachChild(node, cb);
90 }
91 return ts.forEachChild(ctx.sourceFile, cb);
92}
93//# sourceMappingURL=reactA11yEventHasRoleRule.js.map
\No newline at end of file