UNPKG

10.4 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const utils_1 = require("@typescript-eslint/utils");
23const tsutils = __importStar(require("tsutils"));
24const ts = __importStar(require("typescript"));
25const util = __importStar(require("../util"));
26exports.default = util.createRule({
27 name: 'no-unnecessary-boolean-literal-compare',
28 meta: {
29 docs: {
30 description: 'Flags unnecessary equality comparisons against boolean literals',
31 recommended: false,
32 requiresTypeChecking: true,
33 },
34 fixable: 'code',
35 messages: {
36 direct: 'This expression unnecessarily compares a boolean value to a boolean instead of using it directly.',
37 negated: 'This expression unnecessarily compares a boolean value to a boolean instead of negating it.',
38 comparingNullableToTrueDirect: 'This expression unnecessarily compares a nullable boolean value to true instead of using it directly.',
39 comparingNullableToTrueNegated: 'This expression unnecessarily compares a nullable boolean value to true instead of negating it.',
40 comparingNullableToFalse: 'This expression unnecessarily compares a nullable boolean value to false instead of using the ?? operator to provide a default.',
41 },
42 schema: [
43 {
44 type: 'object',
45 properties: {
46 allowComparingNullableBooleansToTrue: {
47 type: 'boolean',
48 },
49 allowComparingNullableBooleansToFalse: {
50 type: 'boolean',
51 },
52 },
53 additionalProperties: false,
54 },
55 ],
56 type: 'suggestion',
57 },
58 defaultOptions: [
59 {
60 allowComparingNullableBooleansToTrue: true,
61 allowComparingNullableBooleansToFalse: true,
62 },
63 ],
64 create(context, [options]) {
65 const parserServices = util.getParserServices(context);
66 const checker = parserServices.program.getTypeChecker();
67 function getBooleanComparison(node) {
68 const comparison = deconstructComparison(node);
69 if (!comparison) {
70 return undefined;
71 }
72 const expressionType = checker.getTypeAtLocation(parserServices.esTreeNodeToTSNodeMap.get(comparison.expression));
73 if (isBooleanType(expressionType)) {
74 return Object.assign(Object.assign({}, comparison), { expressionIsNullableBoolean: false });
75 }
76 if (isNullableBoolean(expressionType)) {
77 return Object.assign(Object.assign({}, comparison), { expressionIsNullableBoolean: true });
78 }
79 return undefined;
80 }
81 function isBooleanType(expressionType) {
82 return tsutils.isTypeFlagSet(expressionType, ts.TypeFlags.Boolean | ts.TypeFlags.BooleanLiteral);
83 }
84 /**
85 * checks if the expressionType is a union that
86 * 1) contains at least one nullish type (null or undefined)
87 * 2) contains at least once boolean type (true or false or boolean)
88 * 3) does not contain any types besides nullish and boolean types
89 */
90 function isNullableBoolean(expressionType) {
91 if (!expressionType.isUnion()) {
92 return false;
93 }
94 const { types } = expressionType;
95 const nonNullishTypes = types.filter(type => !tsutils.isTypeFlagSet(type, ts.TypeFlags.Undefined | ts.TypeFlags.Null));
96 const hasNonNullishType = nonNullishTypes.length > 0;
97 if (!hasNonNullishType) {
98 return false;
99 }
100 const hasNullableType = nonNullishTypes.length < types.length;
101 if (!hasNullableType) {
102 return false;
103 }
104 const allNonNullishTypesAreBoolean = nonNullishTypes.every(isBooleanType);
105 if (!allNonNullishTypesAreBoolean) {
106 return false;
107 }
108 return true;
109 }
110 function deconstructComparison(node) {
111 const comparisonType = getEqualsKind(node.operator);
112 if (!comparisonType) {
113 return undefined;
114 }
115 for (const [against, expression] of [
116 [node.right, node.left],
117 [node.left, node.right],
118 ]) {
119 if (against.type !== utils_1.AST_NODE_TYPES.Literal ||
120 typeof against.value !== 'boolean') {
121 continue;
122 }
123 const { value: literalBooleanInComparison } = against;
124 const negated = !comparisonType.isPositive;
125 return {
126 literalBooleanInComparison,
127 forTruthy: literalBooleanInComparison ? !negated : negated,
128 expression,
129 negated,
130 range: expression.range[0] < against.range[0]
131 ? [expression.range[1], against.range[1]]
132 : [against.range[0], expression.range[0]],
133 };
134 }
135 return undefined;
136 }
137 function nodeIsUnaryNegation(node) {
138 return (node.type === utils_1.AST_NODE_TYPES.UnaryExpression &&
139 node.prefix &&
140 node.operator === '!');
141 }
142 return {
143 BinaryExpression(node) {
144 const comparison = getBooleanComparison(node);
145 if (comparison === undefined) {
146 return;
147 }
148 if (comparison.expressionIsNullableBoolean) {
149 if (comparison.literalBooleanInComparison &&
150 options.allowComparingNullableBooleansToTrue) {
151 return;
152 }
153 if (!comparison.literalBooleanInComparison &&
154 options.allowComparingNullableBooleansToFalse) {
155 return;
156 }
157 }
158 context.report({
159 fix: function* (fixer) {
160 yield fixer.removeRange(comparison.range);
161 // if the expression `exp` isn't nullable, or we're comparing to `true`,
162 // we can just replace the entire comparison with `exp` or `!exp`
163 if (!comparison.expressionIsNullableBoolean ||
164 comparison.literalBooleanInComparison) {
165 if (!comparison.forTruthy) {
166 yield fixer.insertTextBefore(node, '!');
167 }
168 return;
169 }
170 // if we're here, then the expression is a nullable boolean and we're
171 // comparing to a literal `false`
172 // if we're doing `== false` or `=== false`, then we need to negate the expression
173 if (!comparison.negated) {
174 const { parent } = node;
175 // if the parent is a negation, we can instead just get rid of the parent's negation.
176 // i.e. instead of resulting in `!(!(exp))`, we can just result in `exp`
177 if (parent != null && nodeIsUnaryNegation(parent)) {
178 // remove from the beginning of the parent to the beginning of this node
179 yield fixer.removeRange([parent.range[0], node.range[0]]);
180 // remove from the end of the node to the end of the parent
181 yield fixer.removeRange([node.range[1], parent.range[1]]);
182 }
183 else {
184 yield fixer.insertTextBefore(node, '!');
185 }
186 }
187 // provide the default `true`
188 yield fixer.insertTextBefore(node, '(');
189 yield fixer.insertTextAfter(node, ' ?? true)');
190 },
191 messageId: comparison.expressionIsNullableBoolean
192 ? comparison.literalBooleanInComparison
193 ? comparison.negated
194 ? 'comparingNullableToTrueNegated'
195 : 'comparingNullableToTrueDirect'
196 : 'comparingNullableToFalse'
197 : comparison.negated
198 ? 'negated'
199 : 'direct',
200 node,
201 });
202 },
203 };
204 },
205});
206function getEqualsKind(operator) {
207 switch (operator) {
208 case '==':
209 return {
210 isPositive: true,
211 isStrict: false,
212 };
213 case '===':
214 return {
215 isPositive: true,
216 isStrict: true,
217 };
218 case '!=':
219 return {
220 isPositive: false,
221 isStrict: false,
222 };
223 case '!==':
224 return {
225 isPositive: false,
226 isStrict: true,
227 };
228 default:
229 return undefined;
230 }
231}
232//# sourceMappingURL=no-unnecessary-boolean-literal-compare.js.map
\No newline at end of file