use-jsonify
Ast Rule: function definition
use-jsonify
function visit(node, filename, code) {
const checkElement = (element) => {
if (!element) {
return;
}
if (element.astType === "functioncall") {
if (element.functionName.value === "dumps" && element.moduleOrObject.value === "json") {
const error = buildError(element.start.line, element.start.col, element.end.line, element.end.col,
"do not use json.dumps, use flask.jsonify() instead", "CRITICAL", "SECURITY");
addError(error);
}
}
if (element.astType === "assignment") {
checkElement(element.right);
}
};
const isRoute = node.decorators && node.decorators.filter(d => d.name && d.name.value === "app.route").length > 0;
const useJson = node.context.imports.filter(i => i.astType === "importstatement" && i.packages.filter(p => p.name && p.name.value === "json").length > 0).length > 0;
if (!useJson) {
return;
}
if (!isRoute) {
return;
}
if (node.content) {
if (node.content.astType === "sequence") {
node.content.elements.forEach(e => checkElement(e));
}
}
}
error.py
Expected test result: no error