no-static-element-interactions.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 _schemas = require("../util/schemas");
  8. var _isAbstractRole = _interopRequireDefault(require("../util/isAbstractRole"));
  9. var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
  10. var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
  11. var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
  12. var _isNonInteractiveElement = _interopRequireDefault(require("../util/isNonInteractiveElement"));
  13. var _isNonInteractiveRole = _interopRequireDefault(require("../util/isNonInteractiveRole"));
  14. var _isNonLiteralProperty = _interopRequireDefault(require("../util/isNonLiteralProperty"));
  15. var _isPresentationRole = _interopRequireDefault(require("../util/isPresentationRole"));
  16. /**
  17. * @fileoverview Enforce static elements have no interactive handlers.
  18. * @author Ethan Cohen
  19. *
  20. */
  21. // ----------------------------------------------------------------------------
  22. // Rule Definition
  23. // ----------------------------------------------------------------------------
  24. var errorMessage = 'Static HTML elements with event handlers require a role.';
  25. var domElements = (0, _toConsumableArray2["default"])(_ariaQuery.dom.keys());
  26. var defaultInteractiveProps = [].concat((0, _toConsumableArray2["default"])(_jsxAstUtils.eventHandlersByType.focus), (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-static-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 _ref = options[0] || {},
  44. allowExpressionValues = _ref.allowExpressionValues,
  45. _ref$handlers = _ref.handlers,
  46. handlers = _ref$handlers === void 0 ? defaultInteractiveProps : _ref$handlers;
  47. var hasInteractiveProps = handlers.some(function (prop) {
  48. return (0, _jsxAstUtils.hasProp)(attributes, prop) && (0, _jsxAstUtils.getPropValue)((0, _jsxAstUtils.getProp)(attributes, prop)) != null;
  49. });
  50. if (!(0, _arrayIncludes["default"])(domElements, type)) {
  51. // Do not test higher level JSX components, as we do not know what
  52. // low-level DOM element this maps to.
  53. return;
  54. }
  55. if (!hasInteractiveProps || (0, _isHiddenFromScreenReader["default"])(type, attributes) || (0, _isPresentationRole["default"])(type, attributes)) {
  56. // Presentation is an intentional signal from the author that this
  57. // element is not meant to be perceivable. For example, a click screen
  58. // to close a dialog .
  59. return;
  60. }
  61. 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)) {
  62. // This rule has no opinion about abstract roles.
  63. return;
  64. }
  65. if (allowExpressionValues === true && (0, _isNonLiteralProperty["default"])(attributes, 'role')) {
  66. // This rule has no opinion about non-literal roles.
  67. return;
  68. } // Visible, non-interactive elements should not have an interactive handler.
  69. context.report({
  70. node,
  71. message: errorMessage
  72. });
  73. }
  74. };
  75. }
  76. };