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

boolean-conditional-rendering rule #121

Conversation

rayane-djouah
Copy link
Contributor

@rayane-djouah rayane-djouah commented Oct 3, 2024

Expensify/App#50154

Note: you need to add 'rulesdir/boolean-conditional-rendering': 'error', here.
Draft PR for testing: Expensify/App#50190

@rayane-djouah rayane-djouah marked this pull request as ready for review October 3, 2024 21:34
@rayane-djouah
Copy link
Contributor Author

Draft PR for testing: Expensify/App#50190

@rayane-djouah
Copy link
Contributor Author

@szymonrybczak @mountiny PR ready for review

@rayane-djouah
Copy link
Contributor Author

@mountiny mountiny self-requested a review October 3, 2024 22:38
Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

@szymonrybczak can you please review?

eslint-plugin-expensify/boolean-conditional-rendering.js Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Empty file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed to fix this tests error:

Screenshot 2024-10-03 at 11 59 12 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +24 to +41
function isBoolean(type) {
return (
(type.getFlags()
& (ts.TypeFlags.Boolean
| ts.TypeFlags.BooleanLike
| ts.TypeFlags.BooleanLiteral))
!== 0
|| (type.isUnion()
&& _.every(
type.types,
t => (t.getFlags()
& (ts.TypeFlags.Boolean
| ts.TypeFlags.BooleanLike
| ts.TypeFlags.BooleanLiteral))
!== 0,
))
);
}

Choose a reason for hiding this comment

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

I see that mainly this part changed from my initial PoC, and it seems to be a lot better as it uses TypeScript helper functions and is more robust and accurate. Because of this change it seems like there are a lot more errors in the Expensify codebase because this condition is actually correctly validating all possible cases and initial 1 error turns out to be 150+ errors. 🥲

However I did take a look at some of the errors and around half of them are because the left-side value is of type boolean | undefined I believe we shouldn't raise an error for such case, rest of them are accurate.

In such a scenario I don't should we proceed with implementing this rule, as there'll be a lot of changed in Expensify codebase 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@szymonrybczak Can we resolve most errors by using !! to explicitly convert values to booleans?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mountiny What do you think of the above comments?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think its ok to make 150 changes if it will lead to cleaner code and if we can just fix it with !!

},
});

ruleTester.run('boolean-conditional-rendering', rule, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add a test for the boolean | undefined case we have seen a lot in the app?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a test case to handle boolean | undefined in this commit: fbbe2b2. However, the test is failing, as seen in the workflow here: GitHub Actions Run. @szymonrybczak @mountiny Any insights on why this might be happening?

Copy link
Contributor

Choose a reason for hiding this comment

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

The test seems right to me and considering this is correctly flagging in App based on your testing, I am not sure. Asked Szymon in Slack

Choose a reason for hiding this comment

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

But should we raise an error if the value is boolean | undefined? IMO we shouldn't, if the value is equal undefined the condition will be false.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think its better to be more strict when it comes to these than less, it should be easily resolvable by using !!

@mountiny
Copy link
Contributor

@rayane-djouah @szymonrybczak how is this look ing, are you able to debug the jest tests?

@szymonrybczak
Copy link

Hm, this issue is very strange. For some reason when checking flags in an union type boolean | undefined - flag for both types are equal to 512 which is BooleanLiteral and that's why the condition is true and test scenario marks it as valid.

@rayane-djouah
Copy link
Contributor Author

Yeah, it's odd. Shouldn't the flags for the undefined type be ts.TypeFlags.Undefined?

@mountiny
Copy link
Contributor

Lets skip this and allow boolean || undefined since the effect will be the same. Lets not spend more time on this if we have not figured this out by now

@AndrewGable
Copy link
Contributor

Please pull main and merge back in, but fyi publishing is working again.

@rayane-djouah
Copy link
Contributor Author

Updated

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

Thanks!

@mountiny mountiny merged commit 17c4d7f into Expensify:main Oct 17, 2024
2 checks passed
@os-botify
Copy link
Contributor

os-botify bot commented Oct 17, 2024

🚀 Published in 2.0.71 🎉

@rayane-djouah rayane-djouah deleted the add-eslint-rule-for-validating-left-side-condition branch October 17, 2024 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants