From 4350ec13faabb4945a481dfd2c49c28d5de97778 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 17 Aug 2020 21:46:33 +0200 Subject: [PATCH] Update: Add templateString option in no-implicit-coercion (fixes #12866) 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. --- docs/rules/no-implicit-coercion.md | 27 +++++++++++++++++---- lib/rules/no-implicit-coercion.js | 32 +++++++++++++++++++++++++ tests/lib/rules/no-implicit-coercion.js | 11 +++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/docs/rules/no-implicit-coercion.md b/docs/rules/no-implicit-coercion.md index f78e40c66c2..6d33aa93923 100644 --- a/docs/rules/no-implicit-coercion.md +++ b/docs/rules/no-implicit-coercion.md @@ -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). @@ -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. diff --git a/lib/rules/no-implicit-coercion.js b/lib/rules/no-implicit-coercion.js index a639711ecea..058f3cc8c03 100644 --- a/lib/rules/no-implicit-coercion.js +++ b/lib/rules/no-implicit-coercion.js @@ -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 || [] }; } @@ -180,6 +181,10 @@ module.exports = { type: "boolean", default: true }, + templateString: { + type: "boolean", + default: false + }, allow: { type: "array", items: { @@ -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); } }; } diff --git a/tests/lib/rules/no-implicit-coercion.js b/tests/lib/rules/no-implicit-coercion.js index fa2b68b4975..f0048707d55 100644 --- a/tests/lib/rules/no-implicit-coercion.js +++ b/tests/lib/rules/no-implicit-coercion.js @@ -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)",