tabindex-no-positive.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. var _jsxAstUtils = require("jsx-ast-utils");
  3. var _schemas = require("../util/schemas");
  4. /**
  5. * @fileoverview Enforce tabIndex value is not greater than zero.
  6. * @author Ethan Cohen
  7. */
  8. // ----------------------------------------------------------------------------
  9. // Rule Definition
  10. // ----------------------------------------------------------------------------
  11. var errorMessage = 'Avoid positive integer values for tabIndex.';
  12. var schema = (0, _schemas.generateObjSchema)();
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/tabindex-no-positive.md'
  17. },
  18. schema: [schema]
  19. },
  20. create: function create(context) {
  21. return {
  22. JSXAttribute: function JSXAttribute(attribute) {
  23. var name = (0, _jsxAstUtils.propName)(attribute).toUpperCase(); // Check if tabIndex is the attribute
  24. if (name !== 'TABINDEX') {
  25. return;
  26. } // Only check literals because we can't infer values from certain expressions.
  27. var value = Number((0, _jsxAstUtils.getLiteralPropValue)(attribute)); // eslint-disable-next-line no-restricted-globals
  28. if (isNaN(value) || value <= 0) {
  29. return;
  30. }
  31. context.report({
  32. node: attribute,
  33. message: errorMessage
  34. });
  35. }
  36. };
  37. }
  38. };