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 #2111

Merged
merged 4 commits into from May 30, 2020
Merged

fix(eslint-plugin): [no-unnecessary-condition] improve optional chain handling #2111

merged 4 commits into from May 30, 2020

Conversation

yeonjuan
Copy link
Contributor

@yeonjuan yeonjuan commented May 26, 2020

Hi, 😀
IMO, it's a false negative in no-unnecessary-condition

Repro

{
  "rules": {
    "@typescript-eslint/no-unnecessary-condition": ["error"]
  }
}
// case 1
declare const x: { a?: { b: string } };
x?.a?.b;

// case 2
declare const x: { a: { b?: { c: string } } };
x.a?.b?.c;

// case 3
let x: { a?: string };
x?.a;

Expected Result

// case 1
declare const x: { a?: { b: string } };
x?.a?.b;
^^          // lint error 

// case 2
declare const x: { a: { b?: { c: string } } };
x.a?.b?.c;
  ^^        // lint error

// case 3
let x: { a?: string };
x?.a;
^^        // lint error

Actual Result

// case 1
declare const x: { a?: { b: string } };
x?.a?.b; // no error

// case 2
declare const x: { a: { b?: { c: string } } };
x.a?.b?.c; // no error

// case 3
let x: { a?: string };
x?.a;  // no error

Additional Info

It seems "no-unnecessary-condition" checks whether the whole optional member expression type is nullable or not. so it just passed when the last member in the optional member expression is nullable.

I changed the target node to check. Am I miss something? 🤔

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

@yeonjuan yeonjuan changed the title fix(eslint-plugin): [no-unnecessary-condition] handling nullable target fix(eslint-plugin): [no-unnecessary-condition] change nullable target to check May 26, 2020
@codecov
Copy link

codecov bot commented May 26, 2020

Codecov Report

Merging #2111 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2111   +/-   ##
=======================================
  Coverage   93.42%   93.42%           
=======================================
  Files         171      171           
  Lines        7755     7757    +2     
  Branches     2215     2216    +1     
=======================================
+ Hits         7245     7247    +2     
  Misses        244      244           
  Partials      266      266           
Flag Coverage Δ
#unittest 93.42% <100.00%> (+<0.01%) ⬆️
Impacted Files Coverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts 97.01% <100.00%> (+0.04%) ⬆️

@bradzacher bradzacher added the bug Something isn't working label May 26, 2020
@bradzacher
Copy link
Member

Unless I'm mistaken, I believe this is addressing #1977. If I'm right, please add it to the OP 😄

@yeonjuan
Copy link
Contributor Author

yeonjuan commented May 27, 2020

@bradzacher
Well, I found this issue while trying to solve #1977, but it's quite a different issue.
#1977 occurs when a node's nullable type is originated from its parent nullable, but this issue occurs when the OptionalMemberExpression's last member is nullable type. (In my thought)

so this pr can't fix #1977

let x: { a: { b: string } } | null;
x?.a?.b;
   ^^ // a's nullable type is originated from x's type, so this case belong #1977


let x: { a?: string };
x?.a;  // no error
   ^^ // IMO, we should check type of "x" not "a"

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!

@bradzacher bradzacher changed the title fix(eslint-plugin): [no-unnecessary-condition] change nullable target to check fix(eslint-plugin): [no-unnecessary-condition] improve optional chain handling May 30, 2020
@bradzacher bradzacher merged commit 9ee399b into typescript-eslint:master May 30, 2020
@yeonjuan yeonjuan deleted the unn-cond-false-negative branch May 31, 2020 02:38
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 1, 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.

None yet

2 participants