aria-props.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  8. var _ariaQuery = require("aria-query");
  9. var _jsxAstUtils = require("jsx-ast-utils");
  10. var _schemas = require("../util/schemas");
  11. var _getSuggestion = _interopRequireDefault(require("../util/getSuggestion"));
  12. /**
  13. * @fileoverview Enforce all aria-* properties are valid.
  14. * @author Ethan Cohen
  15. */
  16. // ----------------------------------------------------------------------------
  17. // Rule Definition
  18. // ----------------------------------------------------------------------------
  19. var ariaAttributes = (0, _toConsumableArray2["default"])(_ariaQuery.aria.keys());
  20. var errorMessage = function errorMessage(name) {
  21. var suggestions = (0, _getSuggestion["default"])(name, ariaAttributes);
  22. var message = "".concat(name, ": This attribute is an invalid ARIA attribute.");
  23. if (suggestions.length > 0) {
  24. return "".concat(message, " Did you mean to use ").concat(suggestions, "?");
  25. }
  26. return message;
  27. };
  28. var schema = (0, _schemas.generateObjSchema)();
  29. var _default = exports["default"] = {
  30. meta: {
  31. docs: {
  32. url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-props.md',
  33. description: 'Enforce all `aria-*` props are valid.'
  34. },
  35. schema: [schema]
  36. },
  37. create: function create(context) {
  38. return {
  39. JSXAttribute: function JSXAttribute(attribute) {
  40. var name = (0, _jsxAstUtils.propName)(attribute);
  41. // `aria` needs to be prefix of property.
  42. if (name.indexOf('aria-') !== 0) {
  43. return;
  44. }
  45. var isValid = _ariaQuery.aria.has(name);
  46. if (isValid === false) {
  47. context.report({
  48. node: attribute,
  49. message: errorMessage(name)
  50. });
  51. }
  52. }
  53. };
  54. }
  55. };
  56. module.exports = exports.default;