role-supports-aria-props.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  8. var _ariaQuery = require("aria-query");
  9. var _jsxAstUtils = require("jsx-ast-utils");
  10. var _schemas = require("../util/schemas");
  11. var _getElementType = _interopRequireDefault(require("../util/getElementType"));
  12. var _getImplicitRole = _interopRequireDefault(require("../util/getImplicitRole"));
  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. // Rule Definition
  20. // ----------------------------------------------------------------------------
  21. var errorMessage = function errorMessage(attr, role, tag, isImplicit) {
  22. if (isImplicit) {
  23. return "The attribute ".concat(attr, " is not supported by the role ").concat(role, ". This role is implicit on the element ").concat(tag, ".");
  24. }
  25. return "The attribute ".concat(attr, " is not supported by the role ").concat(role, ".");
  26. };
  27. var schema = (0, _schemas.generateObjSchema)();
  28. var _default = {
  29. meta: {
  30. docs: {
  31. url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/role-supports-aria-props.md',
  32. description: 'Enforce that elements with explicit or implicit roles defined contain only `aria-*` properties supported by that `role`.'
  33. },
  34. schema: [schema]
  35. },
  36. create(context) {
  37. var elementType = (0, _getElementType["default"])(context);
  38. return {
  39. JSXOpeningElement(node) {
  40. // If role is not explicitly defined, then try and get its implicit role.
  41. var type = elementType(node);
  42. var role = (0, _jsxAstUtils.getProp)(node.attributes, 'role');
  43. var roleValue = role ? (0, _jsxAstUtils.getLiteralPropValue)(role) : (0, _getImplicitRole["default"])(type, node.attributes);
  44. var isImplicit = roleValue && role === undefined; // 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. } // Make sure it has no aria-* properties defined outside of its property set.
  50. var _roles$get = _ariaQuery.roles.get(roleValue),
  51. propKeyValues = _roles$get.props;
  52. var invalidAriaPropsForRole = (0, _toConsumableArray2["default"])(_ariaQuery.aria.keys()).filter(function (attribute) {
  53. return !(attribute in propKeyValues);
  54. });
  55. node.attributes.filter(function (prop) {
  56. return (0, _jsxAstUtils.getPropValue)(prop) != null // Ignore the attribute if its value is null or undefined.
  57. && prop.type !== 'JSXSpreadAttribute' // Ignore the attribute if it's a spread.
  58. ;
  59. }).forEach(function (prop) {
  60. var name = (0, _jsxAstUtils.propName)(prop);
  61. if (invalidAriaPropsForRole.indexOf(name) > -1) {
  62. context.report({
  63. node,
  64. message: errorMessage(name, roleValue, type, isImplicit)
  65. });
  66. }
  67. });
  68. }
  69. };
  70. }
  71. };
  72. exports["default"] = _default;
  73. module.exports = exports.default;