-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-base-to-string] add checkUnknown Option #11128
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ enum Usefulness { | |||||
export type Options = [ | ||||||
{ | ||||||
ignoredTypeNames?: string[]; | ||||||
checkUnknown?: boolean; | ||||||
}, | ||||||
]; | ||||||
export type MessageIds = 'baseArrayJoin' | 'baseToString'; | ||||||
|
@@ -46,6 +47,12 @@ export default createRule<Options, MessageIds>({ | |||||
type: 'object', | ||||||
additionalProperties: false, | ||||||
properties: { | ||||||
checkUnknown: { | ||||||
type: 'boolean', | ||||||
default: false, | ||||||
description: | ||||||
'Checks the case where toString is applied to unknown type', | ||||||
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. [Docs] A little bit of phrasing alignment:
Suggested change
|
||||||
}, | ||||||
ignoredTypeNames: { | ||||||
type: 'array', | ||||||
description: | ||||||
|
@@ -60,6 +67,7 @@ export default createRule<Options, MessageIds>({ | |||||
}, | ||||||
defaultOptions: [ | ||||||
{ | ||||||
checkUnknown: false, | ||||||
ignoredTypeNames: ['Error', 'RegExp', 'URL', 'URLSearchParams'], | ||||||
}, | ||||||
], | ||||||
|
@@ -76,6 +84,7 @@ export default createRule<Options, MessageIds>({ | |||||
type ?? services.getTypeAtLocation(node), | ||||||
new Set(), | ||||||
); | ||||||
|
||||||
if (certainty === Usefulness.Always) { | ||||||
return; | ||||||
} | ||||||
|
@@ -213,7 +222,7 @@ export default createRule<Options, MessageIds>({ | |||||
return collectToStringCertainty(constraint, visited); | ||||||
} | ||||||
// unconstrained generic means `unknown` | ||||||
return Usefulness.Always; | ||||||
return option.checkUnknown ? Usefulness.Sometimes : Usefulness.Always; | ||||||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
// the Boolean type definition missing toString() | ||||||
|
@@ -251,8 +260,13 @@ export default createRule<Options, MessageIds>({ | |||||
const toString = | ||||||
checker.getPropertyOfType(type, 'toString') ?? | ||||||
checker.getPropertyOfType(type, 'toLocaleString'); | ||||||
|
||||||
if (!toString) { | ||||||
// e.g. any/unknown | ||||||
// unknown | ||||||
if (option.checkUnknown && type.flags === ts.TypeFlags.Unknown) { | ||||||
return Usefulness.Sometimes; | ||||||
} | ||||||
// e.g. any | ||||||
return Usefulness.Always; | ||||||
} | ||||||
|
||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Cleanup] This actually doesn't do anything in our stack (long story...)