New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prefer-const
not triggered (regression from 2.4.0)
#5837
Comments
@mysticatea Could you take a look please? |
@fabienjuif |
Config file :
Code : Is the same |
Thank you for this issue. Hm, I cannot reproduce this on my local environment.
|
I had this problem and could reproduce the test case but it was fixed when I removed |
That plugin flags |
@stevoland interesting! @fabienjuif can you check that? |
yes thats right - I raised this on the react plugin - jsx-eslint/eslint-plugin-react#716 after some investigation, as @stevoland suggested, eslint-plugin-react marks the variable with eslintUsed which then causes prefer-const to ignore the variable because of this line - eslint/lib/rules/prefer-const.js Line 86 in 421e4bf
and searching through the history there used to be a better explanation of why eslint ignores in this case eslint/lib/rules/prefer-const.js Line 184 in 361377f
from that file
so I presume that is to stop this
because in that case, converting the var to a const would change the meaning of the code (a would no longer be defined). maybe we can only ignore eslintUsed variables if they are var so that |
@lukeapage Thank you for investigating.
(Please never mind this sentence, I'm searching my head...) |
How does the reasons of usage change whether prefer-const should be applied? Can you give an example? |
I'm sorry, I was confused. I found my memory. let a;
for (let i = 0; i < 10; ++i) {
a = doSomething();
doSomething2(a);
}
// it can be modified as below.
for (let i = 0; i < 10; ++i) {
let a = doSomething();
doSomething2(a);
} But in the following case (i.e., if there is read before write), the rule should not warn the code. let a;
for (let i = 0; i < 10; ++i) {
doSomething2(a); // this uses the value of the previous iteration.
a = doSomething();
} Now, in the following case, if variable let a;
for (let i = 0; i < 10; ++i) {
/* a code to set `eslintUsed`. */
a = doSomething();
} So, I wrote the condition |
But currently, let a;
for (let i = 0; i < 10; ++i) {
a = doSomething(); // this is inside a different scope from `let a`, so this `a` is not warned.
} So probably we can remove the check of |
Just to confirm, let foo;
const recursive = () => {
return foo;
};
foo = {
dontAsk: recursive
}; |
@ljharb It would be warned by default. It can be modified to: const foo = {
dontAsk: () => {
return foo
}
} Also, we can make the rule ignoring that case by ignoreReadBeforeAssign option. |
@mysticatea ah sorry. My mistake. #5822 Ended up being resolved by adding the option you mentioned. |
@mysticatea any news on this? This check is breaking |
Oh, I'm sorry. I will fix this. |
Oh, wait, but I'm not sure whether it's the issue of this original post or not. Maybe should we separate issue? |
The issue is that any variable that is marked as used by It was the case for But we'll still have the same problem with let App = <div />; // should warn "'App' is never reassigned. Use 'const' instead" but will not
<App /> So it is still the same issue, just on another node type. But I can open another issue if it makes it easier for you. |
Thank you for explaining. |
What version of ESLint are you using?
2.7.0
What parser (default, Babel-ESLint, etc.) are you using?
Please show your full configuration:
What did you do? Please include the actual source code causing the issue.
What did you expect to happen?
prefer-const
should trigger and ask me to changelet dynStyle
toconst dynStyle
What actually happened? Please include the actual, raw output from ESLint.
ESLint don't trigger the
prefer-const
rule.Note I tried these versions :
~2.4.0
: Works~2.5.0
: Doesn't work~2.6.0
: Doesn't work~2.7.0
: Doesn't workCommits that are related to
prefer-const
(maybe I miss some) :2.6
: 9b73ffd Update: destructuring option of prefer-const rule (fixes prefer-const should somehow figure out mixed destructuring #5594) (Toru Nagashima)2.5
: 9a22625 Fix: prefer-const false positive at non-blocked if (fixesprefer-const
false positive #5610) (Toru Nagashima)2.5
: 87d74b2 Fix: prefer-const got to not change scopes (refs Rule Proposal:prefer-smaller-scope
#5284) (Toru Nagashima)The text was updated successfully, but these errors were encountered: