aria-props.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 = {
  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); // `aria` needs to be prefix of property.
  41. if (name.indexOf('aria-') !== 0) {
  42. return;
  43. }
  44. var isValid = ariaAttributes.indexOf(name) > -1;
  45. if (isValid === false) {
  46. context.report({
  47. node: attribute,
  48. message: errorMessage(name)
  49. });
  50. }
  51. }
  52. };
  53. }
  54. };
  55. exports["default"] = _default;
  56. module.exports = exports.default;