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 no-non-null-asserted-nullish-coalescing
rule
#3349
feat(eslint-plugin): add no-non-null-asserted-nullish-coalescing
rule
#3349
Conversation
Thanks for the PR, @sonallux! 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! |
6e00a08
to
c5aab46
Compare
Codecov Report
@@ Coverage Diff @@
## master #3349 +/- ##
==========================================
- Coverage 93.53% 93.52% -0.01%
==========================================
Files 149 150 +1
Lines 8043 8067 +24
Branches 2550 2556 +6
==========================================
+ Hits 7523 7545 +22
- Misses 164 165 +1
- Partials 356 357 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks great so far! But there's one case we probably need to handle the case of a variable being used before it's assigned.
let x: string;
x! ?? '';
Reporting here is bad because there's no way for the user to satisfy it. Which is annoying and reduces user trust.
I think that we could handle this using scope analysis.
So by getting the variable declaration you can inspect its usages to see if it has any writes to it, and if it doesn't then it's a variable that is used before it's assigned.
You can also check the declaration for the weird "trust me it's assigned" syntax: const x!: number
Let me know if you want any pointers on how to use the scope analyser.
You can search this codebase for context.getScope()
for a few examples.
packages/eslint-plugin/src/rules/no-non-null-asserted-nullish-coalescing.ts
Outdated
Show resolved
Hide resolved
@bradzacher I have added support for handling the use-before-assigned case. I hope I did not forget anything. |
4bf9da1
to
9379caa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests look good. Implementation is almost correct.
Thanks for your work here!
packages/eslint-plugin/src/rules/no-non-null-asserted-nullish-coalescing.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/docs/rules/no-non-null-asserted-nullish-coalescing.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-non-null-asserted-nullish-coalescing.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-non-null-asserted-nullish-coalescing.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-non-null-asserted-nullish-coalescing.ts
Outdated
Show resolved
Hide resolved
@bradzacher Thanks for the feedback. I have already fixed all your comments. |
There was a problem hiding this 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!
# Conflicts: # packages/eslint-plugin/README.md
Adds a
no-non-null-asserted-nullish-coalescing
rule that disallows using the non-null assertion on the left operand of the nullish coalescing operator. The non-null assertion is not redundant here because the nullish coalescing operator is explicitly handlingnull
andundefined
.Incorrect code for this rule:
fixes #2853