question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`prefer-const` does not detect variables declared outside loop

See original GitHub issue

What 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:

https://eslint.org/demo#eyJ0ZXh0IjoiZnVuY3Rpb24gZm9vICgpIHtcblx0bGV0IGE7XG5cdHdoaWxlIChNYXRoLnJhbmRvbSgpID4gMC41KSB7XG5cdFx0YSA9IGdldCgpO1xuXHRcdGNvbnNvbGUubG9nKGEpO1xuXHR9XG59Iiwib3B0aW9ucyI6eyJwYXJzZXJPcHRpb25zIjp7ImVjbWFWZXJzaW9uIjoxMiwic291cmNlVHlwZSI6InNjcmlwdCIsImVjbWFGZWF0dXJlcyI6e319LCJydWxlcyI6eyJwcmVmZXItY29uc3QiOjJ9LCJlbnYiOnt9fX0=

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 assignment

    function foo () {
    	let a;
    	while (Math.random() > 0.5) {
    		a = get(a);
    		console.log(a);
    	}
    }
    
  • a is actually used in the outer scope

    function foo () {
    	let a;
    	while (Math.random() > 0.5) {
    		a = get();
    	}
    	console.log(a);
    }
    

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
nzakascommented, Aug 10, 2021

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.

2reactions
brettz9commented, Sep 2, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found