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

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

Merged
merged 9 commits into from Jun 1, 2020

Conversation

yeonjuan
Copy link
Contributor

@yeonjuan yeonjuan commented May 31, 2020

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 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
Copy link

codecov bot commented May 31, 2020

Codecov Report

Merging #2138 into master will increase coverage by 0.01%.
The diff coverage is 100.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              
Flag Coverage Δ
#unittest 93.56% <100.00%> (+0.01%) ⬆️
Impacted Files Coverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts 97.26% <100.00%> (+0.24%) ⬆️
packages/eslint-plugin/src/rules/ban-types.ts 100.00% <0.00%> (ø)
...ackages/eslint-plugin/src/rules/no-var-requires.ts 100.00% <0.00%> (ø)

@bradzacher bradzacher added the bug Something isn't working label May 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.

interface Foo {
    a: number;
}

interface Bar {
    a: string | null;
}

declare const x: 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 the number 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:

interface Foo {
    a: number;
}

interface Bar {
    a: string | null;
}

declare const x: {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"

Copy link
Contributor Author

@yeonjuan yeonjuan Jun 1, 2020

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 :)

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label May 31, 2020
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Jun 1, 2020
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 your work!

@bradzacher bradzacher changed the title fix(eslint-plugin): [no-unnecessary-condition] improve optional chain handling fix(eslint-plugin): [no-unnecessary-condition] improve optional chain handling 2 - electric boogaloo Jun 1, 2020
@bradzacher bradzacher merged commit c87cfaf into typescript-eslint:master Jun 1, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[no-unnecessary-condition] optional chaining clarification
2 participants