html-element-attribute-missing

Try in Playground
javascript-templatesBest PracticeInformational

0

No tags

No CWE or CVE

Simple template to check HTML elements.

More information:

  • HTML Element
  • HTML Attribute

Ast Rule: html element


html-element-attribute-missing

How to write a rule
// See https://doc.codiga.io/docs/rosie/ast/javascript/rosie-ast-javascript-htmlelement/
function visit(node, filename, code) {
  // Check only <ul> elements
  if(node.tag && node.tag.value === "ul") {
    // check that we have attributes
    if(node.attributes && node.attributes.length > 0){
      if(node.attributes && node.attributes.filter(a => a.name && a.name.value && a.name.value === "aria-label").length === 0) {
        const error = buildError(node.tag.start.line, node.tag.start.col, node.tag.end.line, node.tag.end.col, "should have aria-label", "CRITICAL", "SAFETY");
        addError(error);
      }

    }
  }
}

mycomponent.jsx

Expected test result: has error

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <ul 
        id="my-awsome-stuff"
        className="myclass"
        data-object-type="object-type"
        data-object-id="object-id"
        role="group">
      </ul>
    );
  }
}
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.