1 | "use strict";
|
2 | var __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 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var ts = require("typescript");
|
14 | var Lint = require("tslint");
|
15 | var JsxAttribute_1 = require("./utils/JsxAttribute");
|
16 | var getImplicitRole_1 = require("./utils/getImplicitRole");
|
17 | var DOM_SCHEMA = require('./utils/attributes/domSchema.json');
|
18 | var FAILURE_STRING = 'Elements with event handlers must have role attribute.';
|
19 | var ROLE_STRING = 'role';
|
20 | var TARGET_EVENTS = ['click', 'keyup', 'keydown', 'keypress', 'mousedown', 'mouseup',
|
21 | 'mousemove', 'mouseout', 'mouseover', 'onclick', 'onkeyup', 'onkeydown', 'onkeypress', 'onmousedown',
|
22 | 'onmouseup', 'onmousemove', 'onmouseout', 'onmouseover'];
|
23 | var 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));
|
48 | exports.Rule = Rule;
|
49 | var 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 |
|
\ | No newline at end of file |