| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports["default"] = void 0;
|
|---|
| 7 | var _ariaQuery = require("aria-query");
|
|---|
| 8 | var _jsxAstUtils = require("jsx-ast-utils");
|
|---|
| 9 | var _schemas = require("../util/schemas");
|
|---|
| 10 | var _getElementType = _interopRequireDefault(require("../util/getElementType"));
|
|---|
| 11 | var _isSemanticRoleElement = _interopRequireDefault(require("../util/isSemanticRoleElement"));
|
|---|
| 12 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|---|
| 13 | /**
|
|---|
| 14 | * @fileoverview Enforce that elements with ARIA roles must
|
|---|
| 15 | * have all required attributes for that role.
|
|---|
| 16 | * @author Ethan Cohen
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | // ----------------------------------------------------------------------------
|
|---|
| 20 | // Rule Definition
|
|---|
| 21 | // ----------------------------------------------------------------------------
|
|---|
| 22 |
|
|---|
| 23 | var errorMessage = function errorMessage(role, requiredProps) {
|
|---|
| 24 | return "Elements with the ARIA role \"".concat(role, "\" must have the following attributes defined: ").concat(String(requiredProps).toLowerCase());
|
|---|
| 25 | };
|
|---|
| 26 | var schema = (0, _schemas.generateObjSchema)();
|
|---|
| 27 | var roleKeys = _ariaQuery.roles.keys();
|
|---|
| 28 | var _default = exports["default"] = {
|
|---|
| 29 | meta: {
|
|---|
| 30 | docs: {
|
|---|
| 31 | url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/role-has-required-aria-props.md',
|
|---|
| 32 | description: 'Enforce that elements with ARIA roles must have all required attributes for that role.'
|
|---|
| 33 | },
|
|---|
| 34 | schema: [schema]
|
|---|
| 35 | },
|
|---|
| 36 | create: function create(context) {
|
|---|
| 37 | var elementType = (0, _getElementType["default"])(context);
|
|---|
| 38 | return {
|
|---|
| 39 | JSXAttribute: function JSXAttribute(attribute) {
|
|---|
| 40 | var name = (0, _jsxAstUtils.propName)(attribute).toLowerCase();
|
|---|
| 41 | if (name !== 'role') {
|
|---|
| 42 | return;
|
|---|
| 43 | }
|
|---|
| 44 | var type = elementType(attribute.parent);
|
|---|
| 45 | if (!_ariaQuery.dom.get(type)) {
|
|---|
| 46 | return;
|
|---|
| 47 | }
|
|---|
| 48 | var roleAttrValue = (0, _jsxAstUtils.getLiteralPropValue)(attribute);
|
|---|
| 49 | var attributes = attribute.parent.attributes;
|
|---|
| 50 |
|
|---|
| 51 | // If value is undefined, then the role attribute will be dropped in the DOM.
|
|---|
| 52 | // If value is null, then getLiteralAttributeValue is telling us
|
|---|
| 53 | // that the value isn't in the form of a literal.
|
|---|
| 54 | if (roleAttrValue === undefined || roleAttrValue === null) {
|
|---|
| 55 | return;
|
|---|
| 56 | }
|
|---|
| 57 | var normalizedValues = String(roleAttrValue).toLowerCase().split(' ');
|
|---|
| 58 | var validRoles = normalizedValues.filter(function (val) {
|
|---|
| 59 | return roleKeys.indexOf(val) > -1;
|
|---|
| 60 | });
|
|---|
| 61 |
|
|---|
| 62 | // Check semantic DOM elements
|
|---|
| 63 | // For example, <input type="checkbox" role="switch" />
|
|---|
| 64 | if ((0, _isSemanticRoleElement["default"])(type, attributes)) {
|
|---|
| 65 | return;
|
|---|
| 66 | }
|
|---|
| 67 | // Check arbitrary DOM elements
|
|---|
| 68 | validRoles.forEach(function (role) {
|
|---|
| 69 | var _roles$get = _ariaQuery.roles.get(role),
|
|---|
| 70 | requiredPropKeyValues = _roles$get.requiredProps;
|
|---|
| 71 | var requiredProps = Object.keys(requiredPropKeyValues);
|
|---|
| 72 | if (requiredProps.length > 0) {
|
|---|
| 73 | var hasRequiredProps = requiredProps.every(function (prop) {
|
|---|
| 74 | return (0, _jsxAstUtils.getProp)(attributes, prop);
|
|---|
| 75 | });
|
|---|
| 76 | if (hasRequiredProps === false) {
|
|---|
| 77 | context.report({
|
|---|
| 78 | node: attribute,
|
|---|
| 79 | message: errorMessage(role.toLowerCase(), requiredProps)
|
|---|
| 80 | });
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | });
|
|---|
| 84 | }
|
|---|
| 85 | };
|
|---|
| 86 | }
|
|---|
| 87 | };
|
|---|
| 88 | module.exports = exports.default; |
|---|