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] don't crash for recursive array or tuple types#10633

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
60 changes: 44 additions & 16 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@@ -74,6 +74,7 @@ export default createRule<Options, MessageIds>({
}
const certainty = collectToStringCertainty(
type ?? services.getTypeAtLocation(node),
new Set(),
);
if (certainty === Usefulness.Always) {
return;
Expand All@@ -93,7 +94,7 @@ export default createRule<Options, MessageIds>({
node: TSESTree.Node,
type: ts.Type,
): void {
const certainty = collectJoinCertainty(type);
const certainty = collectJoinCertainty(type, new Set());

if (certainty === Usefulness.Always) {
return;
Expand DownExpand Up@@ -140,9 +141,14 @@ export default createRule<Options, MessageIds>({
return Usefulness.Never;
}

function collectTupleCertainty(type: ts.TypeReference): Usefulness {
function collectTupleCertainty(
type: ts.TypeReference,
visited: Set<ts.Type>,
): Usefulness {
const typeArgs = checker.getTypeArguments(type);
const certainties = typeArgs.map(t => collectToStringCertainty(t));
const certainties = typeArgs.map(t =>
collectToStringCertainty(t, visited),
);
if (certainties.some(certainty => certainty === Usefulness.Never)) {
return Usefulness.Never;
}
Expand All@@ -154,39 +160,57 @@ export default createRule<Options, MessageIds>({
return Usefulness.Always;
}

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

function collectJoinCertainty(type: ts.Type): Usefulness {
function collectJoinCertainty(
type: ts.Type,
visited: Set<ts.Type>,
): Usefulness {
if (tsutils.isUnionType(type)) {
return collectUnionTypeCertainty(type, collectJoinCertainty);
return collectUnionTypeCertainty(type, t =>
collectJoinCertainty(t, visited),
);
}

if (tsutils.isIntersectionType(type)) {
return collectIntersectionTypeCertainty(type, collectJoinCertainty);
return collectIntersectionTypeCertainty(type, t =>
collectJoinCertainty(t, visited),
);
}

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

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

return Usefulness.Always;
}

function collectToStringCertainty(type: ts.Type): Usefulness {
function collectToStringCertainty(
type: ts.Type,
visited: Set<ts.Type>,
): Usefulness {
if (visited.has(type)) {
// don't report if this is a self referencing array or tuple type
return Usefulness.Always;
}

if (tsutils.isTypeParameter(type)) {
const constraint = type.getConstraint();
if (constraint) {
return collectToStringCertainty(constraint);
return collectToStringCertainty(constraint, visited);
}
// unconstrained generic means `unknown`
return Usefulness.Always;
Expand All@@ -205,19 +229,23 @@ export default createRule<Options, MessageIds>({
}

if (type.isIntersection()) {
return collectIntersectionTypeCertainty(type, collectToStringCertainty);
return collectIntersectionTypeCertainty(type, t =>
collectToStringCertainty(t, visited),
);
}

if (type.isUnion()) {
return collectUnionTypeCertainty(type, collectToStringCertainty);
return collectUnionTypeCertainty(type, t =>
collectToStringCertainty(t, visited),
);
}

if (checker.isTupleType(type)) {
return collectTupleCertainty(type);
return collectTupleCertainty(type, new Set([...visited, type]));
}

if (checker.isArrayType(type)) {
return collectArrayCertainty(type);
return collectArrayCertainty(type, new Set([...visited, type]));
}

const toString =
Expand Down
94 changes: 94 additions & 0 deletionspackages/eslint-plugin/tests/rules/no-base-to-string.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -483,6 +483,34 @@ function foo<T>(x: T) {
declare const u: unknown;
String(u);
`,
`
type Value = string | Value[];
declare const v: Value;
String(v);
`,
`
type Value = (string | Value)[];
declare const v: Value;
String(v);
`,
`
type Value = Value[];
declare const v: Value;
String(v);
Comment on lines +499 to +502
Copy link
MemberAuthor

@ronamironamiJan 8, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

To my understanding, types like this are valid to stringify.

kirkwaiblinger and JoshuaKGoldberg reacted with thumbs up emoji
`,
`
type Value = [Value];
declare const v: Value;
String(v);
`,
`
declare const v: ('foo' | 'bar')[][];
String(v);
`,
],
invalid:[
{
Expand DownExpand Up@@ -1811,5 +1839,71 @@ foo.toString();
},
],
},
{
code:`
type Value = { foo: string } | Value[];
declare const v: Value;
String(v);
`,
errors:[
{
data:{
certainty:'may',
name:'v',
},
messageId:'baseToString',
},
],
},
{
code:`
type Value = ({ foo: string } | Value)[];
declare const v: Value;
String(v);
`,
errors:[
{
data:{
certainty:'may',
name:'v',
},
messageId:'baseToString',
},
],
},
{
code:`
type Value = [{ foo: string }, Value];
declare const v: Value;
String(v);
`,
errors:[
{
data:{
certainty:'will',
name:'v',
},
messageId:'baseToString',
},
],
},
{
code:`
declare const v: { foo: string }[][];
v.join();
`,
errors:[
{
data:{
certainty:'will',
name:'v',
},
messageId:'baseArrayJoin',
},
],
},
],
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp