img-redundant-alt

Try in Playground
jsx-a11y

oscar143

Best PracticeInformational

0

No tags

No CWE or CVE

Ast Rule: html element


img-redundant-alt

How to write a rule
const REDUNDANT_WORDS = [
  'image',
  'photo',
  'picture',
];

const 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.';

const TYPES = {
  string: getPropStringValue,
  sequence: getPropSequenceValue,
  array: getPropArrayValue,
  object: getPropObjectValue,
  object_element: getPropObjectValue,
  functiondefinition: () => {},
};

function getTag(node) {
  if (node && node.tag) {
    return node.tag.value;
  }
}

function getProp(attributes = [], prop = "") {
  if (!prop) return;

  return attributes.find((attribute) => {
    if (attribute && attribute.name && attribute.name.value) {
      return attribute.name.value === prop;
    }
  });
}

function getPropStringValue(value) {
  if (value.value === "true") {
    return true;
  }

  if (value.value === "false") {
    return false;
  }

  return value.value.replace(/^\"/g, "").replace(/\"$/g, "");
}

function getPropArrayValue(value) {
  const array = [];
  const arrayElements = value.elements;

  if (arrayElements.length) {
    for (const element of arrayElements) {
      array.push(TYPES[element.astType](element));
    }
  }

  return array;
}

function getPropObjectValue(value) {
  const object = {};
  const objectElements = value.elements;

  if (objectElements.length) {
    for (const element of objectElements) {
      object[element.name.value] = TYPES[element.value.astType](element.value);
    }
  }

  return object;
}

function getPropSequenceValue(value) {
  if (value.elements.length) {
    const element = value.elements[0];

    if (element) {
      return TYPES[element.astType](element);
    }
  }
}

function extractValue(attribute, extractor) {
  if (attribute) {
    // Null valued attributes imply truthiness.
    // For example: <div aria-hidden />
    if (attribute.value === null) return true;

    return extractor(attribute.value);
  }
}

function getValue(value) {
  return TYPES[value.astType](value);
}

function getPropValue(attribute) {
  return extractValue(attribute, getValue);
}

function visit(node, filename, code) {
  const tag = getTag(node);

  if (tag !== "img") return;

  const altProp = getProp(node.attributes, 'alt');

  // Return if alt prop is not present.
  if (altProp === undefined) return;

  const value = getPropValue(altProp);

  const ariaHidden = getPropValue(getProp(node.attributes, 'aria-hidden'));
  const isVisible = !ariaHidden;

  if (typeof value === 'string' && isVisible) {
    const hasRedundancy = new RegExp(`(?!{)\\b(${REDUNDANT_WORDS.join('|')})\\b(?!})`, 'i').test(value);

    if (hasRedundancy === true) {
      const error = buildError(
        altProp.start.line,
        altProp.start.col,
        altProp.end.line,
        altProp.end.col,
        errorMessage,
        "INFO",
        "BEST_PRACTICES"
      );

      addError(error);
    }
  }
}

bad.jsx

Expected test result: no error

<img src="#" alt="an image" />
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.