function-call-example

Try in Playground
typescript-templatesUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: function call


function-call-example

How to write a rule
function visit(node, filename, code) {
  const arguments = node.arguments.values;

  /*
   * Check each argument. If one argument has the variable "foo" used,
   * we report it.
   */
  arguments.forEach((a) => {
    if (a.value.astType === "string") {
      console.log(a.value.value);
      if (a.value.value === "foo") {
        const error = buildError(a.value.start.line, a.value.start.col,
          a.value.end.line, a.value.end.col,
          "Do not use foo as value", "WARNING", "BEST_PRACTICE");

        addError(error);
      }
    }
  });

  /*
   * Check the function name. We cannot use my_function, we recommend to use myFunction
   */
  if (node.functionName && node.functionName.astType === "string") {
    if (node.functionName.value === "my_function") {
      const error = buildError(node.functionName.start.line, node.functionName.start.col,
        node.functionName.end.line, node.functionName.end.col,
        "Do not use function_name", "WARNING", "BEST_PRACTICE");
      const edit = buildEditUpdate(node.functionName.start.line, node.functionName.start.col,
        node.functionName.end.line, node.functionName.end.col,
        "myFunction");
      const fix = buildFix("use myFunction", [edit]);
      addError(error.addFix(fix));
    }
  }
}

function-call.ts

Expected test result: has error

foo = my_function(foo, bar);
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.