no-onchange.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. var _jsxAstUtils = require("jsx-ast-utils");
  3. var _schemas = require("../util/schemas");
  4. /**
  5. * @fileoverview Enforce usage of onBlur over onChange for accessibility.
  6. * @author Ethan Cohen
  7. */
  8. // ----------------------------------------------------------------------------
  9. // Rule Definition
  10. // ----------------------------------------------------------------------------
  11. var errorMessage = 'onBlur must be used instead of onchange, unless absolutely necessary and it causes no negative consequences for keyboard only or screen reader users.';
  12. var applicableTypes = ['select', 'option'];
  13. var schema = (0, _schemas.generateObjSchema)();
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-onchange.md'
  18. },
  19. schema: [schema]
  20. },
  21. create: function create(context) {
  22. return {
  23. JSXOpeningElement: function JSXOpeningElement(node) {
  24. var nodeType = (0, _jsxAstUtils.elementType)(node);
  25. if (applicableTypes.indexOf(nodeType) === -1) {
  26. return;
  27. }
  28. var onChange = (0, _jsxAstUtils.getProp)(node.attributes, 'onChange');
  29. var hasOnBlur = (0, _jsxAstUtils.getProp)(node.attributes, 'onBlur') !== undefined;
  30. if (onChange && !hasOnBlur) {
  31. context.report({
  32. node,
  33. message: errorMessage
  34. });
  35. }
  36. }
  37. };
  38. }
  39. };