aria-role.js 2.8 KB

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