No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

85 líneas
3.1 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 aria role attribute is valid.
  7. * @author Ethan Cohen
  8. */
  9. // ----------------------------------------------------------------------------
  10. // Rule Definition
  11. // ----------------------------------------------------------------------------
  12. var errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';
  13. var schema = (0, _schemas.generateObjSchema)({
  14. ignoreNonDOM: {
  15. type: 'boolean',
  16. default: false
  17. }
  18. });
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/aria-role.md'
  23. },
  24. schema: [schema]
  25. },
  26. create: function create(context) {
  27. return {
  28. JSXAttribute: function JSXAttribute(attribute) {
  29. // Determine if ignoreNonDOM is set to true
  30. // If true, then do not run rule.
  31. var options = context.options[0] || {};
  32. var ignoreNonDOM = !!options.ignoreNonDOM;
  33. if (ignoreNonDOM) {
  34. var type = (0, _jsxAstUtils.elementType)(attribute.parent);
  35. if (!_ariaQuery.dom.get(type)) {
  36. return;
  37. }
  38. }
  39. // Get prop name
  40. var name = (0, _jsxAstUtils.propName)(attribute).toUpperCase();
  41. if (name !== 'ROLE') {
  42. return;
  43. }
  44. var value = (0, _jsxAstUtils.getLiteralPropValue)(attribute);
  45. // If value is undefined, then the role attribute will be dropped in the DOM.
  46. // If value is null, then getLiteralAttributeValue is telling us that the
  47. // value isn't in the form of a literal.
  48. if (value === undefined || value === null) {
  49. return;
  50. }
  51. var normalizedValues = String(value).toLowerCase().split(' ');
  52. var validRoles = [].concat(_toConsumableArray(_ariaQuery.roles.keys())).filter(function (role) {
  53. return _ariaQuery.roles.get(role).abstract === false;
  54. });
  55. var isValid = normalizedValues.every(function (val) {
  56. return validRoles.indexOf(val) > -1;
  57. });
  58. if (isValid === true) {
  59. return;
  60. }
  61. context.report({
  62. node: attribute,
  63. message: errorMessage
  64. });
  65. }
  66. };
  67. }
  68. };