role-supports-aria-props.js 3.6 KB

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