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 no-meaningless-void-operator rule #3641

Merged

Conversation

@jtbandes
Copy link
Contributor

@jtbandes jtbandes commented Jul 18, 2021

Report an error on void x when x is of type void, undefined, or never (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.

@typescript-eslint
Copy link

@typescript-eslint typescript-eslint bot commented Jul 18, 2021

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.

Loading

@codecov
Copy link

@codecov codecov bot commented Jul 18, 2021

Codecov Report

Merging #3641 (ed70f1c) into master (26de645) will increase coverage by 0.90%.
The diff coverage is 100.00%.

@@            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     
Flag Coverage Δ
unittest 93.57% <100.00%> (+0.90%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/eslint-plugin/src/configs/all.ts 100.00% <ø> (ø)
...t-plugin/src/rules/no-meaningless-void-operator.ts 100.00% <100.00%> (ø)
packages/scope-manager/src/lib/es2017.intl.ts
packages/scope-manager/src/lib/es2018.promise.ts
packages/scope-manager/src/lib/esnext.intl.ts
packages/scope-manager/src/scope/TypeScope.ts
packages/scope-manager/src/lib/es2015.reflect.ts
...ges/experimental-utils/src/ts-eslint/RuleTester.ts
...ackages/scope-manager/src/scope/MappedTypeScope.ts
...e-manager/src/scope/FunctionExpressionNameScope.ts
... and 173 more

Loading

@nx-cloud
Copy link

@nx-cloud nx-cloud bot commented Jul 18, 2021

Nx Cloud Report

CI 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

Status Command
#000000 nx run-many --target=build --all --parallel
#000000 nx run-many --target=typecheck --all --parallel

Sent with 💌 from NxCloud.

Loading

Copy link
Member

@bradzacher bradzacher left a comment

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)

Loading

Loading
Loading
Loading
@jtbandes jtbandes requested a review from bradzacher Aug 2, 2021
@jtbandes
Copy link
Contributor Author

@jtbandes jtbandes commented Aug 2, 2021

Added a checkNever option which is false by default.

Loading

@jtbandes
Copy link
Contributor Author

@jtbandes jtbandes commented Aug 12, 2021

Hi @bradzacher – anything else you need from me here? Thanks for your time 🙂

Loading

jtbandes added a commit to foxglove/eslint-plugin that referenced this issue Aug 30, 2021
…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
jtbandes added a commit to foxglove/eslint-plugin that referenced this issue Aug 30, 2021
…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
jtbandes added a commit to foxglove/eslint-plugin that referenced this issue Aug 30, 2021
…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.
Copy link
Member

@bradzacher bradzacher left a comment

thanks for this!

Loading

@bradzacher bradzacher merged commit ea40ab6 into typescript-eslint:master Sep 3, 2021
11 checks passed
Loading
@jtbandes jtbandes deleted the no-meaningless-void-operator branch Sep 3, 2021
jtbandes added a commit to foxglove/eslint-plugin that referenced this issue Sep 9, 2021
**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.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants