Skip to content

Commit

Permalink
test: add more cases for prefer-template rule
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Oct 29, 2021
1 parent 9ee0532 commit 97db466
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/prefer-template.js
Expand Up @@ -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}\``;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/prefer-template.js
Expand Up @@ -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
}
]
});

0 comments on commit 97db466

Please sign in to comment.