mouse-events-have-key-events.js 5.3 KB

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