forked from typescript-eslint/typescript-eslint
/
no-unnecessary-condition.ts
192 lines (178 loc) · 5.58 KB
/
no-unnecessary-condition.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import ts, { TypeFlags } from 'typescript';
import {
isTypeFlagSet,
unionTypeParts,
isFalsyType,
isBooleanLiteralType,
isLiteralType,
} from 'tsutils';
import {
createRule,
getParserServices,
getConstrainedTypeAtLocation,
} from '../util';
// Truthiness utilities
// #region
const isTruthyLiteral = (type: ts.Type): boolean =>
isBooleanLiteralType(type, true) || (isLiteralType(type) && !!type.value);
const isPossiblyFalsy = (type: ts.Type): boolean =>
unionTypeParts(type)
// PossiblyFalsy flag includes literal values, so exclude ones that
// are definitely truthy
.filter(t => !isTruthyLiteral(t))
.some(type => isTypeFlagSet(type, ts.TypeFlags.PossiblyFalsy));
const isPossiblyTruthy = (type: ts.Type): boolean =>
unionTypeParts(type).some(type => !isFalsyType(type));
// isLiteralType only covers numbers and strings, this is a more exhaustive check.
const isLiteral = (type: ts.Type): boolean =>
isBooleanLiteralType(type, true) ||
isBooleanLiteralType(type, false) ||
type.flags === ts.TypeFlags.Undefined ||
type.flags === ts.TypeFlags.Null ||
type.flags === ts.TypeFlags.Void ||
isLiteralType(type);
// #endregion
type ExpressionWithTest =
| TSESTree.ConditionalExpression
| TSESTree.DoWhileStatement
| TSESTree.ForStatement
| TSESTree.IfStatement
| TSESTree.WhileStatement;
export type Options = [
{
ignoreRhs?: boolean;
},
];
export type MessageId =
| 'alwaysTruthy'
| 'alwaysFalsy'
| 'literalBooleanExpression'
| 'never';
export default createRule<Options, MessageId>({
name: 'no-unnecessary-conditionals',
meta: {
type: 'suggestion',
docs: {
description:
'Prevents conditionals where the type is always truthy or always falsy',
category: 'Best Practices',
recommended: false,
requiresTypeChecking: true,
},
schema: [
{
type: 'object',
properties: {
ignoreRhs: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
alwaysTruthy: 'Unnecessary conditional, value is always truthy.',
alwaysFalsy: 'Unnecessary conditional, value is always falsy.',
literalBooleanExpression:
'Unnecessary conditional, both sides of the expression are literal values',
never: 'Unnecessary conditional, value is `never`',
},
},
defaultOptions: [
{
ignoreRhs: false,
},
],
create(context, [{ ignoreRhs }]) {
const service = getParserServices(context);
const checker = service.program.getTypeChecker();
function getNodeType(node: TSESTree.Node): ts.Type {
const tsNode = service.esTreeNodeToTSNodeMap.get(node);
return getConstrainedTypeAtLocation(checker, tsNode);
}
/**
* Checks if a conditional node is necessary:
* if the type of the node is always true or always false, it's not necessary.
*/
function checkNode(node: TSESTree.Node): void {
const type = getNodeType(node);
// Conditional is always necessary if it involves `any` or `unknown`
if (isTypeFlagSet(type, TypeFlags.Any | TypeFlags.Unknown)) {
return;
}
if (isTypeFlagSet(type, TypeFlags.Never)) {
context.report({ node, messageId: 'never' });
} else if (!isPossiblyTruthy(type)) {
context.report({ node, messageId: 'alwaysFalsy' });
} else if (!isPossiblyFalsy(type)) {
context.report({ node, messageId: 'alwaysTruthy' });
}
}
/**
* Checks that a binary expression is necessarily conditional, reports otherwise.
* If both sides of the binary expression are literal values, it's not a necessary condition.
*
* NOTE: It's also unnecessary if the types that don't overlap at all
* but that case is handled by the Typescript compiler itself.
*/
const BOOL_OPERATORS = new Set([
'<',
'>',
'<=',
'>=',
'==',
'===',
'!=',
'!==',
]);
function checkIfBinaryExpressionIsNecessaryConditional(
node: TSESTree.BinaryExpression,
): void {
if (
BOOL_OPERATORS.has(node.operator) &&
isLiteral(getNodeType(node.left)) &&
isLiteral(getNodeType(node.right))
) {
context.report({ node, messageId: 'literalBooleanExpression' });
}
}
/**
* Checks that a testable expression is necessarily conditional, reports otherwise.
* Filters all LogicalExpressions to prevent some duplicate reports.
*/
function checkIfTestExpressionIsNecessaryConditional(
node: ExpressionWithTest,
): void {
if (
node.test !== null &&
node.test.type !== AST_NODE_TYPES.LogicalExpression
) {
checkNode(node.test);
}
}
/**
* Checks that a logical expression contains a boolean, reports otherwise.
*/
function checkLogicalExpressionForUnnecessaryConditionals(
node: TSESTree.LogicalExpression,
): void {
checkNode(node.left);
if (!ignoreRhs) {
checkNode(node.right);
}
}
return {
BinaryExpression: checkIfBinaryExpressionIsNecessaryConditional,
ConditionalExpression: checkIfTestExpressionIsNecessaryConditional,
DoWhileStatement: checkIfTestExpressionIsNecessaryConditional,
ForStatement: checkIfTestExpressionIsNecessaryConditional,
IfStatement: checkIfTestExpressionIsNecessaryConditional,
WhileStatement: checkIfTestExpressionIsNecessaryConditional,
LogicalExpression: checkLogicalExpressionForUnnecessaryConditionals,
};
},
});