aria-props.js 1.8 KB

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