img-redundant-alt.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 = {
  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); // Only check 'label' elements and custom types.
  40. if (typesToValidate.indexOf(nodeType) === -1) {
  41. return;
  42. }
  43. var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt'); // Return if alt prop is not present.
  44. if (altProp === undefined) {
  45. return;
  46. }
  47. var value = (0, _jsxAstUtils.getLiteralPropValue)(altProp);
  48. var isVisible = (0, _isHiddenFromScreenReader["default"])(nodeType, node.attributes) === false;
  49. var _options$words = options.words,
  50. words = _options$words === void 0 ? [] : _options$words;
  51. var redundantWords = REDUNDANT_WORDS.concat(words);
  52. if (typeof value === 'string' && isVisible) {
  53. var hasRedundancy = new RegExp("(?!{)\\b(".concat(redundantWords.join('|'), ")\\b(?!})"), 'i').test(value);
  54. if (hasRedundancy === true) {
  55. context.report({
  56. node,
  57. message: errorMessage
  58. });
  59. }
  60. }
  61. }
  62. };
  63. }
  64. };
  65. exports["default"] = _default;
  66. module.exports = exports.default;