if-operator-equal

Try in Playground
javascript-templatesUnknownInformational

0

No tags

No CWE or CVE

In an if condition, make sure that = or == are replaced by ===.

Ast Rule: if condition


if-operator-equal

How to write a rule
function visit(node, filename, code) {
  if(node.condition.operator && (node.condition.operator.value === "=" | node.condition.operator.value === "==")){

    const editChangeFunctionName = buildEditUpdate(node.condition.operator.start.line, node.condition.operator.start.col, node.condition.operator.end.line, node.condition.operator.end.col, "===");
    const fix = buildFix("use ===", [editChangeFunctionName]);

    const error = buildError(node.condition.operator.start.line, node.condition.operator.start.col, node.condition.operator.end.line, node.condition.operator.end.col, "make sure you use ===", "CRITICAL", "SAFETY");
    addError(error.addFix(fix));
  }
}

condition.js

Expected test result: has error

if (value = 42) {
  console.log("is 42");
}

if (obj == "bla"){
  console.log("bla");
}
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.