ESLint rules for var / let and/or uninitialised variables

31 Views Asked by At

See the following example where case B is undesirable and either we want case A or case C. I could envisage this being detected by (i) (ii) or (iii) below. (iii) is possible with rule "no-var" but I don't want to go through and change all of my code base [https://eslint.org/docs/latest/rules/no-var -- which it acknowledges: may not want to apply this rule if the cost of migrating from var to let is too costly.]

function throw1 (j) {
  if (j == 1) throw j;
  return j;
}

var i;
for (i = 0; i < 3; i ++) { let r;             try { r = throw1 (i); } catch {}; console.log (r); }   //  Case A: 0, undefined, 2
for (i = 0; i < 3; i ++) { var r;             try { r = throw1 (i); } catch {}; console.log (r); }   //  Case B: 0, 0, 2         
for (i = 0; i < 3; i ++) { var r = undefined; try { r = throw1 (i); } catch {}; console.log (r); }   //  CAse C: 0, undefined, 2

// i. catch uninitialised variables (case A or B)
// ii. catch use of var uninitialised (case B)
// iii. catch use of var rather than let (case B or C)

0

There are 0 best solutions below