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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
developer-bandi wants to merge3 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromdeveloper-bandi:feat/no-base-to-string
Open
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
11 changes: 11 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-base-to-string.mdx
View file
Open in desktop
Original file line numberDiff line numberDiff 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 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@@ -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,

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...)

Suggested change
default: false,

description:
'Checks the case where toString is applied to unknown type',

Choose a reason for hiding this comment

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

[Docs] A little bit of phrasing alignment:

Suggested change
'Checks the case where toString is applied tounknown type',
'Whether to also check values of type `unknown`',

},
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 DownExpand Up@@ -213,7 +222,7 @@ export default createRule<Options, MessageIds>({
return collectToStringCertainty(constraint, visited);
}
// unconstrained generic means `unknown`
return Usefulness.Always;
returnoption.checkUnknown ? Usefulness.Sometimes :Usefulness.Always;
}

// the Boolean type definition missing toString()
Expand DownExpand 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
View file
Open in desktop

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

179 changes: 170 additions & 9 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@@ -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 DownExpand 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
View file
Open in desktop

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

Loading

[8]ページ先頭

©2009-2025 Movatter.jp