Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Adds a special case to suppress "unnecessary condition" errors on code like:```tsfunction 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 onhttps://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitorsper day. |
codecovbot commentedJan 28, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. consttuple=[{}];// Should raise an error here: this check is definitely unnecessaryif(tuple[0]){}declareconstn: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
Uh oh!
There was an error while loading.Please reload this page.
packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
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
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
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