@@ -18,12 +18,12 @@ enum Usefulness {
18
18
Sometimes = 'may' ,
19
19
}
20
20
21
- type Options = [
21
+ export type Options = [
22
22
{
23
23
ignoredTypeNames ?: string [ ] ;
24
24
} ,
25
25
] ;
26
- type MessageIds = 'baseArrayJoin' | 'baseToString' ;
26
+ export type MessageIds = 'baseArrayJoin' | 'baseToString' ;
27
27
28
28
export default createRule < Options , MessageIds > ( {
29
29
name : 'no-base-to-string' ,
@@ -140,6 +140,28 @@ export default createRule<Options, MessageIds>({
140
140
return Usefulness . Never ;
141
141
}
142
142
143
+ function collectTupleCertainty ( type : ts . TypeReference ) : Usefulness {
144
+ const typeArgs = checker . getTypeArguments ( type ) ;
145
+ const certainties = typeArgs . map ( t => collectToStringCertainty ( t ) ) ;
146
+ if ( certainties . some ( certainty => certainty === Usefulness . Never ) ) {
147
+ return Usefulness . Never ;
148
+ }
149
+
150
+ if ( certainties . some ( certainty => certainty === Usefulness . Sometimes ) ) {
151
+ return Usefulness . Sometimes ;
152
+ }
153
+
154
+ return Usefulness . Always ;
155
+ }
156
+
157
+ function collectArrayCertainty ( type : ts . Type ) : Usefulness {
158
+ const elemType = nullThrows (
159
+ type . getNumberIndexType ( ) ,
160
+ 'array should have number index type' ,
161
+ ) ;
162
+ return collectToStringCertainty ( elemType ) ;
163
+ }
164
+
143
165
function collectJoinCertainty ( type : ts . Type ) : Usefulness {
144
166
if ( tsutils . isUnionType ( type ) ) {
145
167
return collectUnionTypeCertainty ( type , collectJoinCertainty ) ;
@@ -150,25 +172,11 @@ export default createRule<Options, MessageIds>({
150
172
}
151
173
152
174
if ( checker . isTupleType ( type ) ) {
153
- const typeArgs = checker . getTypeArguments ( type ) ;
154
- const certainties = typeArgs . map ( t => collectToStringCertainty ( t ) ) ;
155
- if ( certainties . some ( certainty => certainty === Usefulness . Never ) ) {
156
- return Usefulness . Never ;
157
- }
158
-
159
- if ( certainties . some ( certainty => certainty === Usefulness . Sometimes ) ) {
160
- return Usefulness . Sometimes ;
161
- }
162
-
163
- return Usefulness . Always ;
175
+ return collectTupleCertainty ( type ) ;
164
176
}
165
177
166
178
if ( checker . isArrayType ( type ) ) {
167
- const elemType = nullThrows (
168
- type . getNumberIndexType ( ) ,
169
- 'array should have number index type' ,
170
- ) ;
171
- return collectToStringCertainty ( elemType ) ;
179
+ return collectArrayCertainty ( type ) ;
172
180
}
173
181
174
182
return Usefulness . Always ;
@@ -205,6 +213,14 @@ export default createRule<Options, MessageIds>({
205
213
return collectUnionTypeCertainty ( type , collectToStringCertainty ) ;
206
214
}
207
215
216
+ if ( checker . isTupleType ( type ) ) {
217
+ return collectTupleCertainty ( type ) ;
218
+ }
219
+
220
+ if ( checker . isArrayType ( type ) ) {
221
+ return collectArrayCertainty ( type ) ;
222
+ }
223
+
208
224
const toString =
209
225
checker . getPropertyOfType ( type , 'toString' ) ??
210
226
checker . getPropertyOfType ( type , 'toLocaleString' ) ;
0 commit comments