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

Add no-boolean-literal-compare rule #2013

Merged
merged 2 commits into from Jan 11, 2017

Conversation

andy-hanson
Copy link
Contributor

PR checklist

What changes did you make?

Added the no-boolean-compare rule, which warns for x === true.

@@ -111,6 +111,10 @@ export class Replacement {
return replacements.reduce((text, r) => r.apply(text), content);
}

public static deleteFromTo(start: number, end: number): Replacement {
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 use RuleWalker.deleteText(start: number, length: number) instead?


function operatorKind(operatorToken: ts.BinaryOperatorToken): boolean | undefined {
switch (operatorToken.kind) {
case ts.SyntaxKind.EqualsEqualsToken: case ts.SyntaxKind.EqualsEqualsEqualsToken:
Copy link
Contributor

Choose a reason for hiding this comment

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

cases on separate lines

@@ -0,0 +1,30 @@
declare const x: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

add cases for more complex expressions like functions, result of binary boolean expression, and negating a boolean

@adidahiya
Copy link
Contributor

how about a more explicit name to clarify that this only lints for literals? no-boolean-literal-compare

@andy-hanson andy-hanson changed the title Add no-boolean-compare rule Add no-boolean-literal-compare rule Jan 11, 2017
function needsParenthesesForNegate(node: ts.Expression) {
switch (node.kind) {
case ts.SyntaxKind.AsExpression:
case ts.SyntaxKind.BinaryExpression:
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a more reliable way to check?

ternary?

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 can't think of a better way to do this, unfortunately.
A ternary operator wouldn't happen here since it === binds more tightly, so the ternary would have to be parenthesized already anyway.

@nchen63 nchen63 merged commit a6d3384 into palantir:master Jan 11, 2017
@nchen63
Copy link
Contributor

nchen63 commented Jan 11, 2017

thanks, @andy-hanson

@andy-hanson andy-hanson deleted the no_boolean_compare branch January 11, 2017 02:21
@andy-hanson
Copy link
Contributor Author

Remember to close #1910

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.

None yet

3 participants