no-noninteractive-element-interactions.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 _arrayIncludes = _interopRequireDefault(require("array-includes"));
  7. var _has = _interopRequireDefault(require("has"));
  8. var _schemas = require("../util/schemas");
  9. var _isAbstractRole = _interopRequireDefault(require("../util/isAbstractRole"));
  10. var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
  11. var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
  12. var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
  13. var _isNonInteractiveElement = _interopRequireDefault(require("../util/isNonInteractiveElement"));
  14. var _isNonInteractiveRole = _interopRequireDefault(require("../util/isNonInteractiveRole"));
  15. var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
  16. /**
  17. * @fileoverview Enforce non-interactive elements have no interactive handlers.
  18. * @author Jese Beach
  19. *
  20. */
  21. // ----------------------------------------------------------------------------
  22. // Rule Definition
  23. // ----------------------------------------------------------------------------
  24. var errorMessage = 'Non-interactive elements should not be assigned mouse or keyboard event listeners.';
  25. var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
  26. var defaultInteractiveProps = [].concat((0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.focus), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.image), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.keyboard), (0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.mouse));
  27. var schema = (0, _schemas.generateObjSchema)({
  28. handlers: _schemas.arraySchema
  29. });
  30. module.exports = {
  31. meta: {
  32. docs: {
  33. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-noninteractive-element-interactions.md'
  34. },
  35. schema: [schema]
  36. },
  37. create: function create(context) {
  38. var options = context.options;
  39. return {
  40. JSXOpeningElement: function JSXOpeningElement(node) {
  41. var attributes = node.attributes;
  42. var type = (0, _jsxAstUtils.elementType)(node);
  43. var config = options[0] || {};
  44. var interactiveProps = config.handlers || defaultInteractiveProps; // Allow overrides from rule configuration for specific elements and roles.
  45. if ((0, _has["default"])(config, type)) {
  46. attributes = attributes.filter(function (attr) {
  47. return attr.type !== 'JSXSpreadAttribute' && !(0, _arrayIncludes["default"])(config[type], (0, _jsxAstUtils.propName)(attr));
  48. });
  49. }
  50. var hasInteractiveProps = interactiveProps.some(function (prop) {
  51. return (0, _jsxAstUtils.hasProp)(attributes, prop) && (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(attributes, prop)) != null;
  52. });
  53. if (!(0, _arrayIncludes["default"])(domElements, type)) {
  54. // Do not test higher level JSX components, as we do not know what
  55. // low-level DOM element this maps to.
  56. return;
  57. }
  58. if (!hasInteractiveProps || (0, _isHiddenFromScreenReader["default"])(type, attributes) || (0, _isPresentationRole["default"])(type, attributes)) {
  59. // Presentation is an intentional signal from the author that this
  60. // element is not meant to be perceivable. For example, a click screen
  61. // to close a dialog .
  62. return;
  63. }
  64. if ((0, _isInteractiveElement["default"])(type, attributes) || (0, _isInteractiveRole["default"])(type, attributes) || !(0, _isNonInteractiveElement["default"])(type, attributes) && !(0, _isNonInteractiveRole["default"])(type, attributes) || (0, _isAbstractRole["default"])(type, attributes)) {
  65. // This rule has no opinion about abtract roles.
  66. return;
  67. } // Visible, non-interactive elements should not have an interactive handler.
  68. context.report({
  69. node,
  70. message: errorMessage
  71. });
  72. }
  73. };
  74. }
  75. };