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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. var _ariaQuery = require("aria-query");
  3. var _jsxAstUtils = require("jsx-ast-utils");
  4. var _schemas = require("../util/schemas");
  5. /**
  6. * @fileoverview Enforce onmouseover/onmouseout are
  7. * accompanied by onfocus/onblur.
  8. * @author Ethan Cohen
  9. */
  10. // ----------------------------------------------------------------------------
  11. // Rule Definition
  12. // ----------------------------------------------------------------------------
  13. var mouseOverErrorMessage = 'onMouseOver must be accompanied by onFocus for accessibility.';
  14. var mouseOutErrorMessage = 'onMouseOut must be accompanied by onBlur for accessibility.';
  15. var schema = (0, _schemas.generateObjSchema)();
  16. module.exports = {
  17. meta: {
  18. docs: {
  19. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/mouse-events-have-key-events.md'
  20. },
  21. schema: [schema]
  22. },
  23. create: function create(context) {
  24. return {
  25. JSXOpeningElement: function JSXOpeningElement(node) {
  26. var name = node.name.name;
  27. if (!_ariaQuery.dom.get(name)) {
  28. return;
  29. }
  30. var attributes = node.attributes; // Check onmouseover / onfocus pairing.
  31. var onMouseOver = (0, _jsxAstUtils.getProp)(attributes, 'onMouseOver');
  32. var onMouseOverValue = (0, _jsxAstUtils.getPropValue)(onMouseOver);
  33. if (onMouseOver && onMouseOverValue != null) {
  34. var hasOnFocus = (0, _jsxAstUtils.getProp)(attributes, 'onFocus');
  35. var onFocusValue = (0, _jsxAstUtils.getPropValue)(hasOnFocus);
  36. if (hasOnFocus === false || onFocusValue === null || onFocusValue === undefined) {
  37. context.report({
  38. node,
  39. message: mouseOverErrorMessage
  40. });
  41. }
  42. } // Checkout onmouseout / onblur pairing
  43. var onMouseOut = (0, _jsxAstUtils.getProp)(attributes, 'onMouseOut');
  44. var onMouseOutValue = (0, _jsxAstUtils.getPropValue)(onMouseOut);
  45. if (onMouseOut && onMouseOutValue != null) {
  46. var hasOnBlur = (0, _jsxAstUtils.getProp)(attributes, 'onBlur');
  47. var onBlurValue = (0, _jsxAstUtils.getPropValue)(hasOnBlur);
  48. if (hasOnBlur === false || onBlurValue === null || onBlurValue === undefined) {
  49. context.report({
  50. node,
  51. message: mouseOutErrorMessage
  52. });
  53. }
  54. }
  55. }
  56. };
  57. }
  58. };