This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
Add no-boolean-literal-compare
rule
#2013
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
ruleName: no-boolean-compare | ||
description: 'Warns on comparison to a boolean literal, as in `x === true`.' | ||
hasFix: true | ||
optionsDescription: Not configurable. | ||
options: null | ||
optionExamples: | ||
- 'true' | ||
type: style | ||
typescriptOnly: true | ||
layout: rule | ||
title: 'Rule: no-boolean-compare' | ||
optionsJSON: 'null' | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
ruleName: no-boolean-literal-compare | ||
description: 'Warns on comparison to a boolean literal, as in `x === true`.' | ||
hasFix: true | ||
optionsDescription: Not configurable. | ||
options: null | ||
optionExamples: | ||
- 'true' | ||
type: style | ||
typescriptOnly: true | ||
requiresTypeInfo: true | ||
layout: rule | ||
title: 'Rule: no-boolean-literal-compare' | ||
optionsJSON: 'null' | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as ts from "typescript"; | ||
|
||
import * as Lint from "../index"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "no-boolean-literal-compare", | ||
description: "Warns on comparison to a boolean literal, as in `x === true`.", | ||
hasFix: true, | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: ["true"], | ||
type: "style", | ||
typescriptOnly: true, | ||
requiresTypeInfo: true, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING(negate: boolean) { | ||
return `This expression is unnecessarily compared to a boolean. Just ${negate ? "negate it" : "use it directly"}.`; | ||
} | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new Walker(sourceFile, this.getOptions(), langSvc.getProgram())); | ||
} | ||
} | ||
|
||
class Walker extends Lint.ProgramAwareRuleWalker { | ||
public visitBinaryExpression(node: ts.BinaryExpression) { | ||
this.check(node); | ||
super.visitBinaryExpression(node); | ||
} | ||
|
||
private check(node: ts.BinaryExpression) { | ||
const comparison = deconstructComparison(node); | ||
if (comparison === undefined) { | ||
return; | ||
} | ||
|
||
const { negate, expression } = comparison; | ||
const type = this.getTypeChecker().getTypeAtLocation(expression); | ||
if (!Lint.isTypeFlagSet(type, ts.TypeFlags.Boolean)) { | ||
return; | ||
} | ||
|
||
const deleted = node.left === expression | ||
? this.deleteFromTo(node.left.end, node.end) | ||
: this.deleteFromTo(node.getStart(), node.right.getStart()); | ||
const replacements = [deleted]; | ||
if (negate) { | ||
if (needsParenthesesForNegate(expression)) { | ||
replacements.push(this.appendText(node.getStart(), "!(")); | ||
replacements.push(this.appendText(node.getEnd(), ")")); | ||
} else { | ||
replacements.push(this.appendText(node.getStart(), "!")); | ||
} | ||
} | ||
|
||
this.addFailureAtNode(expression, Rule.FAILURE_STRING(negate), this.createFix(...replacements)); | ||
} | ||
} | ||
|
||
function needsParenthesesForNegate(node: ts.Expression) { | ||
switch (node.kind) { | ||
case ts.SyntaxKind.AsExpression: | ||
case ts.SyntaxKind.BinaryExpression: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function deconstructComparison(node: ts.BinaryExpression): { negate: boolean, expression: ts.Expression } | undefined { | ||
const { left, operatorToken, right } = node; | ||
const operator = operatorKind(operatorToken); | ||
if (operator === undefined) { | ||
return undefined; | ||
} | ||
|
||
const leftValue = booleanFromExpression(left); | ||
if (leftValue !== undefined) { | ||
return { negate: leftValue !== operator, expression: right }; | ||
} | ||
const rightValue = booleanFromExpression(right); | ||
if (rightValue !== undefined) { | ||
return { negate: rightValue !== operator, expression: left }; | ||
} | ||
return undefined; | ||
} | ||
|
||
function operatorKind(operatorToken: ts.BinaryOperatorToken): boolean | undefined { | ||
switch (operatorToken.kind) { | ||
case ts.SyntaxKind.EqualsEqualsToken: | ||
case ts.SyntaxKind.EqualsEqualsEqualsToken: | ||
return true; | ||
case ts.SyntaxKind.ExclamationEqualsToken: | ||
case ts.SyntaxKind.ExclamationEqualsEqualsToken: | ||
return false; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
|
||
function booleanFromExpression(node: ts.Expression): boolean | undefined { | ||
switch (node.kind) { | ||
case ts.SyntaxKind.TrueKeyword: | ||
return true; | ||
case ts.SyntaxKind.FalseKeyword: | ||
return false; | ||
default: | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
declare const x: boolean; | ||
|
||
x; | ||
x; | ||
|
||
!x; | ||
!x; | ||
|
||
!x; | ||
!x; | ||
|
||
x; | ||
x; | ||
|
||
x; | ||
|
||
declare const y: boolean | undefined; | ||
y === true; | ||
|
||
declare function f(): boolean; | ||
!f(); | ||
|
||
declare const a: number, b: number; | ||
|
||
!(a as any as boolean); | ||
|
||
!(a < b); | ||
|
||
!!x; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.