variable-name-check

Try in Playground
python-templatesUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: assignment


variable-name-check

How to write a rule
// This rule ban the name foo in variables


// Detect if a string is foo and suggests to replace it with bar
const banFooName = (element) => {
	console.log(element.value);
  if(element.value === "foo") {
    const error = buildError(element.start.line, element.start.col, 
                             element.end.line, element.end.col, 
                             "do not use foo", "MINOR", "BEST_PRACTICES");
    const edit = buildEditUpdate(element.start.line, element.start.col,
                                 element.end.line, element.end.col, "bar");
    const fix = buildFix("use bar instead", [edit]);
    addError(error.addFix(fix));
  }
};

// Core method to visit the AST for an assignment
function visit(node, filename, code) {
	if(!node.left) {
    return;
  }
  
  // this is the case of "var = ...."
  if(node.left.astType === "string"){
    banFooName(node.left);
  }
  
  // catch case for "var1, var2 = ...."
  if(node.left.astType === "list") {
    if(node.left.elements) {
      node.left.elements.forEach(element => {
        if(element.astType === "string"){
          banFooName(element);
        }
      });
    }
  }
}

test-list.py

Expected test result: has error

foo, bar = myfunction()

test-variable.py

Expected test result: has error

foo = myfunction()
Add comment

Log in to add a comment


    Be the first one to leave a comment!

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

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.