no-noninteractive-element-to-interactive-role.js
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _ariaQuery = require("aria-query");
var _jsxAstUtils = require("jsx-ast-utils");
var _arrayIncludes = _interopRequireDefault(require("array-includes"));
var _has = _interopRequireDefault(require("has"));
var _getElementType = _interopRequireDefault(require("../util/getElementType"));
var _getExplicitRole = _interopRequireDefault(require("../util/getExplicitRole"));
var _isNonInteractiveElement = _interopRequireDefault(require("../util/isNonInteractiveElement"));
var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
/**
* @fileoverview Disallow inherently non-interactive elements to be assigned
* interactive roles.
* @author Jesse Beach
*
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var errorMessage = 'Non-interactive elements should not be assigned interactive roles.';
var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
var _default = {
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-noninteractive-element-to-interactive-role.md',
description: 'Non-interactive elements should not be assigned interactive roles.',
errorOptions: true
},
schema: [{
type: 'object',
additionalProperties: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true
}
}]
},
create: function create(context) {
var options = context.options;
var elementType = (0, _getElementType["default"])(context);
return {
JSXAttribute: function JSXAttribute(attribute) {
var attributeName = (0, _jsxAstUtils.propName)(attribute); // $FlowFixMe: [TODO] Mark propName as a JSXIdentifier, not a string.
if (attributeName !== 'role') {
return;
}
var node = attribute.parent;
var attributes = node.attributes;
var type = elementType(node);
var role = (0, _getExplicitRole["default"])(type, node.attributes);
if (!(0, _arrayIncludes["default"])(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
} // Allow overrides from rule configuration for specific elements and
// roles.
var allowedRoles = options[0] || {};
if ((0, _has["default"])(allowedRoles, type) && (0, _arrayIncludes["default"])(allowedRoles[type], role)) {
return;
}
if ((0, _isNonInteractiveElement["default"])(type, attributes) && (0, _isInteractiveRole["default"])(type, attributes)) {
context.report({
node: attribute,
message: errorMessage
});
}
}
};
}
};
exports["default"] = _default;
module.exports = exports.default;