You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

82 lines
3.5 KiB

  1. 'use strict';
  2. var _ariaQuery = require('aria-query');
  3. var _jsxAstUtils = require('jsx-ast-utils');
  4. var _schemas = require('../util/schemas');
  5. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /**
  6. * @fileoverview Enforce that elements with ARIA roles must
  7. * have all required attributes for that role.
  8. * @author Ethan Cohen
  9. */
  10. // ----------------------------------------------------------------------------
  11. // Rule Definition
  12. // ----------------------------------------------------------------------------
  13. var errorMessage = function errorMessage(role, requiredProps) {
  14. return 'Elements with the ARIA role "' + role + '" must have the following attributes defined: ' + String(requiredProps).toLowerCase();
  15. };
  16. var schema = (0, _schemas.generateObjSchema)();
  17. module.exports = {
  18. meta: {
  19. docs: {
  20. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/role-has-required-aria-props.md'
  21. },
  22. schema: [schema]
  23. },
  24. create: function create(context) {
  25. return {
  26. JSXAttribute: function JSXAttribute(attribute) {
  27. var name = (0, _jsxAstUtils.propName)(attribute).toLowerCase();
  28. if (name !== 'role') {
  29. return;
  30. }
  31. var type = (0, _jsxAstUtils.elementType)(attribute.parent);
  32. if (!_ariaQuery.dom.get(type)) {
  33. return;
  34. }
  35. var value = (0, _jsxAstUtils.getLiteralPropValue)(attribute);
  36. // If value is undefined, then the role attribute will be dropped in the DOM.
  37. // If value is null, then getLiteralAttributeValue is telling us
  38. // that the value isn't in the form of a literal.
  39. if (value === undefined || value === null) {
  40. return;
  41. }
  42. var normalizedValues = String(value).toLowerCase().split(' ');
  43. var validRoles = normalizedValues.filter(function (val) {
  44. return [].concat(_toConsumableArray(_ariaQuery.roles.keys())).indexOf(val) > -1;
  45. });
  46. validRoles.forEach(function (role) {
  47. var _roles$get = _ariaQuery.roles.get(role),
  48. requiredPropKeyValues = _roles$get.requiredProps;
  49. var requiredProps = Object.keys(requiredPropKeyValues);
  50. if (requiredProps.length > 0) {
  51. var hasRequiredProps = requiredProps.every(function (prop) {
  52. return (0, _jsxAstUtils.getProp)(attribute.parent.attributes, prop);
  53. });
  54. if (hasRequiredProps === false) {
  55. context.report({
  56. node: attribute,
  57. message: errorMessage(role.toLowerCase(), requiredProps)
  58. });
  59. }
  60. }
  61. });
  62. }
  63. };
  64. }
  65. };