variation

Try in Playground
python-launchdarklyBest PracticeInformational

0

No tags

No CWE or CVE

Check the good usage of the variation() function call.

See LaunchDarkly documentation

Ast Rule: function call


variation

How to write a rule
function visit(node, filename, code) {

  const FUNCTION_NAMES = ["variation_detail", "variation"];
	if(!FUNCTION_NAMES.includes(node.functionName.value)) {
    return;
  }
  const functionNameNode = node.functionName;

  const hasThreeArguments = node.arguments && node.arguments.values && node.arguments.values.length === 3;
  
  if(!hasThreeArguments){
    var error = buildError(functionNameNode.start.line, functionNameNode.start.col, 
                             functionNameNode.end.line, functionNameNode.end.col, 
                             "function variation() takes three arguments: a key, a user and a default value", "MINOR", "BEST_PRACTICES");
    
    if ((!node.arguments) || (node.arguments && node.arguments.values.length === 0)) {
    	const edit = buildEditAdd(node.end.line, node.end.col - 1, "\"feature.key\", user, False")
    	const fix = buildFix("add key, user and default value", [edit]);
      error = error.addFix(fix);
    }
    
    if (node.arguments && node.arguments.values.length === 1) {
      const lastArgument = node.arguments.values[node.arguments.values.length - 1];
    	const edit = buildEditAdd(lastArgument.end.line, lastArgument.end.col, ", user, False")
    	const fix = buildFix("add user and default values", [edit]);
      error = error.addFix(fix);
    }
    
    if (node.arguments && node.arguments.values.length === 2) {
    	const edit = buildEditAdd(node.end.line, node.end.col, ", False")
    	const fix = buildFix("add default value", [edit]);
      error = error.addFix(fix);
    }
          
    addError(error);
  }
}

one-argument.py

Expected test result: has error

show_feature = ldclient.get().variation(plop)

one-argument.py

Expected test result: has error

show_feature = ldclient.get().variation(plop)

no-arguments.py

Expected test result: has error

show_feature = ldclient.get().variation()
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.