print-foo

Try in Playground
julien-pythonUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: function call


print-foo

How to write a rule
function visit(node, filename, code) {
  if (filename.includes("_test.py") || filename.startsWith("test_")) {
  	return;
	}
  if(!node.functionName || node.functionName.value !== "print") {
    return;
  }
   console.log("bla");

  if(!node.arguments || !node.arguments.values || node.arguments.values.length !== 1 || node.arguments.values[0].value.value !== '\"foo\"') {
  	return;
  }
  const firstArgument = node.arguments.values[0].value;

  const error = buildError(firstArgument.start.line, 
                           firstArgument.start.col, 
                           firstArgument.end.line, 
                           firstArgument.end.col, 
                           "do not print \"foo\"", 
                           "MINOR", "BEST_PRACTICE");
  const edit = buildEditUpdate(firstArgument.start.line,
                               firstArgument.start.col,
                               firstArgument.end.line, 
                               firstArgument.end.col,
                               "\"bar\"");
  const fix = buildFix("print bar", [edit]);
	addError(error.addFix(fix));
    
}

bla.py

Expected test result: no error

wefwef

test_foo.py

Expected test result: no error

print("foo");

print-foo.py

Expected test result: has error

print("foo");
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.