interface-example

Try in Playground
typescript-templatesUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: interface


interface-example

How to write a rule
function visit(node, filename, code) {
  console.log(node.name);

  /*
   * Handle interface name
   */
  if (node.name && node.name.value === "Foo") {
    const edit = buildEditUpdate(node.name.start.line, node.name.start.col, node.name.end.line, node.name.end.col, "Bar");
    const fix = buildFix("use Bar", [edit]);

    const error = buildError(node.name.start.line, node.name.start.col, node.name.end.line, node.name.end.col, "do not use Foo", "CRITICAL", "SAFETY");
    addError(error.addFix(fix));
  }

  if (node.members) {
    node.members.forEach(m => {
      if (m && m.astType === "interfaceproperty") {
        /*
         * Handle the property name
         */
        if (m.name && m.name.value === "label") {
          const edit = buildEditUpdate(m.name.start.line, m.name.start.col, m.name.end.line, m.name.end.col, "otherLabel");
          const fix = buildFix("do not use label", [edit]);

          const error = buildError(m.name.start.line, m.name.start.col, m.name.end.line, m.name.end.col, "do not use label", "CRITICAL", "SAFETY");
          addError(error.addFix(fix));
        }

        /*
         * Handle the type of the property
         */
        if (m.value && m.value.value === "any") {
          const edit = buildEditUpdate(m.value.start.line, m.value.start.col, m.value.end.line, m.value.end.col, "unknown");
          const fix = buildFix("use unknown", [edit]);

          const error = buildError(m.value.start.line, m.value.start.col, m.value.end.line, m.value.end.col, "do not use any", "CRITICAL", "SAFETY");
          addError(error.addFix(fix));
        }
      }

    });
  }
}

interface.ts

Expected test result: has error

interface Foo {
  label: any;
}
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.