Skip to content

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 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
60 changes: 44 additions & 16 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Original file line number Diff line number Diff 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 Down Expand 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 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Original file line number Diff line number Diff 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
Member Author

@ronami ronami Jan 8, 2025

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.

`,
`
type Value = [Value];
declare const v: Value;

String(v);
`,
`
declare const v: ('foo' | 'bar')[][];
String(v);
`,
],
invalid: [
{
Expand Down Expand 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