mouse-events-have-key-events.js 7.2 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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  8. var _ariaQuery = require("aria-query");
  9. var _jsxAstUtils = require("jsx-ast-utils");
  10. var _schemas = require("../util/schemas");
  11. function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
  12. function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /**
  13. * @fileoverview Enforce onmouseover/onmouseout are
  14. * accompanied by onfocus/onblur.
  15. * @author Ethan Cohen
  16. *
  17. */ // ----------------------------------------------------------------------------
  18. // Rule Definition
  19. // ----------------------------------------------------------------------------
  20. var schema = (0, _schemas.generateObjSchema)({
  21. hoverInHandlers: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
  22. description: 'An array of events that need to be accompanied by `onFocus`'
  23. }),
  24. hoverOutHandlers: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
  25. description: 'An array of events that need to be accompanied by `onBlur`'
  26. })
  27. });
  28. // Use `onMouseOver` and `onMouseOut` by default if no config is
  29. // passed in for backwards compatibility
  30. var DEFAULT_HOVER_IN_HANDLERS = ['onMouseOver'];
  31. var DEFAULT_HOVER_OUT_HANDLERS = ['onMouseOut'];
  32. var _default = exports["default"] = {
  33. meta: {
  34. docs: {
  35. url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/mouse-events-have-key-events.md',
  36. description: 'Enforce that `onMouseOver`/`onMouseOut` are accompanied by `onFocus`/`onBlur` for keyboard-only users.'
  37. },
  38. schema: [schema]
  39. },
  40. create: function create(context) {
  41. return {
  42. JSXOpeningElement: function JSXOpeningElement(node) {
  43. var _options$0$hoverInHan, _options$, _options$0$hoverOutHa, _options$2;
  44. var name = node.name.name;
  45. if (!_ariaQuery.dom.get(name)) {
  46. return;
  47. }
  48. var options = context.options;
  49. var hoverInHandlers = (_options$0$hoverInHan = (_options$ = options[0]) === null || _options$ === void 0 ? void 0 : _options$.hoverInHandlers) !== null && _options$0$hoverInHan !== void 0 ? _options$0$hoverInHan : DEFAULT_HOVER_IN_HANDLERS;
  50. var hoverOutHandlers = (_options$0$hoverOutHa = (_options$2 = options[0]) === null || _options$2 === void 0 ? void 0 : _options$2.hoverOutHandlers) !== null && _options$0$hoverOutHa !== void 0 ? _options$0$hoverOutHa : DEFAULT_HOVER_OUT_HANDLERS;
  51. var attributes = node.attributes;
  52. // Check hover in / onfocus pairing
  53. var firstHoverInHandlerWithValue = hoverInHandlers.find(function (handler) {
  54. var prop = (0, _jsxAstUtils.getProp)(attributes, handler);
  55. var propValue = (0, _jsxAstUtils.getPropValue)(prop);
  56. return propValue != null;
  57. });
  58. if (firstHoverInHandlerWithValue != null) {
  59. var hasOnFocus = (0, _jsxAstUtils.getProp)(attributes, 'onFocus');
  60. var onFocusValue = (0, _jsxAstUtils.getPropValue)(hasOnFocus);
  61. if (hasOnFocus === false || onFocusValue === null || onFocusValue === undefined) {
  62. context.report({
  63. node: (0, _jsxAstUtils.getProp)(attributes, firstHoverInHandlerWithValue),
  64. message: "".concat(firstHoverInHandlerWithValue, " must be accompanied by onFocus for accessibility.")
  65. });
  66. }
  67. }
  68. // Check hover out / onblur pairing
  69. var firstHoverOutHandlerWithValue = hoverOutHandlers.find(function (handler) {
  70. var prop = (0, _jsxAstUtils.getProp)(attributes, handler);
  71. var propValue = (0, _jsxAstUtils.getPropValue)(prop);
  72. return propValue != null;
  73. });
  74. if (firstHoverOutHandlerWithValue != null) {
  75. var hasOnBlur = (0, _jsxAstUtils.getProp)(attributes, 'onBlur');
  76. var onBlurValue = (0, _jsxAstUtils.getPropValue)(hasOnBlur);
  77. if (hasOnBlur === false || onBlurValue === null || onBlurValue === undefined) {
  78. context.report({
  79. node: (0, _jsxAstUtils.getProp)(attributes, firstHoverOutHandlerWithValue),
  80. message: "".concat(firstHoverOutHandlerWithValue, " must be accompanied by onBlur for accessibility.")
  81. });
  82. }
  83. }
  84. }
  85. };
  86. }
  87. };
  88. module.exports = exports.default;