autocomplete-valid.js 2.5 KB

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