-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
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.
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 => |
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.
We already have a isDefined util, could we use that instead ?
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.
isNullable => isNotDefined
isNonNullable => isDefined
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.
Hey @lucasbordeau,
Can't seem to find this isDefined
util? If you may kindly direct me to where it is located?
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.
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:
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.
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.
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; |
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.
We should avoid creating utils like this as it circumvents the rule by applying the same behavior as an if.
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.
We want to prevent implicit boolean coercion : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion
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.
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`; |
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.
You can disable the rule here and add a TODO
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.
Alright!
I'm waiting for your modifications and will extensively test the application after that. |
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
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.
@gitstart-twenty Thank you! I'm going to merge it could you open another PR (same ticket) to add a test?
… statements (twentyhq#4354) * ESLint rule: only take explicit boolean predicates in if statements Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]> * Merge main Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]> * Fix frontend linter errors Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]> * Fix jest Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]> * Refactor according to review Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]> * Refactor according to review Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]> * Fix lint on new code Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]> --------- Co-authored-by: gitstart-twenty <[email protected]> Co-authored-by: v1b3m <[email protected]> Co-authored-by: Toledodev <[email protected]>
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