eslint-plugin-th-rules 3.3.1 → 3.3.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;;CA4X1B,CAAC;AAEH,eAAe,sBAAsB,CAAC"}
|
|
@@ -57,23 +57,25 @@ const preferExplicitNilCheck = createRule({
|
|
|
57
57
|
return (flags & ts.TypeFlags.Null) !== 0 || (flags & ts.TypeFlags.Undefined) !== 0;
|
|
58
58
|
}
|
|
59
59
|
function isStringLikeFlag(flags) {
|
|
60
|
-
return (flags & ts.TypeFlags.
|
|
60
|
+
return (flags & ts.TypeFlags.StringLike) !== 0;
|
|
61
61
|
}
|
|
62
62
|
function isNumberLikeFlag(flags) {
|
|
63
|
-
return (flags & ts.TypeFlags.
|
|
63
|
+
return (flags & ts.TypeFlags.NumberLike) !== 0;
|
|
64
|
+
}
|
|
65
|
+
function isBooleanLikeFlag(flags) {
|
|
66
|
+
return (flags & ts.TypeFlags.BooleanLike) !== 0;
|
|
64
67
|
}
|
|
65
68
|
/**
|
|
66
69
|
* Returns true iff the expression type is effectively:
|
|
67
70
|
* string | null | undefined
|
|
68
|
-
* (i.e., all non-nullish constituents are string/string-
|
|
71
|
+
* (i.e., all non-nullish constituents are string/string-like).
|
|
69
72
|
*/
|
|
70
73
|
function isStringByTS(node) {
|
|
71
74
|
const type = getTsType(node);
|
|
72
75
|
if (_.isNil(type))
|
|
73
76
|
return false;
|
|
74
77
|
if (!type.isUnion()) {
|
|
75
|
-
|
|
76
|
-
return isStringLikeFlag(flags);
|
|
78
|
+
return isStringLikeFlag(type.getFlags());
|
|
77
79
|
}
|
|
78
80
|
let sawNonNullish = false;
|
|
79
81
|
for (const t of type.types) {
|
|
@@ -98,8 +100,7 @@ const preferExplicitNilCheck = createRule({
|
|
|
98
100
|
if (_.isNil(type))
|
|
99
101
|
return false;
|
|
100
102
|
if (!type.isUnion()) {
|
|
101
|
-
|
|
102
|
-
return isNumberLikeFlag(flags);
|
|
103
|
+
return isNumberLikeFlag(type.getFlags());
|
|
103
104
|
}
|
|
104
105
|
let sawNonNullish = false;
|
|
105
106
|
for (const t of type.types) {
|
|
@@ -112,12 +113,33 @@ const preferExplicitNilCheck = createRule({
|
|
|
112
113
|
}
|
|
113
114
|
return sawNonNullish;
|
|
114
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Returns true iff the expression type is effectively:
|
|
118
|
+
* boolean | null | undefined
|
|
119
|
+
* (i.e., all non-nullish constituents are boolean/boolean-literal).
|
|
120
|
+
*
|
|
121
|
+
* This ensures we do NOT rewrite checks like:
|
|
122
|
+
* - boolean | undefined
|
|
123
|
+
* - boolean | null
|
|
124
|
+
* - boolean | null | undefined
|
|
125
|
+
*/
|
|
115
126
|
function isBooleanByTS(node) {
|
|
116
127
|
const type = getTsType(node);
|
|
117
128
|
if (_.isNil(type))
|
|
118
129
|
return false;
|
|
119
|
-
|
|
120
|
-
|
|
130
|
+
if (!type.isUnion()) {
|
|
131
|
+
return isBooleanLikeFlag(type.getFlags());
|
|
132
|
+
}
|
|
133
|
+
let sawNonNullish = false;
|
|
134
|
+
for (const t of type.types) {
|
|
135
|
+
const flags = t.getFlags();
|
|
136
|
+
if (isNullableFlag(flags))
|
|
137
|
+
continue;
|
|
138
|
+
sawNonNullish = true;
|
|
139
|
+
if (!isBooleanLikeFlag(flags))
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
return sawNonNullish;
|
|
121
143
|
}
|
|
122
144
|
function isAlreadyExplicitCheck(node) {
|
|
123
145
|
return (node.type === AST_NODE_TYPES.CallExpression &&
|