aria-role.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 _getElementType = _interopRequireDefault(require("../util/getElementType"));
  11. var _schemas = require("../util/schemas");
  12. /**
  13. * @fileoverview Enforce aria role attribute is valid.
  14. * @author Ethan Cohen
  15. */
  16. // ----------------------------------------------------------------------------
  17. // Rule Definition
  18. // ----------------------------------------------------------------------------
  19. var errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';
  20. var schema = (0, _schemas.generateObjSchema)({
  21. allowedInvalidRoles: {
  22. items: {
  23. type: 'string'
  24. },
  25. type: 'array',
  26. uniqueItems: true
  27. },
  28. ignoreNonDOM: {
  29. type: 'boolean',
  30. "default": false
  31. }
  32. });
  33. var _default = {
  34. meta: {
  35. docs: {
  36. url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/aria-role.md',
  37. description: 'Enforce that elements with ARIA roles must use a valid, non-abstract ARIA role.'
  38. },
  39. schema: [schema]
  40. },
  41. create: function create(context) {
  42. var options = context.options[0] || {};
  43. var ignoreNonDOM = !!options.ignoreNonDOM;
  44. var allowedInvalidRoles = new Set(options.allowedInvalidRoles || []);
  45. var validRoles = new Set((0, _toConsumableArray2["default"])(_ariaQuery.roles.keys()).filter(function (role) {
  46. return _ariaQuery.roles.get(role)["abstract"] === false;
  47. }));
  48. var elementType = (0, _getElementType["default"])(context);
  49. return {
  50. JSXAttribute: function JSXAttribute(attribute) {
  51. // If ignoreNonDOM and the parent isn't DOM, don't run rule.
  52. if (ignoreNonDOM) {
  53. var type = elementType(attribute.parent);
  54. if (!_ariaQuery.dom.get(type)) {
  55. return;
  56. }
  57. } // Get prop name
  58. var name = (0, _jsxAstUtils.propName)(attribute).toUpperCase();
  59. if (name !== 'ROLE') {
  60. return;
  61. }
  62. var value = (0, _jsxAstUtils.getLiteralPropValue)(attribute); // If value is undefined, then the role attribute will be dropped in the DOM.
  63. // If value is null, then getLiteralAttributeValue is telling us that the
  64. // value isn't in the form of a literal.
  65. if (value === undefined || value === null) {
  66. return;
  67. }
  68. var values = String(value).split(' ');
  69. var isValid = values.every(function (val) {
  70. return allowedInvalidRoles.has(val) || validRoles.has(val);
  71. });
  72. if (isValid === true) {
  73. return;
  74. }
  75. context.report({
  76. node: attribute,
  77. message: errorMessage
  78. });
  79. }
  80. };
  81. }
  82. };
  83. exports["default"] = _default;
  84. module.exports = exports.default;