Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

107 строки
4.9 KiB

  1. 'use strict';
  2. var _ariaQuery = require('aria-query');
  3. var _jsxAstUtils = require('jsx-ast-utils');
  4. var _arrayIncludes = require('array-includes');
  5. var _arrayIncludes2 = _interopRequireDefault(_arrayIncludes);
  6. var _schemas = require('../util/schemas');
  7. var _isAbstractRole = require('../util/isAbstractRole');
  8. var _isAbstractRole2 = _interopRequireDefault(_isAbstractRole);
  9. var _isHiddenFromScreenReader = require('../util/isHiddenFromScreenReader');
  10. var _isHiddenFromScreenReader2 = _interopRequireDefault(_isHiddenFromScreenReader);
  11. var _isInteractiveElement = require('../util/isInteractiveElement');
  12. var _isInteractiveElement2 = _interopRequireDefault(_isInteractiveElement);
  13. var _isInteractiveRole = require('../util/isInteractiveRole');
  14. var _isInteractiveRole2 = _interopRequireDefault(_isInteractiveRole);
  15. var _isNonInteractiveElement = require('../util/isNonInteractiveElement');
  16. var _isNonInteractiveElement2 = _interopRequireDefault(_isNonInteractiveElement);
  17. var _isNonInteractiveRole = require('../util/isNonInteractiveRole');
  18. var _isNonInteractiveRole2 = _interopRequireDefault(_isNonInteractiveRole);
  19. var _isPresentationRole = require('../util/isPresentationRole');
  20. var _isPresentationRole2 = _interopRequireDefault(_isPresentationRole);
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /**
  23. * @fileoverview Enforce static elements have no interactive handlers.
  24. * @author Ethan Cohen
  25. *
  26. */
  27. // ----------------------------------------------------------------------------
  28. // Rule Definition
  29. // ----------------------------------------------------------------------------
  30. var errorMessage = 'Static HTML elements with event handlers require a role.';
  31. var domElements = [].concat(_toConsumableArray(_ariaQuery.dom.keys()));
  32. var defaultInteractiveProps = [].concat(_toConsumableArray(_jsxAstUtils.eventHandlersByType.focus), _toConsumableArray(_jsxAstUtils.eventHandlersByType.keyboard), _toConsumableArray(_jsxAstUtils.eventHandlersByType.mouse));
  33. var schema = (0, _schemas.generateObjSchema)({
  34. handlers: _schemas.arraySchema
  35. });
  36. module.exports = {
  37. meta: {
  38. docs: {
  39. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-static-element-interactions.md'
  40. },
  41. schema: [schema]
  42. },
  43. create: function create(context) {
  44. var options = context.options;
  45. return {
  46. JSXOpeningElement: function JSXOpeningElement(node) {
  47. var attributes = node.attributes;
  48. var type = (0, _jsxAstUtils.elementType)(node);
  49. var interactiveProps = options[0] ? options[0].handlers : defaultInteractiveProps;
  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, _arrayIncludes2.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, _isHiddenFromScreenReader2.default)(type, attributes) || (0, _isPresentationRole2.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, _isInteractiveElement2.default)(type, attributes) || (0, _isInteractiveRole2.default)(type, attributes) || (0, _isNonInteractiveElement2.default)(type, attributes) || (0, _isNonInteractiveRole2.default)(type, attributes) || (0, _isAbstractRole2.default)(type, attributes)) {
  65. // This rule has no opinion about abstract roles.
  66. return;
  67. }
  68. // Visible, non-interactive elements should not have an interactive handler.
  69. context.report({
  70. node: node,
  71. message: errorMessage
  72. });
  73. }
  74. };
  75. }
  76. };