no-autofocus.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var _jsxAstUtils = require("jsx-ast-utils");
  3. var _ariaQuery = require("aria-query");
  4. var _schemas = require("../util/schemas");
  5. /**
  6. * @fileoverview Enforce autoFocus prop is not used.
  7. * @author Ethan Cohen <@evcohen>
  8. */
  9. // ----------------------------------------------------------------------------
  10. // Rule Definition
  11. // ----------------------------------------------------------------------------
  12. var errorMessage = 'The autoFocus prop should not be used, as it can reduce usability and accessibility for users.';
  13. var schema = (0, _schemas.generateObjSchema)({
  14. ignoreNonDOM: {
  15. type: 'boolean',
  16. "default": false
  17. }
  18. });
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-autofocus.md'
  23. },
  24. schema: [schema]
  25. },
  26. create: function create(context) {
  27. return {
  28. JSXAttribute: function JSXAttribute(attribute) {
  29. // Determine if ignoreNonDOM is set to true
  30. // If true, then do not run rule.
  31. var options = context.options[0] || {};
  32. var ignoreNonDOM = !!options.ignoreNonDOM;
  33. if (ignoreNonDOM) {
  34. var type = (0, _jsxAstUtils.elementType)(attribute.parent);
  35. if (!_ariaQuery.dom.get(type)) {
  36. return;
  37. }
  38. } // Don't normalize, since React only recognizes autoFocus on low-level DOM elements.
  39. if ((0, _jsxAstUtils.propName)(attribute) === 'autoFocus') {
  40. context.report({
  41. node: attribute,
  42. message: errorMessage
  43. });
  44. }
  45. }
  46. };
  47. }
  48. };