autocomplete-valid.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  4. var _axeCore = require("axe-core");
  5. var _jsxAstUtils = require("jsx-ast-utils");
  6. var _schemas = require("../util/schemas");
  7. /**
  8. * @fileoverview Ensure autocomplete attribute is correct.
  9. * @author Wilco Fiers
  10. */
  11. // ----------------------------------------------------------------------------
  12. // Rule Definition
  13. // ----------------------------------------------------------------------------
  14. var schema = (0, _schemas.generateObjSchema)({
  15. inputComponents: _schemas.arraySchema
  16. });
  17. module.exports = {
  18. meta: {
  19. docs: {
  20. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/autocomplete-valid.md'
  21. },
  22. schema: [schema]
  23. },
  24. create: function create(context) {
  25. return {
  26. JSXOpeningElement: function JSXOpeningElement(node) {
  27. var options = context.options[0] || {};
  28. var _options$inputCompone = options.inputComponents,
  29. inputComponents = _options$inputCompone === void 0 ? [] : _options$inputCompone;
  30. var inputTypes = ['input'].concat((0, _toConsumableArray2["default"])(inputComponents));
  31. var elType = (0, _jsxAstUtils.elementType)(node);
  32. var autocomplete = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'autocomplete'));
  33. if (typeof autocomplete !== 'string' || !inputTypes.includes(elType)) {
  34. return;
  35. }
  36. var type = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'type'));
  37. var _runVirtualRule = (0, _axeCore.runVirtualRule)('autocomplete-valid', {
  38. nodeName: 'input',
  39. attributes: {
  40. autocomplete,
  41. // Which autocomplete is valid depends on the input type
  42. type: type === null ? undefined : type
  43. }
  44. }),
  45. violations = _runVirtualRule.violations;
  46. if (violations.length === 0) {
  47. return;
  48. } // Since we only test one rule, with one node, return the message from first (and only) instance of each
  49. context.report({
  50. node,
  51. message: violations[0].nodes[0].all[0].message
  52. });
  53. }
  54. };
  55. }
  56. };