Skip to content

fix(eslint-plugin): [no-base-to-string] check array generic type #10437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ enum Usefulness {
Sometimes = 'may',
}

type Options = [
export type Options = [
{
ignoredTypeNames?: string[];
},
];
type MessageIds = 'baseArrayJoin' | 'baseToString';
export type MessageIds = 'baseArrayJoin' | 'baseToString';

export default createRule<Options, MessageIds>({
name: 'no-base-to-string',
Expand Down Expand Up @@ -140,6 +140,28 @@ export default createRule<Options, MessageIds>({
return Usefulness.Never;
}

function collectTupleCertainty(type: ts.TypeReference): Usefulness {
const typeArgs = checker.getTypeArguments(type);
const certainties = typeArgs.map(t => collectToStringCertainty(t));
if (certainties.some(certainty => certainty === Usefulness.Never)) {
return Usefulness.Never;
}

if (certainties.some(certainty => certainty === Usefulness.Sometimes)) {
return Usefulness.Sometimes;
}

return Usefulness.Always;
}

function collectArrayCertainty(type: ts.Type): Usefulness {
const elemType = nullThrows(
type.getNumberIndexType(),
'array should have number index type',
);
return collectToStringCertainty(elemType);
}

function collectJoinCertainty(type: ts.Type): Usefulness {
if (tsutils.isUnionType(type)) {
return collectUnionTypeCertainty(type, collectJoinCertainty);
Expand All @@ -150,25 +172,11 @@ export default createRule<Options, MessageIds>({
}

if (checker.isTupleType(type)) {
const typeArgs = checker.getTypeArguments(type);
const certainties = typeArgs.map(t => collectToStringCertainty(t));
if (certainties.some(certainty => certainty === Usefulness.Never)) {
return Usefulness.Never;
}

if (certainties.some(certainty => certainty === Usefulness.Sometimes)) {
return Usefulness.Sometimes;
}

return Usefulness.Always;
return collectTupleCertainty(type);
}

if (checker.isArrayType(type)) {
const elemType = nullThrows(
type.getNumberIndexType(),
'array should have number index type',
);
return collectToStringCertainty(elemType);
return collectArrayCertainty(type);
}

return Usefulness.Always;
Expand Down Expand Up @@ -205,6 +213,14 @@ export default createRule<Options, MessageIds>({
return collectUnionTypeCertainty(type, collectToStringCertainty);
}

if (checker.isTupleType(type)) {
return collectTupleCertainty(type);
}

if (checker.isArrayType(type)) {
return collectArrayCertainty(type);
}

const toString =
checker.getPropertyOfType(type, 'toString') ??
checker.getPropertyOfType(type, 'toLocaleString');
Expand Down
Loading
Loading