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): Contextual non-null [no-unnecessary-type-assertion] #478

Merged
merged 3 commits into from
May 9, 2019

Conversation

bradzacher
Copy link
Member

Fix handling of "used before assigned" variables:
Fixes #453

Note: this fix isn't perfect, but it's "good enough".
The type checker exposes no mechanism to detect this case (but it has code for it: https://raw.githubusercontent.com/Microsoft/TypeScript/84076a55351684296f7b3f1d2715690acbe8039f/src/compiler/checker.ts and search for Variable_0_is_used_before_being_assigned)

See: microsoft/TypeScript#31124 and microsoft/TypeScript#20221


Also adds handling for contextually unnecessary non-null operator based on where it's used:

declare function nonNull(s: string | null);

let s: string | null = null;
nonNull(s!); // contextually unnecessary, as function accepts the same type

Will look at adding contextually unnecessary support to the as branch later - this PR is big enough as is...

@bradzacher bradzacher added the enhancement New feature or request label Apr 26, 2019
@codecov
Copy link

codecov bot commented May 9, 2019

Codecov Report

Merging #478 into master will decrease coverage by 0.14%.
The diff coverage is 85.91%.

@@            Coverage Diff             @@
##           master     #478      +/-   ##
==========================================
- Coverage   95.88%   95.73%   -0.15%     
==========================================
  Files          79       79              
  Lines        3571     3613      +42     
  Branches      998     1013      +15     
==========================================
+ Hits         3424     3459      +35     
- Misses         51       53       +2     
- Partials       96      101       +5
Impacted Files Coverage Δ
...ges/eslint-plugin/src/rules/no-extraneous-class.ts 100% <100%> (ø) ⬆️
packages/eslint-plugin/src/rules/member-naming.ts 100% <100%> (ø) ⬆️
packages/eslint-plugin/src/util/types.ts 73.33% <77.77%> (+10.83%) ⬆️
...-plugin/src/rules/no-unnecessary-type-assertion.ts 91.17% <87.23%> (-6.5%) ⬇️

@bradzacher bradzacher merged commit 4cd5590 into master May 9, 2019
@bradzacher bradzacher deleted the more-unnecessary-assertion branch May 9, 2019 16:32
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2020
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-type-assertion] false positive on uninitialized variables
2 participants