var-declare

Try in Playground
js-rulesBest PracticeInformational

0

No tags

No CWE or CVE

This rule prompts the user to enter their name and then checks if the name is empty. If it is empty, it prints out the message "Please enter a name." otherwise it prints out "Welcome, [name]!" where [name] is the name the user entered.

Ast Rule: try block


var-declare

How to write a rule
function visit(node){
 var isBad = true;

 if (isBad) {
   throw new Error('All variables must be declared with the let keyword, not the var keyword.');
 }
}

test-1.js

Expected test result: no error

The function has a descriptive name.

let testIsBad = true;

function testVarKeyword() {
  if (testIsBad) {
    try {
      throw new Error('All variables must be declared with the let keyword, not the var keyword.');
      return true;
    } catch(err) {
      return false;
    }
  }
}

let result = testVarKeyword();

if (result) {
  console.log("Test Passed");
} else {
  console.log("Test Failed");
}
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.