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

98 строки
4.1 KiB

  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /**
  3. * @fileoverview Disallow tabindex on static and noninteractive elements
  4. * @author jessebeach
  5. *
  6. */
  7. // ----------------------------------------------------------------------------
  8. // Rule Definition
  9. // ----------------------------------------------------------------------------
  10. var _ariaQuery = require('aria-query');
  11. var _jsxAstUtils = require('jsx-ast-utils');
  12. var _arrayIncludes = require('array-includes');
  13. var _arrayIncludes2 = _interopRequireDefault(_arrayIncludes);
  14. var _isInteractiveElement = require('../util/isInteractiveElement');
  15. var _isInteractiveElement2 = _interopRequireDefault(_isInteractiveElement);
  16. var _isInteractiveRole = require('../util/isInteractiveRole');
  17. var _isInteractiveRole2 = _interopRequireDefault(_isInteractiveRole);
  18. var _schemas = require('../util/schemas');
  19. var _getTabIndex = require('../util/getTabIndex');
  20. var _getTabIndex2 = _interopRequireDefault(_getTabIndex);
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. var errorMessage = '`tabIndex` should only be declared on interactive elements.';
  23. var schema = (0, _schemas.generateObjSchema)({
  24. roles: _extends({}, _schemas.arraySchema, {
  25. description: 'An array of ARIA roles'
  26. }),
  27. tags: _extends({}, _schemas.arraySchema, {
  28. description: 'An array of HTML tag names'
  29. })
  30. });
  31. module.exports = {
  32. meta: {
  33. docs: {
  34. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-noninteractive-tabindex.md'
  35. },
  36. schema: [schema]
  37. },
  38. create: function create(context) {
  39. var options = context.options;
  40. return {
  41. JSXOpeningElement: function JSXOpeningElement(node) {
  42. var type = (0, _jsxAstUtils.elementType)(node);
  43. var attributes = node.attributes;
  44. var tabIndexProp = (0, _jsxAstUtils.getProp)(attributes, 'tabIndex');
  45. var tabIndex = (0, _getTabIndex2.default)(tabIndexProp);
  46. // Early return;
  47. if (typeof tabIndex === 'undefined') {
  48. return;
  49. }
  50. var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'role'));
  51. if (!_ariaQuery.dom.has(type)) {
  52. // Do not test higher level JSX components, as we do not know what
  53. // low-level DOM element this maps to.
  54. return;
  55. }
  56. // Allow for configuration overrides.
  57. var _ref = options[0] || {},
  58. tags = _ref.tags,
  59. roles = _ref.roles;
  60. if (tags && (0, _arrayIncludes2.default)(tags, type) || roles && (0, _arrayIncludes2.default)(roles, role)) {
  61. return;
  62. }
  63. if ((0, _isInteractiveElement2.default)(type, attributes) || (0, _isInteractiveRole2.default)(type, attributes)) {
  64. return;
  65. }
  66. if (tabIndex >= 0) {
  67. context.report({
  68. node: tabIndexProp,
  69. message: errorMessage
  70. });
  71. }
  72. }
  73. };
  74. }
  75. };