forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-boolean-literal-compare
rule (palantir#2013)
- Loading branch information
1 parent
25de3f9
commit a6d3384
Showing
10 changed files
with
349 additions
and
22 deletions.
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.