From 97db46690094516a7de1367f32beff21dc968d10 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 28 Oct 2021 07:16:30 -0700 Subject: [PATCH] test: add more cases for prefer-template rule --- lib/rules/prefer-template.js | 4 ++-- tests/lib/rules/prefer-template.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/rules/prefer-template.js b/lib/rules/prefer-template.js index 4e946b37664..fbd9b5ca339 100644 --- a/lib/rules/prefer-template.js +++ b/lib/rules/prefer-template.js @@ -203,8 +203,8 @@ module.exports = { if (isConcatenation(currentNode) && !hasIdentifierReference(currentNode)) { - // eslint-disable-next-line require-unicode-regexp -- `"Hello " + "World"` -> `Hello World` - const concatenatedText = sourceCode.getText(currentNode).split("+").map(e => e.trim()).join("").replace(/"/g, ""); + // As there are no varibales used in this concatenation, we can sipmly calculate the concatenated string + const concatenatedText = sourceCode.getText(currentNode).split("+").map(e => e.trim().slice(1, -1)).join(""); return `\`${concatenatedText}\``; } diff --git a/tests/lib/rules/prefer-template.js b/tests/lib/rules/prefer-template.js index eea0255f1ac..ffb0cda86a9 100644 --- a/tests/lib/rules/prefer-template.js +++ b/tests/lib/rules/prefer-template.js @@ -223,6 +223,30 @@ ruleTester.run("prefer-template", rule, { code: "foo + '\\0'", output: "`${foo }\\0`", errors + }, + + // https://github.com/eslint/eslint/issues/15083 + { + code: "\"Hello \" + \"world \" + test", + output: "`Hello world ${ test}`", + errors + }, + { + code: "var foo = \"Hello \" + \"world \" + \"another \" + test", + output: "var foo = `Hello world another ${ test}`", + errors + }, + { + code: "\"Hello \" + \"'world' \" + test", + output: "`Hello 'world' ${ test}`", + errors + }, + { + code: `"default-src 'self' https://*.google.com;" + + "frame-ancestors 'none';" + + "report-to " + test + ";"`, + output: "`default-src 'self' https://*.google.com;frame-ancestors 'none';report-to ${ test };`", + errors } ] });