Skip to content

Commit

Permalink
Update: Add templateString option in no-implicit-coercion (fixes #12866)
Browse files Browse the repository at this point in the history
This adds the `templateString` option to `no-implicit-coercion`. This makes the
rule report the following code:

```js
`${foo}`
```

For backwards compatibility, this was added as a separate option instead of as
default behaviour.
  • Loading branch information
remcohaszing committed Aug 17, 2020
1 parent 0d90508 commit 6f9144d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
27 changes: 23 additions & 4 deletions docs/rules/no-implicit-coercion.md
Expand Up @@ -33,10 +33,11 @@ This rule is aimed to flag shorter notations for the type conversion, then sugge

This rule has three main options and one override option to allow some coercions as required.

* `"boolean"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `boolean` type.
* `"number"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `number` type.
* `"string"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `string` type.
* `"allow"` (`empty` by default) - Each entry in this array can be one of `~`, `!!`, `+` or `*` that are to be allowed.
- `"boolean"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `boolean` type.
- `"number"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `number` type.
- `"string"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `string` type.
- `"templateString"` (`false` by default) - When this is `true`, this rule warns shorter type conversions using template strings.
- `"allow"` (`empty` by default) - Each entry in this array can be one of `~`, `!!`, `+` or `*` that are to be allowed.

Note that operator `+` in `allow` list would allow `+foo` (number coercion) as well as `"" + foo` (string coercion).

Expand Down Expand Up @@ -106,6 +107,24 @@ var s = String(foo);
foo = String(foo);
```

### templateString

Examples of **incorrect** code for the default `{ "templateString": true }` option:

```js
/*eslint no-implicit-coercion: "error"*/

`${foo}`;
```

Examples of **correct** code for the default `{ "templateString": false }` option:

```js
/*eslint no-implicit-coercion: "error"*/

`${foo}`;
```

### allow

Using `allow` list, we can override and allow specific operators.
Expand Down
32 changes: 32 additions & 0 deletions lib/rules/no-implicit-coercion.js
Expand Up @@ -24,6 +24,7 @@ function parseOptions(options) {
boolean: "boolean" in options ? options.boolean : true,
number: "number" in options ? options.number : true,
string: "string" in options ? options.string : true,
templateString: "templateString" in options ? options.templateString : false,
allow: options.allow || []
};
}
Expand Down Expand Up @@ -180,6 +181,10 @@ module.exports = {
type: "boolean",
default: true
},
templateString: {
type: "boolean",
default: false
},
allow: {
type: "array",
items: {
Expand Down Expand Up @@ -299,6 +304,33 @@ module.exports = {

report(node, recommendation, true);
}
},

TemplateLiteral(node) {
if (!options.templateString) {
return;
}

// `` or `${foo}${bar}`
if (node.expressions.length !== 1) {
return;
}


// `prefix${foo}`
if (node.quasis[0].value.raw !== "") {
return;
}

// `${foo}postfix`
if (node.quasis[1].value.raw !== "") {
return;
}

const code = sourceCode.getText(node.expressions[0]);
const recommendation = `String(${code})`;

report(node, recommendation, true);
}
};
}
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/no-implicit-coercion.js
Expand Up @@ -248,6 +248,17 @@ ruleTester.run("no-implicit-coercion", rule, {
type: "BinaryExpression"
}]
},
{
code: "`${foo}`",
output: "String(foo)",
options: [{ templateString: true }],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "useRecommendation",
data: { recommendation: "String(foo)" },
type: "TemplateLiteral"
}]
},
{
code: "foo += \"\"",
output: "foo = String(foo)",
Expand Down

0 comments on commit 6f9144d

Please sign in to comment.