eslint-plugin-th-rules 3.4.1 → 3.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-explicit-nil-check.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-explicit-nil-check.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAWtF,QAAA,MAAM,sBAAsB;;
|
|
1
|
+
{"version":3,"file":"prefer-explicit-nil-check.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-explicit-nil-check.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAWtF,QAAA,MAAM,sBAAsB;;CA0d1B,CAAC;AAEH,eAAe,sBAAsB,CAAC"}
|
|
@@ -107,6 +107,38 @@ const preferExplicitNilCheck = createRule({
|
|
|
107
107
|
}
|
|
108
108
|
return false;
|
|
109
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Returns true when the type is a union that includes boolean-like AND also
|
|
112
|
+
* includes some other non-nullish, non-boolean-like constituent.
|
|
113
|
+
*
|
|
114
|
+
* Example: string | object | null | boolean | number
|
|
115
|
+
*
|
|
116
|
+
* In these cases we avoid rewriting `if (x)` / `if (!x)` because the intent
|
|
117
|
+
* may be genuine boolean gating (or semantic-preserving rewrite isn't clear).
|
|
118
|
+
*/
|
|
119
|
+
function isPartialBooleanUnionByTS(node) {
|
|
120
|
+
const type = getTsType(node);
|
|
121
|
+
if (_.isNil(type))
|
|
122
|
+
return false;
|
|
123
|
+
if (!type.isUnion())
|
|
124
|
+
return false;
|
|
125
|
+
let sawBoolean = false;
|
|
126
|
+
let sawOtherNonNullish = false;
|
|
127
|
+
for (const t of type.types) {
|
|
128
|
+
const flags = t.getFlags();
|
|
129
|
+
if (isNullableFlag(flags))
|
|
130
|
+
continue;
|
|
131
|
+
if (isBooleanLikeFlag(flags)) {
|
|
132
|
+
sawBoolean = true;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
sawOtherNonNullish = true;
|
|
136
|
+
}
|
|
137
|
+
if (sawBoolean && sawOtherNonNullish)
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
110
142
|
/**
|
|
111
143
|
* Returns true iff the expression type is effectively:
|
|
112
144
|
* string | null | undefined
|
|
@@ -222,6 +254,8 @@ const preferExplicitNilCheck = createRule({
|
|
|
222
254
|
return;
|
|
223
255
|
if (isNumberByTS(node))
|
|
224
256
|
return;
|
|
257
|
+
if (isPartialBooleanUnionByTS(node))
|
|
258
|
+
return;
|
|
225
259
|
const text = context.sourceCode.getText(node);
|
|
226
260
|
if (isStringByTS(node)) {
|
|
227
261
|
reportFull(node, `!${LODASH_IDENT}.isEmpty(${text})`);
|
|
@@ -235,6 +269,8 @@ const preferExplicitNilCheck = createRule({
|
|
|
235
269
|
return;
|
|
236
270
|
if (isNumberByTS(arg))
|
|
237
271
|
return;
|
|
272
|
+
if (isPartialBooleanUnionByTS(arg))
|
|
273
|
+
return;
|
|
238
274
|
const text = context.sourceCode.getText(arg);
|
|
239
275
|
if (isStringByTS(arg)) {
|
|
240
276
|
reportFull(node, `${LODASH_IDENT}.isEmpty(${text})`);
|
|
@@ -327,6 +363,8 @@ const preferExplicitNilCheck = createRule({
|
|
|
327
363
|
return;
|
|
328
364
|
if (isBooleanByTS(arg))
|
|
329
365
|
return;
|
|
366
|
+
if (isPartialBooleanUnionByTS(arg))
|
|
367
|
+
return;
|
|
330
368
|
if (isNumberByTS(arg))
|
|
331
369
|
return;
|
|
332
370
|
transformFalsyUnary(node);
|
|
@@ -345,6 +383,8 @@ const preferExplicitNilCheck = createRule({
|
|
|
345
383
|
return;
|
|
346
384
|
if (isBooleanByTS(node))
|
|
347
385
|
return;
|
|
386
|
+
if (isPartialBooleanUnionByTS(node))
|
|
387
|
+
return;
|
|
348
388
|
if (isNumberByTS(node))
|
|
349
389
|
return;
|
|
350
390
|
transformTruthy(node);
|