aria-role.js 3.1 KB

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