/
no-unnecessary-boolean-literal-compare.ts
123 lines (108 loc) · 3.15 KB
/
no-unnecessary-boolean-literal-compare.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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
type MessageIds = 'direct' | 'negated';
interface BooleanComparison {
expression: TSESTree.Expression;
forTruthy: boolean;
negated: boolean;
range: [number, number];
}
export default util.createRule<[], MessageIds>({
name: 'no-unnecessary-boolean-literal-compare',
meta: {
docs: {
description:
'Flags unnecessary equality comparisons against boolean literals',
category: 'Stylistic Issues',
recommended: false,
requiresTypeChecking: true,
},
fixable: 'code',
messages: {
direct:
'This expression unnecessarily compares a boolean value to a boolean instead of using it directly',
negated:
'This expression unnecessarily compares a boolean value to a boolean instead of negating it.',
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
function getBooleanComparison(
node: TSESTree.BinaryExpression,
): BooleanComparison | undefined {
const comparison = deconstructComparison(node);
if (!comparison) {
return undefined;
}
const expressionType = checker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(comparison.expression),
);
if (
!tsutils.isTypeFlagSet(
expressionType,
ts.TypeFlags.Boolean | ts.TypeFlags.BooleanLiteral,
)
) {
return undefined;
}
return comparison;
}
function deconstructComparison(
node: TSESTree.BinaryExpression,
): BooleanComparison | undefined {
const comparisonType = util.getEqualsKind(node.operator);
if (!comparisonType) {
return undefined;
}
for (const [against, expression] of [
[node.right, node.left],
[node.left, node.right],
]) {
if (
against.type !== AST_NODE_TYPES.Literal ||
typeof against.value !== 'boolean'
) {
continue;
}
const { value } = against;
const negated = node.operator.startsWith('!');
return {
forTruthy: value ? !negated : negated,
expression,
negated,
range:
expression.range[0] < against.range[0]
? [expression.range[1], against.range[1]]
: [against.range[1], expression.range[1]],
};
}
return undefined;
}
return {
BinaryExpression(node): void {
const comparison = getBooleanComparison(node);
if (comparison) {
context.report({
fix: function*(fixer) {
yield fixer.removeRange(comparison.range);
if (!comparison.forTruthy) {
yield fixer.insertTextBefore(node, '!');
}
},
messageId: comparison.negated ? 'negated' : 'direct',
node,
});
}
},
};
},
});