assignment-names

Try in Playground
python-code-styleUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: assignment


assignment-names

How to write a rule
const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
const useCamelCase = (string) => /[A-Z]/.test(string);
const twoConsecutiveCapitalLetters = (string) => /[A-Z]{2,}/.test(string);

const checkString = (element) => {

  if(useCamelCase(element.value) && !twoConsecutiveCapitalLetters(element.value)) {
    const snakeCaseValue = camelToSnakeCase(element.value);
    const error = buildError(element.start.line, element.start.col, 
                             element.end.line, element.end.col, 
                             "use snake_case and not camelCase", "MINOR", "BEST_PRACTICES");
    const edit = buildEditUpdate(element.start.line, element.start.col,
                                 element.end.line, element.end.col, snakeCaseValue);
    const fix = buildFix(`${snakeCaseValue} instead of ${element.value}`, [edit]);
    addError(error.addFix(fix));
  }
};

function visit(node, filename, code) {
	if(!node.left) {
    return;
  }
  if(node.left.astType === "string"){
    checkString(node.left);
  }
  if(node.left.astType === "list") {
    if(node.left.elements) {
      node.left.elements.forEach(element => {
        if(element.astType === "string"){
          checkString(element);
        }
      });
    }
  }
}

two-consecutive-capitals.py

Expected test result: no error

fooBAr = foobar()

simple_variable.py

Expected test result: has error

fooBar = foobar()

variable-list.py

Expected test result: has error

firstVariable, secondVariable = functioncall()
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.