Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

Retsam
Copy link
Contributor

@RetsamRetsam commentedJan 28, 2020
edited
Loading

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:

functionexample(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:

functionexample(arr:number[]){consthead=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 asArray<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

bradzacher and pekala reacted with thumbs up emojicarlegbert and pekala reacted with heart emoji
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]) {    //...  }}```
@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 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.

@bradzacherbradzacher added the enhancement: plugin rule optionNew rule option for an existing eslint-plugin rule labelJan 28, 2020
@codecov
Copy link

codecovbot commentedJan 28, 2020
edited
Loading

Codecov Report

Merging#1534 intomaster willincrease coverage by0.01%.
The diff coverage is100%.

@@            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
FlagCoverage Δ
#unittest95.2% <100%> (+0.01%)⬆️
Impacted FilesCoverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts99.16% <100%> (+0.14%)⬆️

@Retsam
Copy link
ContributorAuthor

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

Copy link
Member

@bradzacherbradzacher 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

@bradzacherbradzacher added 1 approval>=1 team member has approved this PR; we're now leaving it open for more reviews before we merge awaiting responseIssues waiting for a reply from the OP or another party and removed 1 approval>=1 team member has approved this PR; we're now leaving it open for more reviews before we merge labelsFeb 19, 2020
@RetsamRetsam requested a review frombradzacherMarch 2, 2020 17:52
@bradzacherbradzacher removed the awaiting responseIssues waiting for a reply from the OP or another party labelMar 2, 2020
@bradzacherbradzacher changed the titlefix(eslint-plugin): [no-unnecessary-condition] ignore basic array indexing false positivesfeat(eslint-plugin): [no-unnecessary-condition] ignore basic array indexing false positivesMar 2, 2020
Copy link
Contributor

@G-RathG-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

@bradzacherbradzacher 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

@bradzacherbradzacher added enhancementNew feature or request and removed enhancement: plugin rule optionNew rule option for an existing eslint-plugin rule labelsMar 20, 2020
@bradzacherbradzacher merged commit2b9603d intotypescript-eslint:masterMar 20, 2020
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsApr 20, 2020
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@G-RathG-RathG-Rath approved these changes

@bradzacherbradzacherbradzacher approved these changes

Assignees
No one assigned
Labels
enhancementNew feature or request
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[no-unnecessary-condition] False positives with arrays and optional chains
3 participants
@Retsam@G-Rath@bradzacher

[8]ページ先頭

©2009-2025 Movatter.jp