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): add rule no-confusing-non-null-assertion #1941

Merged
merged 24 commits into from Jun 7, 2020

Conversation

zhangciwu
Copy link
Contributor

Fixes #1940
Using a non-null assertions(!) and an equals test(==) together makes code looks very similar with not equals test(!==), look at below code:

a! == b; // a non-null assertions(`!`) and an equals test(`==`)
a !== b; // not equals test(`!==`)
a! === b; // a non-null assertions(`!`) and an triple equals test(`===`)

I have submit an issue on typescript repo Issue: Easy misunderstanding: "! ===", and a PR to fix it.

And I think it's good for many people to avoid this writing by composing a lint rule

Doc at packages/eslint-plugin/docs/rules/confusing-non-null-assertion-like-not-equal.md in this PR

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @zhangciwu!

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.

@zhangciwu
Copy link
Contributor Author

zhangciwu commented Apr 27, 2020

A problem: in the doc, code are altered by git hooks,
(line 36 in confusing-non-null-assertion-like-not-equal.md)

const isEqualsNum = (1 + foo.num!) == 2;
becomes
const isEqualsNum = 1 + foo.num! == 2;

Now I remove code block language describer to avoid this transformation

(Some other rule "fix" it? )


And lack of languege describer cause a markdown lint error……

@codecov
Copy link

codecov bot commented Apr 27, 2020

Codecov Report

Merging #1941 into master will decrease coverage by 0.05%.
The diff coverage is 82.60%.

@@            Coverage Diff             @@
##           master    #1941      +/-   ##
==========================================
- Coverage   93.45%   93.40%   -0.06%     
==========================================
  Files         171      172       +1     
  Lines        7813     7844      +31     
  Branches     2227     2239      +12     
==========================================
+ Hits         7302     7327      +25     
- Misses        244      245       +1     
- Partials      267      272       +5     
Flag Coverage Δ
#unittest 93.40% <82.60%> (-0.06%) ⬇️
Impacted Files Coverage Δ
packages/eslint-plugin/src/configs/all.ts 100.00% <ø> (ø)
...lugin/src/rules/no-confusing-non-null-assertion.ts 82.60% <82.60%> (ø)
...int-plugin/src/rules/require-array-sort-compare.ts 88.23% <0.00%> (-11.77%) ⬇️

@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Apr 27, 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.

mostly LGTM.
a few comments.

Also one additional case that should also be handled by this

let x: null | string = null;
x! = 'a';
// looks similar to x != 'a'

// should fix to
x = 'a';

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label May 17, 2020
@zhangciwu
Copy link
Contributor Author

@bradzacher Great idea for x! = 'a';, will work it out; together with rule renaming

@zhangciwu
Copy link
Contributor Author

@bradzacher
Done modifing for reviews,

But for CI / Code style and lint (pull_request)
packages/eslint-plugin/docs/rules/no-confusing-non-null-assertion.md L36
const isEqualsNum = (1 + foo.num!) == 2;
This line just can't get through check:format, prettier think the parentheses should be deleted

@zhangciwu zhangciwu requested a review from bradzacher June 5, 2020 12:21
…ion.md

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Jun 6, 2020
@bradzacher bradzacher changed the title feat(eslint-plugin): add rule: confusing-non-null-assertion-like-not-equal feat(eslint-plugin): add rule: no-confusing-non-null-assertion Jun 6, 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.

logic lgtm - one change for the tests

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Jun 6, 2020
@zhangciwu zhangciwu requested a review from bradzacher June 6, 2020 20:52
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Jun 7, 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.

thanks for your work!

@bradzacher bradzacher changed the title feat(eslint-plugin): add rule: no-confusing-non-null-assertion feat(eslint-plugin): add rule no-confusing-non-null-assertion Jun 7, 2020
@bradzacher bradzacher merged commit 9b51c44 into typescript-eslint:master Jun 7, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New rule: confusing-non-null-assertion-like-not-equal
2 participants