sindresorhus/eslint-plugin-unicorn
Rule proposal: `prefer-smaller-scope` (like `prefer-const`, but across scopes)
Closed
#1,524 opened on Sep 13, 2021
help wantednew rule
Repository metrics
- Stars
- (5,022 stars)
- PR merge metrics
- (Avg merge 1d 16h) (399 merged PRs in 30d)
Description
Context:
This is basically what prefer-const does:
function foo () {
let a; // ERROR: Use `const` instead (prefer-const)
a = 1;
return a;
}
Except that is should work across scopes
Fail
function foo () {
let a;
while (Math.random() > 0.5) {
a = get(); // ERROR: Use `const` instead (prefer-smaller-scope)
console.log(a);
}
}
Pass
function foo () {
while (Math.random() > 0.5) {
const a = get(); // ✅
console.log(a);
}
}
function foo () {
let a;
while (Math.random() > 0.5) {
a = get(a); // ✅ `a` is used before assignment
console.log(a);
}
}
function foo () {
let a;
while (Math.random() > 0.5) {
a = get();
}
console.log(a); // ✅ `a` is actually used in the outer scope
}