aria-activedescendant-has-tabindex.js 2.5 KB

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