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 support for catching toLocaleString#10138

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
Show all changes
13 commits
Select commitHold shift + click to select a range
00754ea
feat: add support for toLocaleString method
y-hsgwOct 12, 2024
6ef7066
test: add Tests
y-hsgwOct 12, 2024
3e4bcff
test: fix test
y-hsgwOct 12, 2024
c246f0a
docs: update doc
y-hsgwOct 12, 2024
f37af6a
test: update snapshot and fix description
y-hsgwOct 12, 2024
ebc6947
docs: fix typo
y-hsgwOct 14, 2024
31a7cf4
test: fix typo and remove test
y-hsgwOct 15, 2024
3ffe462
Update packages/eslint-plugin/docs/rules/no-base-to-string.mdx
y-hsgwOct 15, 2024
1c49fd6
Update packages/eslint-plugin/docs/rules/no-base-to-string.mdx
y-hsgwOct 15, 2024
ea11e91
Merge branch 'feature/no-base-to-string-to-locale-string' of https://…
y-hsgwOct 15, 2024
379dba6
no-base-to-string.mdx を更新
y-hsgwOct 15, 2024
b4c7216
Update packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
y-hsgwOct 15, 2024
49de003
Merge branch 'main'
JoshuaKGoldbergOct 21, 2024
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
19 changes: 11 additions & 8 deletionspackages/eslint-plugin/docs/rules/no-base-to-string.mdx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
---
description: 'Require `.toString()` to only be called on objects which provide useful information when stringified.'
description: 'Require `.toString()`and `.toLocaleString()`to only be called on objects which provide useful information when stringified.'
---

import Tabs from '@theme/Tabs';
Expand All@@ -10,10 +10,10 @@ import TabItem from '@theme/TabItem';
> See **https://typescript-eslint.io/rules/no-base-to-string** for documentation.

JavaScript will call `toString()` on an object when it is converted to a string, such as when `+` adding to a string or in `${}` template literals.
The default Object `.toString()`uses the format `"[object Object]"`, which is often not what was intended.
This rule reports on stringified values that aren't primitives and don't define a more useful `.toString()` method.
The default Object `.toString()`and `toLocaleString()` use the format `"[object Object]"`, which is often not what was intended.
This rule reports on stringified values that aren't primitives and don't define a more useful `.toString()`or `toLocaleString()`method.

> Note that `Function` provides its own `.toString()` thatreturns the function's code.
> Note that `Function` provides its own `.toString()`and `toLocaleString()`thatreturn the function's code.
> Functions are not flagged by this rule.

## Examples
Expand All@@ -29,20 +29,22 @@ class MyClass {}
const value = new MyClass();
value + '';

// Interpolation and manual .toString() calls too:
// Interpolation and manual .toString()and `toLocaleString()`calls too:
`Value: ${value}`;
({}).toString();
({}).toLocaleString();
```

</TabItem>
<TabItem value="✅ Correct">

```ts
// These types all have useful .toString()s
// These types all have useful .toString() and `toLocaleString()` methods
'Text' + true;
`Value: ${123}`;
`Arrays too: ${[1, 2, 3]}`;
(() => {}).toString();
(() => {}).toLocaleString();

// Defining a custom .toString class is considered acceptable
class CustomToString {
Expand All@@ -68,8 +70,8 @@ const literalWithToString = {

{/* insert option description */}

This is useful for typeswhose declarationsmissing `toString()`, but are known toactuallyhave `toString()`.
There are some types missing `toString()`in TypeScript, like `RegExp`, `URL`, `URLSearchParams` etc.
This is useful for types missing `toString()` or `toLocaleString()` (butactuallyhas `toString()` or `toLocaleString()`).
There are some types missing `toString()`or `toLocaleString()` in old versions of TypeScript, like `RegExp`, `URL`, `URLSearchParams` etc.

The following patterns are considered correct with the default options `{ ignoredTypeNames: ["RegExp"] }`:

Expand All@@ -94,4 +96,5 @@ If you don't mind a risk of `"[object Object]"` or incorrect type coercions in y
## Further Reading

- [`Object.prototype.toString()` MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString)
- [`Object.prototype.toLocaleString()` MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString)
- [Microsoft/TypeScript Add missing toString declarations for base types that have them](https://github.com/microsoft/TypeScript/issues/38347)
8 changes: 5 additions & 3 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@@ -24,7 +24,7 @@ export default createRule<Options, MessageIds>({
type: 'suggestion',
docs: {
description:
'Require `.toString()` to only be called on objects which provide useful information when stringified',
'Require `.toString()`and `.toLocaleString()`to only be called on objects which provide useful information when stringified',
recommended: 'recommended',
requiresTypeChecking: true,
},
Expand DownExpand Up@@ -82,7 +82,9 @@ export default createRule<Options, MessageIds>({
}

function collectToStringCertainty(type: ts.Type): Usefulness {
const toString = checker.getPropertyOfType(type, 'toString');
const toString =
checker.getPropertyOfType(type, 'toString') ??
checker.getPropertyOfType(type, 'toLocaleString');
const declarations = toString?.getDeclarations();
if (!toString || !declarations || declarations.length === 0) {
return Usefulness.Always;
Expand DownExpand Up@@ -167,7 +169,7 @@ export default createRule<Options, MessageIds>({
checkExpression(node.left, leftType);
}
},
'CallExpression > MemberExpression.callee > Identifier[name ="toString"].property'(
'CallExpression > MemberExpression.callee > Identifier[name =/^(toLocaleString|toString)$/].property'(
node: TSESTree.Expression,
): void {
const memberExpr = node.parent as TSESTree.MemberExpression;
Expand Down
View file
Open in desktop

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

58 changes: 57 additions & 1 deletionpackages/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@@ -63,9 +63,15 @@ ruleTester.run('no-base-to-string', rule, {
`
function someFunction() {}
someFunction.toString();
let text = \`\${someFunction}\`;
`,
`
function someFunction() {}
someFunction.toLocaleString();
let text = \`\${someFunction}\`;
`,
'unknownObject.toString();',
'unknownObject.toLocaleString();',
'unknownObject.someOtherMethod();',
`
class CustomToString {
Expand All@@ -79,14 +85,22 @@ class CustomToString {
const literalWithToString = {
toString: () => 'Hello, world!',
};
'' +literalToString;
'' +literalWithToString;
`,
`
const printer = (inVar: string | number | boolean) => {
inVar.toString();
};
printer('');
printer(1);
printer(true);
`,
`
const printer = (inVar: string | number | boolean) => {
inVar.toLocaleString();
};
printer('');
printer(1);
printer(true);
`,
'let _ = {} * {};',
Expand DownExpand Up@@ -144,6 +158,18 @@ tag\`\${{}}\`;
},
],
},
{
code: '({}).toLocaleString();',
errors: [
{
data: {
certainty: 'will',
name: '{}',
},
messageId: 'baseToString',
},
],
},
{
code: "'' + {};",
errors: [
Expand DownExpand Up@@ -183,6 +209,21 @@ tag\`\${{}}\`;
},
],
},
{
code: `
let someObjectOrString = Math.random() ? { a: true } : 'text';
someObjectOrString.toLocaleString();
`,
errors: [
{
data: {
certainty: 'may',
name: 'someObjectOrString',
},
messageId: 'baseToString',
},
],
},
{
code: `
let someObjectOrString = Math.random() ? { a: true } : 'text';
Expand DownExpand Up@@ -213,6 +254,21 @@ tag\`\${{}}\`;
},
],
},
{
code: `
let someObjectOrObject = Math.random() ? { a: true, b: true } : { a: true };
someObjectOrObject.toLocaleString();
`,
errors: [
{
data: {
certainty: 'will',
name: 'someObjectOrObject',
},
messageId: 'baseToString',
},
],
},
{
code: `
let someObjectOrObject = Math.random() ? { a: true, b: true } : { a: true };
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp