| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports["default"] = void 0;
|
|---|
| 7 | var _ariaQuery = require("aria-query");
|
|---|
| 8 | var _jsxAstUtils = require("jsx-ast-utils");
|
|---|
| 9 | var _arrayIncludes = _interopRequireDefault(require("array-includes"));
|
|---|
| 10 | var _getElementType = _interopRequireDefault(require("../util/getElementType"));
|
|---|
| 11 | var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
|
|---|
| 12 | var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
|
|---|
| 13 | var _isNonLiteralProperty = _interopRequireDefault(require("../util/isNonLiteralProperty"));
|
|---|
| 14 | var _schemas = require("../util/schemas");
|
|---|
| 15 | var _getTabIndex = _interopRequireDefault(require("../util/getTabIndex"));
|
|---|
| 16 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|---|
| 17 | function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|---|
| 18 | function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|---|
| 19 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 20 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 21 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /**
|
|---|
| 22 | * @fileoverview Disallow tabindex on static and noninteractive elements
|
|---|
| 23 | * @author jessebeach
|
|---|
| 24 | *
|
|---|
| 25 | */ // ----------------------------------------------------------------------------
|
|---|
| 26 | // Rule Definition
|
|---|
| 27 | // ----------------------------------------------------------------------------
|
|---|
| 28 | var errorMessage = '`tabIndex` should only be declared on interactive elements.';
|
|---|
| 29 | var schema = (0, _schemas.generateObjSchema)({
|
|---|
| 30 | roles: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
|
|---|
| 31 | description: 'An array of ARIA roles'
|
|---|
| 32 | }),
|
|---|
| 33 | tags: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
|
|---|
| 34 | description: 'An array of HTML tag names'
|
|---|
| 35 | })
|
|---|
| 36 | });
|
|---|
| 37 | var _default = exports["default"] = {
|
|---|
| 38 | meta: {
|
|---|
| 39 | docs: {
|
|---|
| 40 | url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-noninteractive-tabindex.md',
|
|---|
| 41 | description: '`tabIndex` should only be declared on interactive elements.'
|
|---|
| 42 | },
|
|---|
| 43 | schema: [schema]
|
|---|
| 44 | },
|
|---|
| 45 | create: function create(context) {
|
|---|
| 46 | var options = context.options;
|
|---|
| 47 | var elementType = (0, _getElementType["default"])(context);
|
|---|
| 48 | return {
|
|---|
| 49 | JSXOpeningElement: function (_JSXOpeningElement) {
|
|---|
| 50 | function JSXOpeningElement(_x) {
|
|---|
| 51 | return _JSXOpeningElement.apply(this, arguments);
|
|---|
| 52 | }
|
|---|
| 53 | JSXOpeningElement.toString = function () {
|
|---|
| 54 | return _JSXOpeningElement.toString();
|
|---|
| 55 | };
|
|---|
| 56 | return JSXOpeningElement;
|
|---|
| 57 | }(function (node) {
|
|---|
| 58 | var type = elementType(node);
|
|---|
| 59 | var attributes = node.attributes;
|
|---|
| 60 | var tabIndexProp = (0, _jsxAstUtils.getProp)(attributes, 'tabIndex');
|
|---|
| 61 | var tabIndex = (0, _getTabIndex["default"])(tabIndexProp);
|
|---|
| 62 | // Early return;
|
|---|
| 63 | if (typeof tabIndex === 'undefined') {
|
|---|
| 64 | return;
|
|---|
| 65 | }
|
|---|
| 66 | var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'role'));
|
|---|
| 67 | if (!_ariaQuery.dom.has(type)) {
|
|---|
| 68 | // Do not test higher level JSX components, as we do not know what
|
|---|
| 69 | // low-level DOM element this maps to.
|
|---|
| 70 | return;
|
|---|
| 71 | }
|
|---|
| 72 | // Allow for configuration overrides.
|
|---|
| 73 | var _ref = options[0] || {},
|
|---|
| 74 | tags = _ref.tags,
|
|---|
| 75 | roles = _ref.roles,
|
|---|
| 76 | allowExpressionValues = _ref.allowExpressionValues;
|
|---|
| 77 | if (tags && (0, _arrayIncludes["default"])(tags, type)) {
|
|---|
| 78 | return;
|
|---|
| 79 | }
|
|---|
| 80 | if (roles && (0, _arrayIncludes["default"])(roles, role)) {
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 | if (allowExpressionValues === true && (0, _isNonLiteralProperty["default"])(attributes, 'role')) {
|
|---|
| 84 | // Special case if role is assigned using ternary with literals on both side
|
|---|
| 85 | var roleProp = (0, _jsxAstUtils.getProp)(attributes, 'role');
|
|---|
| 86 | if (roleProp && roleProp.type === 'JSXAttribute' && roleProp.value.type === 'JSXExpressionContainer') {
|
|---|
| 87 | if (roleProp.value.expression.type === 'ConditionalExpression') {
|
|---|
| 88 | if (roleProp.value.expression.consequent.type === 'Literal' && roleProp.value.expression.alternate.type === 'Literal') {
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | return;
|
|---|
| 94 | }
|
|---|
| 95 | if ((0, _isInteractiveElement["default"])(type, attributes) || (0, _isInteractiveRole["default"])(type, attributes)) {
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 | if (tabIndex >= 0) {
|
|---|
| 99 | context.report({
|
|---|
| 100 | node: tabIndexProp,
|
|---|
| 101 | message: errorMessage
|
|---|
| 102 | });
|
|---|
| 103 | }
|
|---|
| 104 | })
|
|---|
| 105 | };
|
|---|
| 106 | }
|
|---|
| 107 | };
|
|---|
| 108 | module.exports = exports.default; |
|---|