variable-declaration

Try in Playground
typescript-templatesUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: variable declaration


variable-declaration

How to write a rule
function visit(node, filename, code) {
  // If we use var, suggest to use const
  if (node.modifier && node.modifier.value && node.modifier.value === "var") {
    const error = buildError(node.modifier.start.line, node.modifier.start.col,
      node.modifier.end.line, node.modifier.end.col,
      "do not use var", "CRITICAL", "SAFETY");
    const edit = buildEditUpdate(
      node.modifier.start.line, node.modifier.start.col,
      node.modifier.end.line, node.modifier.end.col,
      "const");
    const fix = buildFix("use const instead", [edit]);

    addError(error.addFix(fix));
  }

  // If the type of the variable is "any", show a warning
  if (node.type && node.type.astType === "string" && node.type.value === "any") {
    const error = buildError(node.type.start.line, node.type.start.col,
      node.type.end.line, node.type.end.col,
      "do not use any", "CRITICAL", "SAFETY");
    const edit = buildEditUpdate(
      node.type.start.line, node.type.start.col,
      node.type.end.line, node.type.end.col,
      "unknown");
    const fix = buildFix("use unknown instead", [edit]);

    addError(error.addFix(fix));
  }

}

var-example.ts

Expected test result: has error

var foo: any = 42;
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.