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

TWNTY-3794 - ESLint rule: only take explicit boolean predicates in if statements #4354

Merged
merged 9 commits into from
Mar 9, 2024

Conversation

gitstart-app[bot]
Copy link
Contributor

@gitstart-app gitstart-app bot commented Mar 7, 2024

This PR was created by GitStart to address the requirements from this ticket: TWNTY-3794.
This ticket was imported from: TWNTY-3794


Description

ESLint rule: only take explicit boolean predicates in if statements

Refs

#3794

DEMO

https://jam.dev/c/be8b6da7-c49e-4b9d-8ed7-d98f9831b605

Fixes #3794

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
gitstart-twenty and others added 4 commits March 7, 2024 17:08
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Copy link
Contributor

@lucasbordeau lucasbordeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work,

Some minor comments.

Please don't hesitate to deactivate the rule and put a TODO where it seems hazardous to refactor.

Could you create isNotDefined ?

@@ -0,0 +1,4 @@
import { isNull, isUndefined } from '@sniptt/guards';

export const isNullable = (value: any): value is null | undefined =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a isDefined util, could we use that instead ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isNullable => isNotDefined
isNonNullable => isDefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lucasbordeau,
Can't seem to find this isDefined util? If you may kindly direct me to where it is located?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I believe isDefined was there but got removed in a previous commit and replaced with isNonNullable(I'm not so sure about the commit/PR this was done in). Found this example in the commit history:

Screenshot 2024-03-09 at 03 31 20

The only new util here is isNullable and it makes more sense to define it this way than negating isNonNullable i.e isNullable(value) >>>> !isNonNullable(value)

However, we can still get rid of isNullable if you prefer it that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference :

  • isDefined has been removed but shouldn't have been, we'll fix it thanks !
  • isNullable feels wrong because we actually have a TypeScript utility type Nullable and we don't want to check if it's Nullable but check if the runtime value is null | undefined, that's two separate concepts even if close.

@@ -0,0 +1 @@
export const isTruthy = Boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid creating utils like this as it circumvents the rule by applying the same behavior as an if.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@@ -122,11 +124,12 @@ export const beautifyDateDiff = (
['years', 'days'],
);
let result = '';
if (dateDiff.years) result = result + `${dateDiff.years} year`;
if (isTruthy(dateDiff.years)) result = result + `${dateDiff.years} year`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can disable the rule here and add a TODO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright!

@lucasbordeau
Copy link
Contributor

I'm waiting for your modifications and will extensively test the application after that.

gitstart-twenty and others added 4 commits March 9, 2024 00:19
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Copy link
Member

@charlesBochet charlesBochet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gitstart-twenty Thank you! I'm going to merge it could you open another PR (same ticket) to add a test?

@charlesBochet charlesBochet merged commit 17511be into main Mar 9, 2024
15 checks passed
@charlesBochet charlesBochet deleted the TWNTY-3794 branch March 9, 2024 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESLint rule: only take explicit boolean predicates in if statements
3 participants