Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -74,6 +74,7 @@ export default createRule<Options, MessageIds>({ | ||
| } | ||
| const certainty = collectToStringCertainty( | ||
| type ?? services.getTypeAtLocation(node), | ||
| new Set(), | ||
| ); | ||
| if (certainty === Usefulness.Always) { | ||
| return; | ||
| @@ -93,7 +94,7 @@ export default createRule<Options, MessageIds>({ | ||
| node: TSESTree.Node, | ||
| type: ts.Type, | ||
| ): void { | ||
| const certainty = collectJoinCertainty(type, new Set()); | ||
| if (certainty === Usefulness.Always) { | ||
| return; | ||
| @@ -140,9 +141,14 @@ export default createRule<Options, MessageIds>({ | ||
| return Usefulness.Never; | ||
| } | ||
| function collectTupleCertainty( | ||
| type: ts.TypeReference, | ||
| visited: Set<ts.Type>, | ||
| ): Usefulness { | ||
| const typeArgs = checker.getTypeArguments(type); | ||
| const certainties = typeArgs.map(t => | ||
| collectToStringCertainty(t, visited), | ||
| ); | ||
| if (certainties.some(certainty => certainty === Usefulness.Never)) { | ||
| return Usefulness.Never; | ||
| } | ||
| @@ -154,39 +160,57 @@ export default createRule<Options, MessageIds>({ | ||
| return Usefulness.Always; | ||
| } | ||
| function collectArrayCertainty( | ||
| type: ts.Type, | ||
| visited: Set<ts.Type>, | ||
| ): Usefulness { | ||
| const elemType = nullThrows( | ||
| type.getNumberIndexType(), | ||
| 'array should have number index type', | ||
| ); | ||
| return collectToStringCertainty(elemType, visited); | ||
| } | ||
| function collectJoinCertainty( | ||
| type: ts.Type, | ||
| visited: Set<ts.Type>, | ||
| ): Usefulness { | ||
| if (tsutils.isUnionType(type)) { | ||
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return collectUnionTypeCertainty(type, t => | ||
| collectJoinCertainty(t, visited), | ||
| ); | ||
| } | ||
| if (tsutils.isIntersectionType(type)) { | ||
| return collectIntersectionTypeCertainty(type, t => | ||
| collectJoinCertainty(t, visited), | ||
| ); | ||
| } | ||
| if (checker.isTupleType(type)) { | ||
| return collectTupleCertainty(type, visited); | ||
| } | ||
| if (checker.isArrayType(type)) { | ||
| return collectArrayCertainty(type, visited); | ||
| } | ||
| return Usefulness.Always; | ||
| } | ||
| 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, visited); | ||
| } | ||
| // unconstrained generic means `unknown` | ||
| return Usefulness.Always; | ||
| @@ -205,19 +229,23 @@ export default createRule<Options, MessageIds>({ | ||
| } | ||
| if (type.isIntersection()) { | ||
| return collectIntersectionTypeCertainty(type, t => | ||
| collectToStringCertainty(t, visited), | ||
| ); | ||
| } | ||
| if (type.isUnion()) { | ||
| return collectUnionTypeCertainty(type, t => | ||
| collectToStringCertainty(t, visited), | ||
| ); | ||
| } | ||
| if (checker.isTupleType(type)) { | ||
| return collectTupleCertainty(type, new Set([...visited, type])); | ||
| } | ||
| if (checker.isArrayType(type)) { | ||
| return collectArrayCertainty(type, new Set([...visited, type])); | ||
| } | ||
| const toString = | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:[ | ||
| { | ||
| @@ -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', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
Uh oh!
There was an error while loading.Please reload this page.