form-validation
Ast Rule: function call
form-validation
function visit(node, filename, code) {
const FUNCTION_NAMES = ["StringField", "PasswordField", "BooleanField"];
// If filename starts or ends with test_ or _test, do not do anything
if(filename.includes("_test.py") || filename.startsWith("test_")) {
return;
}
if(!node.functionName){
console.log("bplop");
return;
}
if(!FUNCTION_NAMES.includes(node.functionName.value)){
return;
}
// Get the list of argument
const arguments = (node.arguments && node.arguments.values) || [];
// Get all the package list that are imported with "import <name>"
const useWtfForms = node.context.imports.filter(r => r.pkg && r.pkg.value === "wtforms").length > 0;
const hasValidatorsArgument = arguments.filter(a => a.name && a.name.value === "validators").length > 0;
console.log(hasValidatorsArgument);
if(!hasValidatorsArgument){
// build the error
const error = buildError(node.start.line, node.start.col,
node.end.line, node.end.col,
`${node.functionName.value} does not use validators`, "WARNING", "CRITICAL");
// build the fix (replace foo by bar)
const edit = buildEditAdd(node.end.line, node.end.col - 1,
arguments.length > 0 ? ", validators=[]" : "validators=[]")
// build a fix with one edit
const fix = buildFix("add validators", [edit]);
// report an error with a fix
addError(error.addFix(fix));
}
}
has-errors-with-arguments.py
Expected test result: has error
no-wtform-package.used
Expected test result: has error
no-error.py
Expected test result: no error
has-errors-without-arguments.py
Expected test result: has error