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

[Bug]: jsx-no-leaked-render report && in prop #3719

Closed
2 tasks done
AhmedBaset opened this issue Mar 18, 2024 · 14 comments · Fixed by #3746
Closed
2 tasks done

[Bug]: jsx-no-leaked-render report && in prop #3719

AhmedBaset opened this issue Mar 18, 2024 · 14 comments · Fixed by #3746

Comments

@AhmedBaset
Copy link

AhmedBaset commented Mar 18, 2024

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

Imagine you have a prop (e.g. isOpen), you want to set the value as firstCondition === true && anotherCondition === true. Currently the plugin report wrongly this kind of code which should be valid.

image

Expected Behavior

I expect to not throw

eslint-plugin-react version

7.34.1

eslint version

8.57.0

node version

18

@AhmedBaset AhmedBaset added the bug label Mar 18, 2024
@ljharb
Copy link
Member

ljharb commented Mar 18, 2024

i agree, this should be valid (altho !!firstCondition && !!anotherCondition would be more idiomatic)

@AhmedBaset
Copy link
Author

One more thing is frustrating to me.

number && value might cause unintentional reslut but boolean && value won't.

Why the plugin requires ternary all the time even when no potential unexpected result? Could we use the TypeScrypt parser to only report the potential errors? && conditional rendering is a JS feature not something developer should avoid.

@ljharb
Copy link
Member

ljharb commented Mar 21, 2024

@A7med3bdulBaset specifically because a 0 is falsy but will render a literal zero - that's the point of the rule, and indeed is why && conditional rendering is dangerous.

@AhmedBaset
Copy link
Author

I understand well && and its cons/pros.

I know 0 && content will render 0. My point is why boolean && content, contentOrNullOrUndefinedOrBoolean && content is prevented?

@ljharb
Copy link
Member

ljharb commented Mar 21, 2024

I agree that neither of those should be prevented when the type is known.

@AhmedBaset
Copy link
Author

Thanks @akulsr0 for working on this

@AhmedBaset
Copy link
Author

I know 0 && content will render 0. My point is why boolean && content, contentOrNullOrUndefinedOrBoolean && content is prevented?

I agree that neither of those should be prevented when the type is known.

@ljharb, should I create an issue for this?

@ljharb
Copy link
Member

ljharb commented May 10, 2024

@AhmedBaset if, with the latest unreleased code, you still have an issue, please do file one.

@AhmedBaset
Copy link
Author

<Comp prop={cond1 && cond2} /> was solved. But, dataOrUndefined && data... still an issue. IMO the rule should only complain if the left side is of type number.

@ljharb
Copy link
Member

ljharb commented May 10, 2024

More specifically, it should only complain if the left side is a known type and is not a number.

This should also be an option, I'd think, because TS isn't actually "type safety" and folks may not want to rely on it.

@AhmedBaset
Copy link
Author

More specifically, it should only complain if the left side is a known type and is not a number.

This should also be an option, I'd think, because TS isn't actually "type safety" and folks may not want to rely on it.

hmm. then, worth be an issue?

@ljharb
Copy link
Member

ljharb commented May 10, 2024

Yes.

@AhmedBaset
Copy link
Author

Yes.

Done in #3754

@yusufkinatas
Copy link

Seems like this issue is closed, but I think the code can be improved for checking if left side is boolean or not. Currently, the implementation only looks if the variable is assigned to a boolean in it's current scope. So it is not working if I am using a TS-typed component prop.

I think this can be improved by actually checking TS-type of the variable on the left-side.

Here is an example demonstrating the pain-point.

image

I checked the codebase a bit, but could not find an easy way to get it right by checking the TS type.
I think we need to somehow get to what IDE shows as variable type when hovered.
Screenshot 2024-05-29 at 12 09 19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants