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

fix(eslint-plugin): [no-unnecessary-condition] improve optional chain handling 2 - electric boogaloo#2138

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
bradzacher merged 9 commits intotypescript-eslint:masterfromyeonjuan:issue-1977
Jun 1, 2020

Conversation

yeonjuan
Copy link
Contributor

@yeonjuanyeonjuan commentedMay 31, 2020
edited
Loading

Fixes#1977

#1977 occurs when member expression's nullable type is originated from the previous node.
So, I changed it to check whether a member expression's nullable type is origin from its previous.

@typescript-eslint
Copy link
Contributor

Thanks for the PR,@yeonjuan!

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.

@codecov
Copy link

codecovbot commentedMay 31, 2020
edited
Loading

Codecov Report

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

@@            Coverage Diff             @@##           master    #2138      +/-   ##==========================================+ Coverage   93.54%   93.56%   +0.01%==========================================  Files         171      171                Lines        7798     7811      +13       Branches     2225     2229       +4     ==========================================+ Hits         7295     7308      +13  Misses        244      244                Partials      259      259
FlagCoverage Δ
#unittest93.56% <100.00%> (+0.01%)⬆️
Impacted FilesCoverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts97.26% <100.00%> (+0.24%)⬆️
packages/eslint-plugin/src/rules/ban-types.ts100.00% <0.00%> (ø)
...ackages/eslint-plugin/src/rules/no-var-requires.ts100.00% <0.00%> (ø)

@bradzacherbradzacher added the bugSomething isn't working labelMay 31, 2020
Comment on lines 444 to 446
const ownPropertyType = prevType.types
.map(type => checker.getTypeOfPropertyOfType(type, property.name))
.find(t => t);
Copy link
Member

Choose a reason for hiding this comment

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

Mapping over the types in the union isn't quite right, and will cause issues in the following case. The checker API is smart enough to analyse unions to get the type of a property on that union.

interfaceFoo{a:number;}interfaceBar{a:string|null;}declareconstx:Foo|Bar;
# type = type of x;$ type.types.map(t => checker.typeToString(checker.getTypeOfPropertyOfType(t,'a')))> ["number","string | null"]

Eg with your current logic, this case will just check thenumber case, and will thus false-positive on the property.

If you instead just ask for the property on the union type itself:

# type = type of x;$ checker.typeToString(checker.getTypeOfPropertyOfType(type,'a'))>"string | number | null"

It will work as expected. However if for some reason the base type is nullish, you need to also scrub the null from the union first:

interfaceFoo{a:number;}interfaceBar{a:string|null;}declareconstx:{t:Foo|Bar}|null;x?.t.a;
# type = type of x?.t;$ nonNullType = checker.getNonNullableType(type)$ checker.typeToString(checker.getTypeOfPropertyOfType(nonNullType,'a'))>"string | number | null"

yeonjuan reacted with thumbs up emoji
Copy link
ContributorAuthor

@yeonjuanyeonjuanJun 1, 2020
edited
Loading

Choose a reason for hiding this comment

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

@bradzacher
Thanks for the review. I'll change it soon :)

@bradzacherbradzacher added the awaiting responseIssues waiting for a reply from the OP or another party labelMay 31, 2020
@bradzacherbradzacher removed the awaiting responseIssues waiting for a reply from the OP or another party labelJun 1, 2020
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 your work!

@bradzacherbradzacher changed the titlefix(eslint-plugin): [no-unnecessary-condition] improve optional chain handlingfix(eslint-plugin): [no-unnecessary-condition] improve optional chain handling 2 - electric boogalooJun 1, 2020
@bradzacherbradzacher merged commitc87cfaf intotypescript-eslint:masterJun 1, 2020
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsJul 2, 2020
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@bradzacherbradzacherbradzacher approved these changes

Assignees
No one assigned
Labels
bugSomething isn't working
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[no-unnecessary-condition] optional chaining clarification
2 participants
@yeonjuan@bradzacher

[8]ページ先頭

©2009-2025 Movatter.jp