img-redundant-alt.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 _jsxAstUtils = require("jsx-ast-utils");
  8. var _schemas = require("../util/schemas");
  9. var _getElementType = _interopRequireDefault(require("../util/getElementType"));
  10. var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
  11. /**
  12. * @fileoverview Enforce img alt attribute does not have the word image, picture, or photo.
  13. * @author Ethan Cohen
  14. */
  15. // ----------------------------------------------------------------------------
  16. // Rule Definition
  17. // ----------------------------------------------------------------------------
  18. var REDUNDANT_WORDS = ['image', 'photo', 'picture'];
  19. var errorMessage = 'Redundant alt attribute. Screen-readers already announce `img` tags as an image. You don’t need to use the words `image`, `photo,` or `picture` (or any specified custom words) in the alt prop.';
  20. var schema = (0, _schemas.generateObjSchema)({
  21. components: _schemas.arraySchema,
  22. words: _schemas.arraySchema
  23. });
  24. var _default = exports["default"] = {
  25. meta: {
  26. docs: {
  27. url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/img-redundant-alt.md',
  28. description: 'Enforce `<img>` alt prop does not contain the word "image", "picture", or "photo".'
  29. },
  30. schema: [schema]
  31. },
  32. create: function create(context) {
  33. var elementType = (0, _getElementType["default"])(context);
  34. return {
  35. JSXOpeningElement: function JSXOpeningElement(node) {
  36. var options = context.options[0] || {};
  37. var componentOptions = options.components || [];
  38. var typesToValidate = ['img'].concat(componentOptions);
  39. var nodeType = elementType(node);
  40. // Only check 'label' elements and custom types.
  41. if (typesToValidate.indexOf(nodeType) === -1) {
  42. return;
  43. }
  44. var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt');
  45. // Return if alt prop is not present.
  46. if (altProp === undefined) {
  47. return;
  48. }
  49. var value = (0, _jsxAstUtils.getLiteralPropValue)(altProp);
  50. var isVisible = (0, _isHiddenFromScreenReader["default"])(nodeType, node.attributes) === false;
  51. var _options$words = options.words,
  52. words = _options$words === void 0 ? [] : _options$words;
  53. var redundantWords = REDUNDANT_WORDS.concat(words);
  54. if (typeof value === 'string' && isVisible) {
  55. var hasRedundancy = new RegExp("(?!{)\\b(".concat(redundantWords.join('|'), ")\\b(?!})"), 'i').test(value);
  56. if (hasRedundancy === true) {
  57. context.report({
  58. node,
  59. message: errorMessage
  60. });
  61. }
  62. }
  63. }
  64. };
  65. }
  66. };
  67. module.exports = exports.default;