aria-activedescendant-has-tabindex.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  4. var _ariaQuery = require("aria-query");
  5. var _jsxAstUtils = require("jsx-ast-utils");
  6. var _schemas = require("../util/schemas");
  7. var _getTabIndex = _interopRequireDefault(require("../util/getTabIndex"));
  8. var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
  9. /**
  10. * @fileoverview Enforce elements with aria-activedescendant are tabbable.
  11. * @author Jesse Beach <@jessebeach>
  12. */
  13. // ----------------------------------------------------------------------------
  14. // Rule Definition
  15. // ----------------------------------------------------------------------------
  16. var errorMessage = 'An element that manages focus with `aria-activedescendant` must have a tabindex';
  17. var schema = (0, _schemas.generateObjSchema)();
  18. var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-activedescendant-has-tabindex.md'
  23. },
  24. schema: [schema]
  25. },
  26. create: function create(context) {
  27. return {
  28. JSXOpeningElement: function JSXOpeningElement(node) {
  29. var attributes = node.attributes;
  30. if ((0, _jsxAstUtils.getProp)(attributes, 'aria-activedescendant') === undefined) {
  31. return;
  32. }
  33. var type = (0, _jsxAstUtils.elementType)(node); // Do not test higher level JSX components, as we do not know what
  34. // low-level DOM element this maps to.
  35. if (domElements.indexOf(type) === -1) {
  36. return;
  37. }
  38. var tabIndex = (0, _getTabIndex["default"])((0, _jsxAstUtils.getProp)(attributes, 'tabIndex')); // If this is an interactive element and the tabindex attribute is not specified,
  39. // or the tabIndex property was not mutated, then the tabIndex
  40. // property will be undefined.
  41. if ((0, _isInteractiveElement["default"])(type, attributes) && tabIndex === undefined) {
  42. return;
  43. }
  44. if (tabIndex >= -1) {
  45. return;
  46. }
  47. context.report({
  48. node,
  49. message: errorMessage
  50. });
  51. }
  52. };
  53. }
  54. };