Skip to content

Commit

Permalink
fix: cover more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Dec 5, 2021
1 parent 8195370 commit 8b107c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/rules/prefer-template.js
Expand Up @@ -198,10 +198,11 @@ module.exports = {

if (currentNode.range[0] === currentNode.parent.left.range[0]) {
if (isConcatenation(currentNode.right)) {
const openingParentesisSign = sourceCode.getFirstTokenBetween(currentNode.left, currentNode.right, token => token.value === "(");
const leftNodeText = currentNodeText.slice(0, openingParentesisSign.range[0] - firstToken.range[0]);
const openingParenthesisSign = sourceCode.getFirstTokenBetween(currentNode.left, currentNode.right, token => token.value === "(");
const leftNodeText = currentNodeText.slice(0, openingParenthesisSign.range[0] - firstToken.range[0]);
const leftNodeTextBetweenParenthesis = currentNodeText.slice(currentNode.right.left.range[0], currentNode.right.left.range[1]);

return `${leftNodeText}${currentNode.right.left.raw} + \`${escapeTemplatePlaceholders(currentNode.right.right.raw)}'`;
return `${leftNodeText}${leftNodeTextBetweenParenthesis} + \`${escapeTemplatePlaceholders(currentNode.right.right.raw)}'`;
}

const leftNodeText = currentNodeText.slice(0, currentNode.right.range[0] - firstToken.range[0]);
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/prefer-template.js
Expand Up @@ -370,6 +370,16 @@ ruleTester.run("prefer-template", rule, {
output: "`${a }\\`b\\` + \\`c\\`` + `\\`d\\`${ e}`",
errors
},
{
code: "'a' + ('b' + 'c' + 'd') + e",
output: "'a' + 'b' + 'c' + `d${ e}`",
errors
},
{
code: "'a' + ('b' + 'c' + 'd' + (e + 'f') + 'g' +'h' + 'i') + j",
output: "`a` + 'b' + 'c' + `d${ e }fg` +`h` + `i${ j}`",
errors
},
{
code: "var foo = \"Hello \" + \"world \" + \"another \" + test",
output: "var foo = \"Hello \" + \"world \" + `another ${ test}`",
Expand Down

0 comments on commit 8b107c2

Please sign in to comment.