Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add ignore-rhs option for strict-boolean-expressions #4159

Merged
merged 2 commits into from
Jan 8, 2019

Conversation

dobesv
Copy link
Contributor

@dobesv dobesv commented Sep 8, 2018

PR checklist

Overview of change:

If specified, the right-hand operand of && and || will not be checked
as a strict boolean expression.

CHANGELOG.md entry:

[new-rule-option] strict-boolean-expressions accepts ignore-rhs option to disable checking the right-hand side of the && and || operators as strictly boolean.

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @dobesv! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@dobesv dobesv force-pushed the strict-boolean-ignore-rhs branch 2 times, most recently from 10f89f2 to f702d97 Compare September 8, 2018 18:30
@dobesv
Copy link
Contributor Author

dobesv commented Sep 21, 2018

Is there anything else I need to (or can) do for this PR to move forward?

@krishan
Copy link

krishan commented Oct 25, 2018

I would love for #4158 to get solved, as it's blocking me from using strict-boolean-expresssions in my project.

So I'm wondering, what's up with this PR?
What would be needed to move this forward?

@Kinrany
Copy link

Kinrany commented Oct 29, 2018

Has @palantirtech already updated this PR, or is it still waiting for the contributor license agreement to be signed?

@dobesv
Copy link
Contributor Author

dobesv commented Oct 30, 2018

It's just waiting for a team member review, I guess. I haven't heard any feedback about it and I signed the CLA, so assuming I read it right it's just a matter of someone with write access reviewing and approving it.

@dobesv
Copy link
Contributor Author

dobesv commented Nov 5, 2018

Fixes #3279

@dobesv
Copy link
Contributor Author

dobesv commented Nov 5, 2018

@JoshuaKGoldberg can you review this one?

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Nov 5, 2018

Will do, soon!

Note: I'm new to reviewing & maintaining TSLint, so I won't merge it in soon if it passes review. I'll just leave comments for a more experienced maintainer to double-check. Same with other PRs.

@rhys-vdw
Copy link

Any movement on this one?

@JoshuaKGoldberg
Copy link
Contributor

@rhys-vdw unfortunately no 🙁. The Palantir folks are looking for someone to maintain TSLint full time, but until then there's no SLA on PR reviews.

Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

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

I think the goal here is good and this PR is well-implemented, but unfortunately this is a breaking change which should be introduced in the next major version. Can you please retain the existing behavior by default and instead introduce an option called "ignore-rhs" which addresses #4158? After this merges, I can create a tracking issue for making "ignore-rhs" the default behavior in v6.0

If specified, the right-hand operand of `&&` and `||` will not be checked
as a strict boolean expression, allowing a common use of `&&` and `||` to
check a boolean and return the right-hand operand as an object.
If specified, the right-hand operand of `&&` and `||` will not be checked
as a strict boolean expression, allowing a common use of `&&` and `||` to
check a boolean and return the right-hand operand as an object.
@dobesv
Copy link
Contributor Author

dobesv commented Dec 29, 2018

@adidahiya I reversed the option as requested. Let me know if there's anything else I can do.

@dobesv dobesv changed the title Add check-rhs option for strict-boolean-expressions Add ignore-rhs option for strict-boolean-expressions Dec 29, 2018
@krishan
Copy link

krishan commented Jan 9, 2019

Thank you, looking forward to using this 🎉

@jmoseley
Copy link

How/when does this change get published?

@lukewlms
Copy link

lukewlms commented Apr 8, 2019

@dobesv This is useful, thank you! The ideal for us would be to still disallow RHS non-boolean items in places that require a boolean (e.g. ternary, if/else...) while allowing RHS non-boolean in any other locations (particularly React rendering - someBool && ). Do you think this is a common enough case to address?

@dobesv
Copy link
Contributor Author

dobesv commented Apr 8, 2019

@lukewlms Are you trying to say that you want the rhs to be forced as a boolean in case like:

if(x && 1) return;  // <-- error: 1 is not a boolean

but still ignored in other expressions, like:

const whatever = { b: (Date.now() > 5000) && 1 }; // <-- OK because the surrounding expression is not a conditional

My change only ignores the rhs in the sense that it does not complain if the rhs or && or || is not a boolean. However, the overall expression itself would still not be a boolean, and it's likely an existing rule requiring the condition of if() or the ternary operator would still report a problem independantly of this change.

Have you verified that your case isn't already covered by the rule as it is ?

@lukewlms
Copy link

lukewlms commented Apr 9, 2019

@dobesv Yes, checked it - here's an example. I'd like the first two to get flagged but the third to not be flagged - so in places where we absolutely must have a boolean, we're sure to get a strict one.

const func = (b: boolean, s: string) => {
  // Gets flagged with ignore-rhs on
  if (s) {
    // ...
  }

  // Does not get flagged with ignore-rhs on, but I'd like it to!
  if (b || s) {
    // ...
  }

  // This does not get flagged and that's good!
  return b || s;
}

@dobesv
Copy link
Contributor Author

dobesv commented Apr 9, 2019 via email

@adidahiya
Copy link
Contributor

I don't think we reached a consensus on whether boolean-coerced expressions in conditional statements should be treated differently from expressions in other positions in #3279. So I don't know if it's a "bug" per se. Please do file a new issue if you'd like to keep discussing your proposed rule behavior change.

@lukewlms
Copy link

Logged #4671 for addressing ignoring RHS only when a boolean value is not required.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants