Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5edb99

Browse files
authored
feat(eslint-plugin): [no-base-to-string] add option to ignore tagged templates (typescript-eslint#1763)
1 parent 6b4680b commit f5edb99

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

packages/eslint-plugin/docs/rules/no-base-to-string.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@ const literalWithToString = {
5252
`Value: ${literalWithToString}`;
5353
```
5454

55+
## Options
56+
57+
The rule accepts an options object with the following properties:
58+
59+
```ts
60+
type Options = {
61+
// if true, interpolated expressions in tagged templates will not be checked
62+
ignoreTaggedTemplateExpressions?: boolean;
63+
};
64+
65+
const defaults = {
66+
ignoreTaggedTemplateExpressions: false,
67+
};
68+
```
69+
70+
### `ignoreTaggedTemplateExpressions`
71+
72+
This allows to skip checking tagged templates, for cases where the tags do not necessarily stringify interpolated values.
73+
74+
Examples of additional **correct** code for this rule with `{ ignoreTaggedTemplateExpressions: true }`:
75+
76+
```ts
77+
function tag() {}
78+
tag`${{}}`;
79+
```
80+
5581
## When Not To Use It
5682

5783
If you don't mind `"[object Object]"` in your strings, then you will not need this rule.

packages/eslint-plugin/src/rules/no-base-to-string.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ enum Usefulness {
1212
Sometimes = 'may',
1313
}
1414

15-
export default util.createRule({
15+
type Options = [
16+
{
17+
ignoreTaggedTemplateExpressions?: boolean;
18+
},
19+
];
20+
type MessageIds = 'baseToString';
21+
22+
export default util.createRule<Options, MessageIds>({
1623
name: 'no-base-to-string',
1724
meta: {
1825
docs: {
@@ -26,11 +33,22 @@ export default util.createRule({
2633
baseToString:
2734
"'{{name}} {{certainty}} evaluate to '[object Object]' when stringified.",
2835
},
29-
schema: [],
36+
schema: [
37+
{
38+
type: 'object',
39+
properties: {
40+
ignoreTaggedTemplateExpressions: {
41+
type: 'boolean',
42+
default: false,
43+
},
44+
},
45+
additionalProperties: false,
46+
},
47+
],
3048
type: 'suggestion',
3149
},
32-
defaultOptions: [],
33-
create(context) {
50+
defaultOptions: [{ ignoreTaggedTemplateExpressions: false }],
51+
create(context, [options]) {
3452
const parserServices = util.getParserServices(context);
3553
const typeChecker = parserServices.program.getTypeChecker();
3654

@@ -110,8 +128,14 @@ export default util.createRule({
110128
const memberExpr = node.parent as TSESTree.MemberExpression;
111129
checkExpression(memberExpr.object);
112130
},
113-
114131
TemplateLiteral(node: TSESTree.TemplateLiteral): void {
132+
if (
133+
options.ignoreTaggedTemplateExpressions &&
134+
node.parent &&
135+
node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression
136+
) {
137+
return;
138+
}
115139
for (const expression of node.expressions) {
116140
checkExpression(expression);
117141
}

packages/eslint-plugin/tests/rules/no-base-to-string.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ const literalWithToString = {
7070
'let _ = {} ^ {};',
7171
'let _ = {} << {};',
7272
'let _ = {} >> {};',
73+
{
74+
code: `
75+
function tag() {}
76+
tag\`\${{}}\`;
77+
`,
78+
options: [
79+
{
80+
ignoreTaggedTemplateExpressions: true,
81+
},
82+
],
83+
},
7384
],
7485
invalid: [
7586
{
@@ -84,6 +95,21 @@ const literalWithToString = {
8495
},
8596
],
8697
},
98+
{
99+
code: `
100+
function tag() {}
101+
tag\`\${{}}\`;
102+
`,
103+
errors: [
104+
{
105+
data: {
106+
certainty: 'will',
107+
name: '{}',
108+
},
109+
messageId: 'baseToString',
110+
},
111+
],
112+
},
87113
{
88114
code: '({}.toString());',
89115
errors: [

0 commit comments

Comments
 (0)