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
[no-unnecessary-condition] False positive with nullish coalescing #2384
Comments
Are you using the |
I'm using |
Sorry, it's just my first question before properly reading the issue because 95% of the time the user hasn't got it turned on, so the rule ofc doesn't work as expected. Can confirm this is a bug. Pretty simple to fix - it shouldn't inspect the right hand side of a nullish coalesce. |
No problem, I understand where did this first question come from. I was actually going to post my findings (from my little debugging session, quite similar to what you've found, but probably not as accurate), but it looks like you already solved the issue, cool :) |
Maybe that should be part of the issue template. |
So many users don't read issue templates. So many don't even glance at the comments. There's also the issue search, which many people don't use. Instead I'm just making the executive decision to stop these rules working (by default) without |
Repro
Expected Result
No ESLint errors reported (condition inside the function is falsy only when we explicitly pass
testVal = false
).Actual Result
2:20 error Unnecessary conditional, value is always truthy @typescript-eslint/no-unnecessary-condition
Additional info
I've noticed this recently when reading about additional options introduced in #1983, namely
allowComparingNullableBooleansToFalse
. With that option set tofalse
, code like this:... turns into this with an autofix:
This is a valid transformation, since there should be only one situation when
console.log()
is run - when thetestVal
argument was explicitly set tofalse
. Therefore,no-unnecessary-condition
incorrectly reports that nullish coalescing is not needed here.Versions
@typescript-eslint/eslint-plugin
3.9.0
@typescript-eslint/parser
3.9.0
TypeScript
3.9.7
ESLint
7.6.0
node
14.7.0
npm
6.14.7
@typescript-eslint/eslint-plugin
4.0.0-rc (2edbca380bd8f317cd96bac0df5030ddcd14a6af)
@typescript-eslint/parser
4.0.0-rc (2edbca380bd8f317cd96bac0df5030ddcd14a6af)
TypeScript
4.0.0-beta
ESLint
7.2.0
node
14.7.0
npm
6.14.7
The text was updated successfully, but these errors were encountered: