Skip to content
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

Merged
merged 7 commits into from Mar 20, 2020

Conversation

Retsam
Copy link
Contributor

@Retsam Retsam commented Jan 28, 2020

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:

function example(arr: number[]) {
  if(arr[0]) {}
}

This version only handles the "obvious" case where the index signature is directly inside the condition - it will still raise errors on code like:

function example(arr: number[]) {
  const head = array[0];
  if(head) {}
}

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

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]) {
    //...
  }
}
```
@typescript-eslint
Copy link
Contributor

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.

@bradzacher bradzacher added the enhancement: plugin rule option New rule option for an existing eslint-plugin rule label Jan 28, 2020
@codecov
Copy link

codecov bot commented Jan 28, 2020

Codecov Report

Merging #1534 into master will increase coverage by 0.01%.
The diff coverage is 100%.

@@            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
Flag Coverage Δ
#unittest 95.2% <100%> (+0.01%) ⬆️
Impacted Files Coverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts 99.16% <100%> (+0.14%) ⬆️

@Retsam
Copy link
Contributor Author

Retsam commented Jan 28, 2020

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]) {}

Copy link
Member

@bradzacher bradzacher left a 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

@bradzacher bradzacher added 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready awaiting response Issues waiting for a reply from the OP or another party and removed 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready labels Feb 19, 2020
@Retsam Retsam requested a review from bradzacher March 2, 2020 17:52
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Mar 2, 2020
@bradzacher bradzacher changed the title fix(eslint-plugin): [no-unnecessary-condition] ignore basic array indexing false positives feat(eslint-plugin): [no-unnecessary-condition] ignore basic array indexing false positives Mar 2, 2020
Copy link
Contributor

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Copy link
Member

@bradzacher bradzacher left a 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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[no-unnecessary-condition] False positives with arrays and optional chains
3 participants