Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

79 linhas
2.8 KiB

  1. 'use strict';
  2. var _jsxAstUtils = require('jsx-ast-utils');
  3. var _schemas = require('../util/schemas');
  4. var _isHiddenFromScreenReader = require('../util/isHiddenFromScreenReader');
  5. var _isHiddenFromScreenReader2 = _interopRequireDefault(_isHiddenFromScreenReader);
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7. var REDUNDANT_WORDS = ['image', 'photo', 'picture']; /**
  8. * @fileoverview Enforce img alt attribute does not have the word image, picture, or photo.
  9. * @author Ethan Cohen
  10. */
  11. // ----------------------------------------------------------------------------
  12. // Rule Definition
  13. // ----------------------------------------------------------------------------
  14. 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.';
  15. var schema = (0, _schemas.generateObjSchema)({
  16. components: _schemas.arraySchema,
  17. words: _schemas.arraySchema
  18. });
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/img-redundant-alt.md'
  23. },
  24. schema: [schema]
  25. },
  26. create: function create(context) {
  27. return {
  28. JSXOpeningElement: function JSXOpeningElement(node) {
  29. var options = context.options[0] || {};
  30. var componentOptions = options.components || [];
  31. var typesToValidate = ['img'].concat(componentOptions);
  32. var nodeType = (0, _jsxAstUtils.elementType)(node);
  33. // Only check 'label' elements and custom types.
  34. if (typesToValidate.indexOf(nodeType) === -1) {
  35. return;
  36. }
  37. var altProp = (0, _jsxAstUtils.getProp)(node.attributes, 'alt');
  38. // Return if alt prop is not present.
  39. if (altProp === undefined) {
  40. return;
  41. }
  42. var value = (0, _jsxAstUtils.getLiteralPropValue)(altProp);
  43. var isVisible = (0, _isHiddenFromScreenReader2.default)(nodeType, node.attributes) === false;
  44. var _options$words = options.words,
  45. words = _options$words === undefined ? [] : _options$words;
  46. var redundantWords = REDUNDANT_WORDS.concat(words);
  47. if (typeof value === 'string' && isVisible) {
  48. var hasRedundancy = redundantWords.some(function (word) {
  49. return Boolean(value.match(new RegExp('(?!{)\\b' + word + '\\b(?!})', 'i')));
  50. });
  51. if (hasRedundancy === true) {
  52. context.report({
  53. node: node,
  54. message: errorMessage
  55. });
  56. }
  57. }
  58. }
  59. };
  60. }
  61. };