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 } ] });