Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/src/rules/no-base-to-string.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,12 +18,12 @@ enum Usefulness {
Sometimes = 'may',
}

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

export default createRule<Options, MessageIds>({
name: 'no-base-to-string',
Expand DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp