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.
 
 
 
 

56 lines
2.4 KiB

  1. 'use strict';
  2. var _emojiRegex = require('emoji-regex');
  3. var _emojiRegex2 = _interopRequireDefault(_emojiRegex);
  4. var _jsxAstUtils = require('jsx-ast-utils');
  5. var _schemas = require('../util/schemas');
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7. var errorMessage = 'Emojis should be wrapped in <span>, have role="img", and have an accessible description with aria-label or aria-labelledby.'; /**
  8. * @fileoverview Enforce emojis are wrapped in <span> and provide screenreader access.
  9. * @author Ethan Cohen
  10. */
  11. // ----------------------------------------------------------------------------
  12. // Rule Definition
  13. // ----------------------------------------------------------------------------
  14. var schema = (0, _schemas.generateObjSchema)();
  15. module.exports = {
  16. meta: {
  17. docs: {
  18. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/accessible-emoji.md'
  19. },
  20. schema: [schema]
  21. },
  22. create: function create(context) {
  23. return {
  24. JSXOpeningElement: function JSXOpeningElement(node) {
  25. var literalChildValue = node.parent.children.find(function (child) {
  26. return child.type === 'Literal' || child.type === 'JSXText';
  27. });
  28. if (literalChildValue && (0, _emojiRegex2.default)().test(literalChildValue.value)) {
  29. var rolePropValue = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.attributes, 'role'));
  30. var ariaLabelProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-label');
  31. var arialLabelledByProp = (0, _jsxAstUtils.getProp)(node.attributes, 'aria-labelledby');
  32. var hasLabel = ariaLabelProp !== undefined || arialLabelledByProp !== undefined;
  33. var isSpan = (0, _jsxAstUtils.elementType)(node) === 'span';
  34. if (hasLabel === false || rolePropValue !== 'img' || isSpan === false) {
  35. context.report({
  36. node: node,
  37. message: errorMessage
  38. });
  39. }
  40. }
  41. }
  42. };
  43. }
  44. };