8000 feat(eslint-plugin): [no-base-to-string] add checkUnknown Option by developer-bandi · Pull Request #11128 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions packages/eslint-plugin/docs/rules/no-base-to-string.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ let text = `${value}`;
String(/regex/);
```

### `checkUnknown`

{/* insert option description */}

The following patterns are considered incorrect with the options `{ checkUnknown: true }`:

```ts option='{ "checkUnknown": true }' showPlaygroundButton
declare const x: unknown;
x.toString();
```

## When Not To Use It

If you don't mind a risk of `"[object Object]"` or incorrect type coercions in your values, then you will not need this rule.
Expand Down
18 changes: 16 additions & 2 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum Usefulness {
export type Options = [
{
ignoredTypeNames?: string[];
checkUnknown?: boolean;
},
];
export type MessageIds = 'baseArrayJoin' | 'baseToString';
Expand All @@ -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',
},
ignoredTypeNames: {
type: 'array',
description:
Expand All @@ -60,6 +67,7 @@ export default createRule<Options, MessageIds>({
},
defaultOptions: [
{
checkUnknown: false,
ignoredTypeNames: ['Error', 'RegExp', 'URL', 'URLSearchParams'],
},
],
Expand All @@ -76,6 +84,7 @@ export default createRule<Options, MessageIds>({
type ?? services.getTypeAtLocation(node),
new Set(),
);

if (certainty === Usefulness.Always) {
return;
}
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

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

One thing I'm curious about is, is it correct to treat generic types as unknown?

👍 agreed, keeping to the "unconstrained generic means unknown"

}

// the Boolean type definition missing toString()
Expand Down Expand Up @@ -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;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

179 changes: 170 additions & 9 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,6 @@ declare const bb: ExtendedGuildChannel;
bb.toString();
`,
`
function foo<T>(x: T) {
String(x);
}
`,
`
declare const u: unknown;
String(u);
`,
`
type Value = string | Value[];
declare const v: Value;

Expand Down Expand Up @@ -511,8 +502,178 @@ String(v);
declare const v: ('foo' | 'bar')[][];
String(v);
`,
`
declare const x: unknown;
\`\${x})\`;
`,
`
declare const x: unknown;
x.toString();
`,
`
declare const x: unknown;
x.toLocaleString();
`,
`
declare const x: unknown;
'' + x;
`,
`
declare const x: unknown;
String(x);
`,
`
declare const x: unknown;
'' += x;
`,
`
function foo<T>(x: T) {
String(x);
}
`,
],
invalid: [
{
code: `
declare const x: unknown;
\`\${x})\`;
`,
errors: [
{
data: {
certainty: 'may',
name: 'x',
},
messageId: 'baseToString',
},
],
options: [
{
checkUnknown: true,
},
],
},
{
code: `
declare const x: unknown;
x.toString();
`,
errors: [
{
data: {
certainty: 'may',
name: 'x',
},
messageId: 'baseToString',
},
],
options: [
{
checkUnknown: true,
},
],
},
{
code: `
declare const x: unknown;
x.toLocaleString();
`,
errors: [
{
data: {
certainty: 'may',
name: 'x',
},
messageId: 'baseToString',
},
],
options: [
{
checkUnknown: true,
},
],
},
{
code: `
declare const x: unknown;
'' + x;
`,
errors: [
{
data: {
certainty: 'may',
name: 'x',
},
messageId: 'baseToString',
},
],
options: [
{
checkUnknown: true,
},
],
},
{
code: `
declare const x: unknown;
String(x);
`,
errors: [
{
data: {
certainty: 'may',
name: 'x',
},
messageId: 'baseToString',
},
],
options: [
{
checkUnknown: true,
},
],
},
{
code: `
declare const x: unknown;
'' += x;
`,
errors: [
{
data: {
certainty: 'may',
name: 'x',
},
messageId: 'baseToString',
},
],
options: [
{
checkUnknown: true,
},
],
},
{
code: `
function foo<T>(x: T) {
String(x);
}
`,
errors: [
{
data: {
certainty: 'may',
name: 'x',
},
messageId: 'baseToString',
},
],
options: [
{
checkUnknown: true,
},
],
},
{
code: '`${{}})`;',
errors: [
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0