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

Add 'no-boolean-literal-in-conditional' rule #2534

Conversation

andy-hanson
Copy link
Contributor

PR checklist

Overview of change:

Added the no-boolean-litearl-in-conditional rule, which forbids x ? true : y and the like.

CHANGELOG.md entry:

[new-rule] no-boolean-literal-in-conditional

Copy link
Contributor

@ajafff ajafff left a comment

Choose a reason for hiding this comment

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

This rule is only usefule when compiling with strictNullChecks. That should be documented.
The rule could even check the compiler option and print a warning if necessary

@@ -86,7 +86,7 @@ function fix(node: ts.BinaryExpression, { negate, expression }: Compare): Lint.F
}
}

function needsParenthesesForNegate(node: ts.Expression): boolean {
export function needsParenthesesForNegate(node: ts.Expression): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

if you really need to share this function, move it to utils

return undefined;
}

if (l !== undefined && r !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the following lines need some refactoring really badly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How specifically?

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO those conditional expressions look really nasty. It would help clarity to convert them to if ... else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's already written as an if ... else if ... chain.

const l = getLiteralBoolean(whenTrue);
const r = getLiteralBoolean(whenFalse);
// Perf: only check type of condition if there would be a failure otherwise
if ((l !== undefined || r !== undefined) && !isBoolean(condition, checker)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess isBoolean() assumes strictNullChecks: true?

@andy-hanson
Copy link
Contributor Author

andy-hanson commented Apr 10, 2017

It's useful without strictNullChecks, although it may assume that something is a boolean when it's really boolean | undefined.
Also: This is kind of similar to no-boolean-literal-compare, maybe they should be joined?

@giladgray
Copy link

@andy-hanson please update this branch so we can review it, or close it if no longer relevant. we will close this if we do not hear from you in two weeks.

@ajafff is this still worth it or should we just close?

@johnwiseheart
Copy link
Contributor

Closing due to age and inactivity. Feel free to re-open or create a new pull request if you decide to continue working on this.

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.

4 participants