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

Merged
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;
String(x);
```

## 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
16 changes: 14 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 {
exporttypeOptions=[
{
ignoredTypeNames?:string[];
checkUnknown?:boolean;
},
];
exporttypeMessageIds='baseArrayJoin'|'baseToString';
Expand All@@ -46,6 +47,10 @@ export default createRule<Options, MessageIds>({
type:'object',
additionalProperties:false,
properties:{
checkUnknown:{
type:'boolean',
description:'Whether to also check values of type `unknown`',
},
ignoredTypeNames:{
type:'array',
description:
Expand All@@ -60,6 +65,7 @@ export default createRule<Options, MessageIds>({
},
defaultOptions:[
{
checkUnknown:false,
ignoredTypeNames:['Error','RegExp','URL','URLSearchParams'],
},
],
Expand All@@ -76,6 +82,7 @@ export default createRule<Options, MessageIds>({
type??services.getTypeAtLocation(node),
newSet(),
);

if(certainty===Usefulness.Always){
return;
}
Expand DownExpand Up@@ -213,7 +220,7 @@ export default createRule<Options, MessageIds>({
returncollectToStringCertainty(constraint,visited);
}
// unconstrained generic means `unknown`
returnUsefulness.Always;
returnoption.checkUnknown ?Usefulness.Sometimes :Usefulness.Always;
}

// the Boolean type definition missing toString()
Expand DownExpand Up@@ -251,8 +258,13 @@ export default createRule<Options, MessageIds>({
consttoString=
checker.getPropertyOfType(type,'toString')??
checker.getPropertyOfType(type,'toLocaleString');

if(!toString){
// e.g. any/unknown
// unknown
if(option.checkUnknown&&type.flags===ts.TypeFlags.Unknown){
returnUsefulness.Sometimes;
}
// e.g. any
returnUsefulness.Always;
}

Expand Down
View file
Open in desktop

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

203 changes: 194 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,202 @@ 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);
}
`,
`
declare const x: any;
\`\${x})\`;
`,
`
declare const x: any;
x.toString();
`,
`
declare const x: any;
x.toLocaleString();
`,
`
declare const x: any;
'' + x;
`,
`
declare const x: any;
String(x);
`,
`
declare const x: any;
'' += 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.


[8]ページ先頭

©2009-2025 Movatter.jp