/
strict-boolean-expressions.ts
89 lines (82 loc) · 2.62 KB
/
strict-boolean-expressions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import ts from 'typescript';
import * as util from '../util';
type ExpressionWithTest =
| TSESTree.ConditionalExpression
| TSESTree.DoWhileStatement
| TSESTree.ForStatement
| TSESTree.IfStatement
| TSESTree.WhileStatement;
export default util.createRule({
name: 'strict-boolean-expressions',
meta: {
type: 'suggestion',
docs: {
description: 'Restricts the types allowed in boolean expressions',
category: 'Best Practices',
recommended: false,
},
schema: [],
messages: {
strictBooleanExpression: 'Unexpected non-boolean in conditional.',
},
},
defaultOptions: [],
create(context) {
const service = util.getParserServices(context);
const checker = service.program.getTypeChecker();
/**
* Determines if the node has a boolean type. Does recursion for nodes with
* left/right operators.
*/
function isBooleanType(node: TSESTree.Expression): boolean {
if (node.type === AST_NODE_TYPES.LogicalExpression) {
return isBooleanType(node.left) && isBooleanType(node.right);
}
const tsNode = service.esTreeNodeToTSNodeMap.get<ts.ExpressionStatement>(
node,
);
const type = checker.getTypeAtLocation(tsNode);
const typeName = util.getTypeName(checker, type);
return ['true', 'false', 'boolean'].includes(typeName);
}
/**
* Asserts that a node is a boolean, reports otherwise.
*/
function assertNodeIsBoolean(node: TSESTree.Expression): void {
if (!isBooleanType(node)) {
return context.report({ node, messageId: 'strictBooleanExpression' });
}
}
/**
* Asserts that an expression contains a boolean, reports otherwise. Filters
* all LogicalExpressions to prevent some duplicate reports.
*/
function assertExpressionContainsBoolean(node: ExpressionWithTest): void {
if (
node.test !== null &&
node.test.type !== AST_NODE_TYPES.LogicalExpression
) {
assertNodeIsBoolean(node.test);
}
}
return {
ConditionalExpression: assertExpressionContainsBoolean,
DoWhileStatement: assertExpressionContainsBoolean,
ForStatement: assertExpressionContainsBoolean,
IfStatement: assertExpressionContainsBoolean,
WhileStatement: assertExpressionContainsBoolean,
LogicalExpression(node: TSESTree.LogicalExpression) {
assertNodeIsBoolean(node);
},
UnaryExpression(node) {
if (node.operator === '!') {
assertNodeIsBoolean(node.argument);
}
},
};
},
});