| 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 _getElementType = _interopRequireDefault(require("../util/getElementType"));
|
|---|
| 10 | var _schemas = require("../util/schemas");
|
|---|
| 11 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|---|
| 12 | /**
|
|---|
| 13 | * @fileoverview Enforce aria role attribute is valid.
|
|---|
| 14 | * @author Ethan Cohen
|
|---|
| 15 | */
|
|---|
| 16 |
|
|---|
| 17 | // ----------------------------------------------------------------------------
|
|---|
| 18 | // Rule Definition
|
|---|
| 19 | // ----------------------------------------------------------------------------
|
|---|
| 20 |
|
|---|
| 21 | var errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';
|
|---|
| 22 | var schema = (0, _schemas.generateObjSchema)({
|
|---|
| 23 | allowedInvalidRoles: {
|
|---|
| 24 | items: {
|
|---|
| 25 | type: 'string'
|
|---|
| 26 | },
|
|---|
| 27 | type: 'array',
|
|---|
| 28 | uniqueItems: true
|
|---|
| 29 | },
|
|---|
| 30 | ignoreNonDOM: {
|
|---|
| 31 | type: 'boolean',
|
|---|
| 32 | "default": false
|
|---|
| 33 | }
|
|---|
| 34 | });
|
|---|
| 35 | var validRoles = new Set(_ariaQuery.roles.keys().filter(function (role) {
|
|---|
| 36 | return _ariaQuery.roles.get(role)["abstract"] === false;
|
|---|
| 37 | }));
|
|---|
| 38 | var _default = exports["default"] = {
|
|---|
| 39 | meta: {
|
|---|
| 40 | docs: {
|
|---|
| 41 | url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-role.md',
|
|---|
| 42 | description: 'Enforce that elements with ARIA roles must use a valid, non-abstract ARIA role.'
|
|---|
| 43 | },
|
|---|
| 44 | schema: [schema]
|
|---|
| 45 | },
|
|---|
| 46 | create: function create(context) {
|
|---|
| 47 | var options = context.options[0] || {};
|
|---|
| 48 | var ignoreNonDOM = !!options.ignoreNonDOM;
|
|---|
| 49 | var allowedInvalidRoles = new Set(options.allowedInvalidRoles || []);
|
|---|
| 50 | var elementType = (0, _getElementType["default"])(context);
|
|---|
| 51 | return {
|
|---|
| 52 | JSXAttribute: function JSXAttribute(attribute) {
|
|---|
| 53 | // If ignoreNonDOM and the parent isn't DOM, don't run rule.
|
|---|
| 54 | if (ignoreNonDOM) {
|
|---|
| 55 | var type = elementType(attribute.parent);
|
|---|
| 56 | if (!_ariaQuery.dom.get(type)) {
|
|---|
| 57 | return;
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | // Get prop name
|
|---|
| 62 | var name = (0, _jsxAstUtils.propName)(attribute).toUpperCase();
|
|---|
| 63 | if (name !== 'ROLE') {
|
|---|
| 64 | return;
|
|---|
| 65 | }
|
|---|
| 66 | var value = (0, _jsxAstUtils.getLiteralPropValue)(attribute);
|
|---|
| 67 |
|
|---|
| 68 | // If value is undefined, then the role attribute will be dropped in the DOM.
|
|---|
| 69 | // If value is null, then getLiteralAttributeValue is telling us that the
|
|---|
| 70 | // value isn't in the form of a literal.
|
|---|
| 71 | if (value === undefined || value === null) {
|
|---|
| 72 | return;
|
|---|
| 73 | }
|
|---|
| 74 | var values = String(value).split(' ');
|
|---|
| 75 | var isValid = values.every(function (val) {
|
|---|
| 76 | return allowedInvalidRoles.has(val) || validRoles.has(val);
|
|---|
| 77 | });
|
|---|
| 78 | if (isValid === true) {
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 | context.report({
|
|---|
| 82 | node: attribute,
|
|---|
| 83 | message: errorMessage
|
|---|
| 84 | });
|
|---|
| 85 | }
|
|---|
| 86 | };
|
|---|
| 87 | }
|
|---|
| 88 | };
|
|---|
| 89 | module.exports = exports.default; |
|---|