| 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 _getImplicitRole = _interopRequireDefault(require("../util/getImplicitRole"));
|
|---|
| 12 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|---|
| 13 | /**
|
|---|
| 14 | * @fileoverview Enforce that elements with explicit or implicit roles defined contain only
|
|---|
| 15 | * `aria-*` properties supported by that `role`.
|
|---|
| 16 | * @author Ethan Cohen
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | // ----------------------------------------------------------------------------
|
|---|
| 20 | // Rule Definition
|
|---|
| 21 | // ----------------------------------------------------------------------------
|
|---|
| 22 |
|
|---|
| 23 | var errorMessage = function errorMessage(attr, role, tag, isImplicit) {
|
|---|
| 24 | if (isImplicit) {
|
|---|
| 25 | return "The attribute ".concat(attr, " is not supported by the role ").concat(role, ". This role is implicit on the element ").concat(tag, ".");
|
|---|
| 26 | }
|
|---|
| 27 | return "The attribute ".concat(attr, " is not supported by the role ").concat(role, ".");
|
|---|
| 28 | };
|
|---|
| 29 | var schema = (0, _schemas.generateObjSchema)();
|
|---|
| 30 | var _default = exports["default"] = {
|
|---|
| 31 | meta: {
|
|---|
| 32 | docs: {
|
|---|
| 33 | url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/role-supports-aria-props.md',
|
|---|
| 34 | description: 'Enforce that elements with explicit or implicit roles defined contain only `aria-*` properties supported by that `role`.'
|
|---|
| 35 | },
|
|---|
| 36 | schema: [schema]
|
|---|
| 37 | },
|
|---|
| 38 | create(context) {
|
|---|
| 39 | var elementType = (0, _getElementType["default"])(context);
|
|---|
| 40 | return {
|
|---|
| 41 | JSXOpeningElement(node) {
|
|---|
| 42 | // If role is not explicitly defined, then try and get its implicit role.
|
|---|
| 43 | var type = elementType(node);
|
|---|
| 44 | var role = (0, _jsxAstUtils.getProp)(node.attributes, 'role');
|
|---|
| 45 | var roleValue = role ? (0, _jsxAstUtils.getLiteralPropValue)(role) : (0, _getImplicitRole["default"])(type, node.attributes);
|
|---|
| 46 | var isImplicit = roleValue && role === undefined;
|
|---|
| 47 |
|
|---|
| 48 | // If there is no explicit or implicit role, then assume that the element
|
|---|
| 49 | // can handle the global set of aria-* properties.
|
|---|
| 50 | // This actually isn't true - should fix in future release.
|
|---|
| 51 | if (typeof roleValue !== 'string' || _ariaQuery.roles.get(roleValue) === undefined) {
|
|---|
| 52 | return;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | // Make sure it has no aria-* properties defined outside its property set.
|
|---|
| 56 | var _roles$get = _ariaQuery.roles.get(roleValue),
|
|---|
| 57 | propKeyValues = _roles$get.props;
|
|---|
| 58 | var invalidAriaPropsForRole = new Set(_ariaQuery.aria.keys().filter(function (attribute) {
|
|---|
| 59 | return !(attribute in propKeyValues);
|
|---|
| 60 | }));
|
|---|
| 61 | node.attributes.filter(function (prop) {
|
|---|
| 62 | return (0, _jsxAstUtils.getPropValue)(prop) != null // Ignore the attribute if its value is null or undefined.
|
|---|
| 63 | && prop.type !== 'JSXSpreadAttribute' // Ignore the attribute if it's a spread.
|
|---|
| 64 | ;
|
|---|
| 65 | }).forEach(function (prop) {
|
|---|
| 66 | var name = (0, _jsxAstUtils.propName)(prop);
|
|---|
| 67 | if (invalidAriaPropsForRole.has(name)) {
|
|---|
| 68 | context.report({
|
|---|
| 69 | node,
|
|---|
| 70 | message: errorMessage(name, roleValue, type, isImplicit)
|
|---|
| 71 | });
|
|---|
| 72 | }
|
|---|
| 73 | });
|
|---|
| 74 | }
|
|---|
| 75 | };
|
|---|
| 76 | }
|
|---|
| 77 | };
|
|---|
| 78 | module.exports = exports.default; |
|---|