Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(eslint-plugin): [no-base-to-string] add option to ignore tagged …
…templates (#1763)
  • Loading branch information
susisu committed Apr 12, 2020
1 parent 6b4680b commit f5edb99
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
26 changes: 26 additions & 0 deletions packages/eslint-plugin/docs/rules/no-base-to-string.md
Expand Up @@ -52,6 +52,32 @@ const literalWithToString = {
`Value: ${literalWithToString}`;
```

## Options

The rule accepts an options object with the following properties:

```ts
type Options = {
// if true, interpolated expressions in tagged templates will not be checked
ignoreTaggedTemplateExpressions?: boolean;
};

const defaults = {
ignoreTaggedTemplateExpressions: false,
};
```

### `ignoreTaggedTemplateExpressions`

This allows to skip checking tagged templates, for cases where the tags do not necessarily stringify interpolated values.

Examples of additional **correct** code for this rule with `{ ignoreTaggedTemplateExpressions: true }`:

```ts
function tag() {}
tag`${{}}`;
```

## When Not To Use It

If you don't mind `"[object Object]"` in your strings, then you will not need this rule.
Expand Down
34 changes: 29 additions & 5 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Expand Up @@ -12,7 +12,14 @@ enum Usefulness {
Sometimes = 'may',
}

export default util.createRule({
type Options = [
{
ignoreTaggedTemplateExpressions?: boolean;
},
];
type MessageIds = 'baseToString';

export default util.createRule<Options, MessageIds>({
name: 'no-base-to-string',
meta: {
docs: {
Expand All @@ -26,11 +33,22 @@ export default util.createRule({
baseToString:
"'{{name}} {{certainty}} evaluate to '[object Object]' when stringified.",
},
schema: [],
schema: [
{
type: 'object',
properties: {
ignoreTaggedTemplateExpressions: {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
defaultOptions: [{ ignoreTaggedTemplateExpressions: false }],
create(context, [options]) {
const parserServices = util.getParserServices(context);
const typeChecker = parserServices.program.getTypeChecker();

Expand Down Expand Up @@ -110,8 +128,14 @@ export default util.createRule({
const memberExpr = node.parent as TSESTree.MemberExpression;
checkExpression(memberExpr.object);
},

TemplateLiteral(node: TSESTree.TemplateLiteral): void {
if (
options.ignoreTaggedTemplateExpressions &&
node.parent &&
node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression
) {
return;
}
for (const expression of node.expressions) {
checkExpression(expression);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Expand Up @@ -70,6 +70,17 @@ const literalWithToString = {
'let _ = {} ^ {};',
'let _ = {} << {};',
'let _ = {} >> {};',
{
code: `
function tag() {}
tag\`\${{}}\`;
`,
options: [
{
ignoreTaggedTemplateExpressions: true,
},
],
},
],
invalid: [
{
Expand All @@ -84,6 +95,21 @@ const literalWithToString = {
},
],
},
{
code: `
function tag() {}
tag\`\${{}}\`;
`,
errors: [
{
data: {
certainty: 'will',
name: '{}',
},
messageId: 'baseToString',
},
],
},
{
code: '({}.toString());',
errors: [
Expand Down

0 comments on commit f5edb99

Please sign in to comment.