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

Commit

Permalink
4746: fix weird failures in strict-boolean-expressions for 'ignore-rh…
Browse files Browse the repository at this point in the history
…s' option (#4833)

* 4746: fix weird failures in strict-boolean-expressions for 'ignore-rhs' option

* fix test2.1
  • Loading branch information
tanmoyopenroot authored and Josh Goldberg committed Aug 25, 2019
1 parent 00e5d2d commit 85d4125
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/rules/strictBooleanExpressionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function walk(ctx: Lint.WalkContext<Options>, checker: ts.TypeChecker): void {
// on the left side because it will be checked well enough on its own. However,
// if ignore-rhs is on, we have to analyze the overall result of the left
// side no matter what, because its right side might not follow the rules.
if (options.ignoreRhs || !isBooleanBinaryExpression(left)) {
if (!isBooleanBinaryExpression(left)) {
checkExpression(left, b);
}
// If ignore-rhs is on, we don't have to analyze the right hand side
Expand Down
43 changes: 43 additions & 0 deletions test/rules/strict-boolean-expressions/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,46 @@ if (trueOrDate) {}
declare var a: any;
// Allow 'any'
if (a) {}

let bool1:boolean = true
let bool2:boolean = true
let obj:{}|undefined = {}

bool1 && obj && bool2
~~~ [This type is not allowed in the operand for the '&&' operator because it could be undefined. Only booleans are allowed.]
bool1 && bool2 && obj
~~~ [This type is not allowed in the operand for the '&&' operator because it could be undefined. Only booleans are allowed.]
boolExpr && numType || strType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a number. Only booleans are allowed.]
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is a string. Only booleans are allowed.]
boolExpr || numType || strType;
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is a number. Only booleans are allowed.]
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is a string. Only booleans are allowed.]
boolExpr || numType && strType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a number. Only booleans are allowed.]
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a string. Only booleans are allowed.]
boolExpr && numType && strType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a number. Only booleans are allowed.]
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a string. Only booleans are allowed.]
boolExpr || boolType && strType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a string. Only booleans are allowed.]
boolExpr || boolType || strType;
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is a string. Only booleans are allowed.]
boolExpr && boolType || strType;
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is a string. Only booleans are allowed.]

bool1 && obj && bool2
~~~ [This type is not allowed in the operand for the '&&' operator because it could be undefined. Only booleans are allowed.]

obj && bool2 && obj
~~~ [This type is not allowed in the operand for the '&&' operator because it could be undefined. Only booleans are allowed.]
~~~ [This type is not allowed in the operand for the '&&' operator because it could be undefined. Only booleans are allowed.]

obj || bool2 || obj
~~~ [This type is not allowed in the operand for the '||' operator because it could be undefined. Only booleans are allowed.]
~~~ [This type is not allowed in the operand for the '||' operator because it could be undefined. Only booleans are allowed.]

obj || bool2 && obj
~~~ [This type is not allowed in the operand for the '||' operator because it could be undefined. Only booleans are allowed.]
~~~ [This type is not allowed in the operand for the '&&' operator because it could be undefined. Only booleans are allowed.]

36 changes: 31 additions & 5 deletions test/rules/strict-boolean-expressions/ignore-rhs/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,54 @@ anyType && boolType;
numType && boolType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a number. Only booleans are allowed.]
boolType && strType;

boolType && objType && enumType;
#if typescript < 2.2.0
~~~~~~~~~~~~~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is always truthy. Only booleans are allowed.]
#endif

boolType && objType || enumType;

boolType || objType && enumType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is always truthy. Only booleans are allowed.]

boolType || objType || enumType;

bwrapType && boolType;
~~~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is always truthy. Only booleans are allowed.]

boolType || classType;
boolType || anyType;
boolType || numType;

strType || boolType;
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is a string. Only booleans are allowed.]

bwrapType || boolType;
~~~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is always truthy. Only booleans are allowed.]

objType || boolType || enumType;
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is always truthy. Only booleans are allowed.]
~~~~~~~~~~~~~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is always truthy. Only booleans are allowed.]

boolExpr && strType;

numType || boolExpr;
~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is a number. Only booleans are allowed.]

numType && boolExpr || strType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a number. Only booleans are allowed.]
~~~~~~~~~~~~~~~~~~~ [This type is not allowed in the operand for the '||' operator because it could be a number. Only booleans are allowed.]

bwrapType || boolExpr && bwrapType;
~~~~~~~~~ [This type is not allowed in the operand for the '||' operator because it is always truthy. Only booleans are allowed.]

let bool1:boolean = true
let bool2:boolean = true
let obj:{}|undefined = {}

bool1 && obj && bool2
bool1 && bool2 && obj
boolExpr && numType || strType;
boolExpr || numType || strType;
boolExpr || numType && strType;
~~~~~~~ [This type is not allowed in the operand for the '&&' operator because it is a number. Only booleans are allowed.]
boolExpr && numType && strType;
boolExpr || boolType && strType;
boolExpr || boolType || strType;
boolExpr && boolType || strType;

0 comments on commit 85d4125

Please sign in to comment.