pattern-rule

Try in Playground
javascript-templatesUnknownInformational

0

No tags

No CWE or CVE

Pattern Rule: console.log(${BAR})


pattern-rule

How to write a rule
function visit(pattern, filename, code) {
  const variable = pattern.variables.get("BAR");
  
  // If the name of the variable passed is foo, raise an error
  if (variable.value === "foo") {
    // build the error being reported
    const error = buildError(pattern.start.line, pattern.start.col, pattern.end.line, pattern.end.col, "Do not use foo as a variable", "INFO", "BEST_PRACTICES");
    
    // Make an edit to replace where the variable is by "bar"
    const edit = buildEdit(variable.start.line, variable.start.col, variable.end.line, variable.end.col, "update", "bar");
    
    // Make a fix with the edit we previously built
    const fix = buildFix("use bar instead", [edit]);
    
    // add the error to the list of reported errors
    addError(error.addFix(fix));
  }
}

program.js

Expected test result: no error

Detect when we pass foo to console.log

function foo(bar){
   console.log(foo);
}
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.