Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c27399b

Browse files
authored
fix(eslint-plugin): [no-base-to-string] check array generic type (typescript-eslint#10437)
* initial implementation * add missing test * undo unrelated change * also cover [].toString() * refactor * respect the ignoredTypeNames option * update existing tests to use a helper for checking arrays, and change faulty tests to not check against the {} type * don't use a test helper for creating tests dynamically * remove unnecessary check
1 parent e19f30f commit c27399b

File tree

2 files changed

+1067
-44
lines changed

2 files changed

+1067
-44
lines changed

packages/eslint-plugin/src/rules/no-base-to-string.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ enum Usefulness {
1818
Sometimes = 'may',
1919
}
2020

21-
type Options = [
21+
export type Options = [
2222
{
2323
ignoredTypeNames?: string[];
2424
},
2525
];
26-
type MessageIds = 'baseArrayJoin' | 'baseToString';
26+
export type MessageIds = 'baseArrayJoin' | 'baseToString';
2727

2828
export default createRule<Options, MessageIds>({
2929
name: 'no-base-to-string',
@@ -140,6 +140,28 @@ export default createRule<Options, MessageIds>({
140140
return Usefulness.Never;
141141
}
142142

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+
143165
function collectJoinCertainty(type: ts.Type): Usefulness {
144166
if (tsutils.isUnionType(type)) {
145167
return collectUnionTypeCertainty(type, collectJoinCertainty);
@@ -150,25 +172,11 @@ export default createRule<Options, MessageIds>({
150172
}
151173

152174
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);
164176
}
165177

166178
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);
172180
}
173181

174182
return Usefulness.Always;
@@ -205,6 +213,14 @@ export default createRule<Options, MessageIds>({
205213
return collectUnionTypeCertainty(type, collectToStringCertainty);
206214
}
207215

216+
if (checker.isTupleType(type)) {
217+
return collectTupleCertainty(type);
218+
}
219+
220+
if (checker.isArrayType(type)) {
221+
return collectArrayCertainty(type);
222+
}
223+
208224
const toString =
209225
checker.getPropertyOfType(type, 'toString') ??
210226
checker.getPropertyOfType(type, 'toLocaleString');

0 commit comments

Comments
 (0)