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

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;