no-noninteractive-tabindex.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  4. var _ariaQuery = require("aria-query");
  5. var _jsxAstUtils = require("jsx-ast-utils");
  6. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  7. var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
  8. var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
  9. var _isNonLiteralProperty = _interopRequireDefault(require("../util/isNonLiteralProperty"));
  10. var _schemas = require("../util/schemas");
  11. var _getTabIndex = _interopRequireDefault(require("../util/getTabIndex"));
  12. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  13. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  14. var errorMessage = '`tabIndex` should only be declared on interactive elements.';
  15. var schema = (0, _schemas.generateObjSchema)({
  16. roles: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
  17. description: 'An array of ARIA roles'
  18. }),
  19. tags: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
  20. description: 'An array of HTML tag names'
  21. })
  22. });
  23. module.exports = {
  24. meta: {
  25. docs: {
  26. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-noninteractive-tabindex.md'
  27. },
  28. schema: [schema]
  29. },
  30. create: function create(context) {
  31. var options = context.options;
  32. return {
  33. JSXOpeningElement: function JSXOpeningElement(node) {
  34. var type = (0, _jsxAstUtils.elementType)(node);
  35. var attributes = node.attributes;
  36. var tabIndexProp = (0, _jsxAstUtils.getProp)(attributes, 'tabIndex');
  37. var tabIndex = (0, _getTabIndex["default"])(tabIndexProp); // Early return;
  38. if (typeof tabIndex === 'undefined') {
  39. return;
  40. }
  41. var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'role'));
  42. if (!_ariaQuery.dom.has(type)) {
  43. // Do not test higher level JSX components, as we do not know what
  44. // low-level DOM element this maps to.
  45. return;
  46. } // Allow for configuration overrides.
  47. var _ref = options[0] || {},
  48. tags = _ref.tags,
  49. roles = _ref.roles,
  50. allowExpressionValues = _ref.allowExpressionValues;
  51. if (tags && (0, _arrayIncludes["default"])(tags, type)) {
  52. return;
  53. }
  54. if (roles && (0, _arrayIncludes["default"])(roles, role)) {
  55. return;
  56. }
  57. if (allowExpressionValues === true && (0, _isNonLiteralProperty["default"])(attributes, 'role')) {
  58. return;
  59. }
  60. if ((0, _isInteractiveElement["default"])(type, attributes) || (0, _isInteractiveRole["default"])(type, attributes)) {
  61. return;
  62. }
  63. if (tabIndex >= 0) {
  64. context.report({
  65. node: tabIndexProp,
  66. message: errorMessage
  67. });
  68. }
  69. }
  70. };
  71. }
  72. };