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-meaningless-void-operator
rule
#3641
feat(eslint-plugin): add no-meaningless-void-operator
rule
#3641
Conversation
Thanks for the PR, @jtbandes! 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 Report
@@ Coverage Diff @@
## master #3641 +/- ##
==========================================
+ Coverage 92.67% 93.57% +0.90%
==========================================
Files 327 148 -179
Lines 11357 7890 -3467
Branches 3204 2496 -708
==========================================
- Hits 10525 7383 -3142
+ Misses 370 162 -208
+ Partials 462 345 -117
Flags with carried forward coverage won't be shown. Click here to find out more. |
Nx Cloud ReportCI ran the following commands for commit c5e9937. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this branch
Sent with 💌 from NxCloud. |
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.
The rule makes sense to me, and the code LGTM.
One change required: It's not correct or safe to autofix void (never)
to (never)
.
Because typeof void (never) === undefined
, but typeof never === never
.
This could cause TS build errors - which breaks the cardinal rule of ESLint fixers (don't break the build).
Example - imagine that the client code and server response are out-of-sync due to the client not having refreshed their browser:
function foo(arg: 'a' | 'b') {
switch (arg) {
case 'a':
return 'ayyy';
case 'b':
return 'beee';
default:
return void arg;
}
}
function getPayloadFromServer(): 'a' | 'b' {
return 'c'; // oops - the client code is behind the server code
}
foo(getPayloadFromServer()); // returns `undefined`
If you remove the void
here it'll instead return 'c'
- which is a change in runtime behaviour.
Obviously the code above is pretty dumb and likely wouldn't exist in production, but it's valid code that we have to be weary of.
I'm happy for the never check to be behind an option, however the never
fix would have to be a suggestion fixer instead of an autofixer (as it's potentially unsafe)
packages/eslint-plugin/docs/rules/no-meaningless-void-operator.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-meaningless-void-operator.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-meaningless-void-operator.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
Added a |
Hi @bradzacher – anything else you need from me here? Thanks for your time 🙂 |
…rectives The RuleTester provides the ability to assert on specific error messages and fixes that rules produce. It's also more in line with how the upstream projects (eslint and typescript-eslint) test their rules, which will smooth the process if we decide to upstream these rules. The tests for (and changes to) no-meaningless-void-operator are changes I made based on feedback on my typescript-eslint PR: typescript-eslint/typescript-eslint#3641
…rectives The RuleTester provides the ability to assert on specific error messages and fixes that rules produce. It's also more in line with how the upstream projects (eslint and typescript-eslint) test their rules, which will smooth the process if we decide to upstream these rules. The tests for (and changes to) no-meaningless-void-operator are changes I made based on feedback on my typescript-eslint PR: typescript-eslint/typescript-eslint#3641
…rectives (#31) **Public-Facing Changes** - `no-boolean-parameters` correctly reports the function name in variable assignments like `const x = (a: boolean) => {}` - `no-meaningless-void-operator` no longer reports errors on `never` values by default; added `checkNever` option to enable (imported these changes from typescript-eslint/typescript-eslint#3641) **Description** The `RuleTester` provides the ability to assert on specific error messages and fixes that rules produce. It's also more in line with how the upstream projects (eslint and typescript-eslint) test their rules, which will smooth the process if we decide to upstream these rules. The tests for (and changes to) no-meaningless-void-operator are changes I made based on feedback on my typescript-eslint PR: typescript-eslint/typescript-eslint#3641 Imported jest.config.js from template-typescript.
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.
thanks for this!
**Public-Facing Changes** Removes `@foxglove/no-meaningless-void-operator`, because this rule has been upstreamed as `@typescript-eslint/no-meaningless-void-operator` (typescript-eslint/typescript-eslint#3641), as of `@typescript-eslint/eslint-plugin` version 4.31.0.
Report an error on
void x
whenx
is of typevoid
,undefined
, ornever
(and automatically delete the void operator).This rule was first implemented (by me) in https://github.com/foxglove/eslint-plugin. As it could be generally useful I'm proposing to add it to @typescript-eslint/eslint-plugin.