role-supports-aria-props.js 3.3 KB

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