`prefer-const` does not detect variables declared outside loop
See original GitHub issueWhat rule do you want to change?
prefer-const
Does this change cause the rule to produce more or fewer warnings?
More warnings
How will the change be implemented? (New option, new default behavior, etc.)?
New default
Please provide some example code that this change will affect:
function foo () {
let a;
while (Math.random() > 0.5) {
a = get();
console.log(a);
}
}
What does the rule currently do for this code?
Not detected
What will the rule do after it’s changed?
a
is only assigned and used in the loop, it should not be declared in the outer scope. There are only two situations where this usage is fine:
-
a
is used before assignmentfunction foo () { let a; while (Math.random() > 0.5) { a = get(a); console.log(a); } }
-
a
is actually used in the outer scopefunction foo () { let a; while (Math.random() > 0.5) { a = get(); } console.log(a); }
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
prefer-const - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >ES6 JavaScript - const inside or let outside loop?
I'd say const would make optimisation out of the loop even more likely considering the compiler can instantly see that it's an immutable...
Read more >Prefer const rule triggered in loops : r/typescript - Reddit
Each loop run creates its own context, so it will not reassign the variable, the old context is lost by that point.
Read more >`prefer-const` barks at looping variables · Issue #4088 - GitHub
In strict mode const works fine. (All these are tested with Node.js v4.1.2). Is it really safe/okay to suggest changing loop variable to...
Read more >ES6 Variable Declaration & For Loops — Why 'const' works in ...
Const is special, because variables defined with it are constants that can't be reassigned or redeclared.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think prefer-const is the right place for this warning because you aren’t asking specifically if the variable can be a const, you’re asking if it’s declared in the right spot. I think the prefer-smaller-scope approach is the correct solution to this problem (assuming we still want to solve it)
@snitin315 please just move the issue into the Feedback Needed column if you are requesting help. The @-mention pings over 20 people including alumni, so we try not to do that. You can ping the TSC directly if you want more attention.
eslint-plugin-unicorn
seems to be carrying the torch for rules which might not be strictly necessary for preventing errors but which enforce good practices like this and help keep code clean. Maybe this could be proposed there.