-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-unnecessary-condition] ignore basic array indexing false positives #1534
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
feat(eslint-plugin): [no-unnecessary-condition] ignore basic array indexing false positives #1534
Conversation
Adds a special case to suppress "unnecessary condition" errors on code like: ```ts function example(arr: number[]) { // type is number, but could be undefined if(arr[0]) { //... } } ```
Thanks for the PR, @Retsam! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
Codecov Report
@@ Coverage Diff @@
## master #1534 +/- ##
=========================================
+ Coverage 95.19% 95.2% +0.01%
=========================================
Files 148 148
Lines 6971 6989 +18
Branches 2010 2017 +7
=========================================
+ Hits 6636 6654 +18
Misses 124 124
Partials 211 211
|
Addressed one more edge-case. Since tuple types are sound when indexing with literal numbers, there's no need to suppress potential errors. const tuple = [{}];
// Should raise an error here: this check is definitely unnecessary
if(tuple[0]) {}
declare const n: number;
// Should not raise an error here, this is equivalent to a normal array index.
if(tuple[n]) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for doing this.
Could you please add a few more cases, just to be sure it handles the spectrum of optional chaining
packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks for this
As discussed here, a current pain-point with no-unnecessary-condition is that array indexes are often a source of false positives because the types don't represent the possibility of an out-of-bounds error.
This adds a check to ignore code like:
This version only handles the "obvious" case where the index signature is directly inside the condition - it will still raise errors on code like:
I believe fixing this would be possible, but I'm not sure it's worth the complexity of implementation.
I considered making this configurable, but I just can't come up with any cases in which you'd actually want the previous behavior. Unlike the object case, I don't think anyone is realistically going to model their arrays as
Array<T | undefined>
for the sake of safe index checks.This I don't think this is considered a breaking-change - all previously valid code is still valid - but if so, I'd rather point at 3.0 than put it behind a temporary option flag.
Fixes #1544