Skip to content

Commit

Permalink
fix: improve fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Nov 7, 2021
1 parent a0a5763 commit 875dfac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rules/prefer-template.js
Expand Up @@ -219,6 +219,15 @@ module.exports = {
const nodeBeforeCurrentNode = currentNode.parent.left.right || currentNode.parent.left;

if (hasNonStringLiteral(nodeBeforeCurrentNode)) {
const nodeAfterCurrentNode = currentNode.parent.parent && currentNode.parent.parent.right;
const firstTokenAfterCurrentNode = nodeAfterCurrentNode && sourceCode.getFirstToken(nodeAfterCurrentNode);

if (nodeAfterCurrentNode && hasNonStringLiteral(nodeAfterCurrentNode) && firstTokenAfterCurrentNode.type !== "String") {
const leftNodeText = sourceCode.getText(currentNode.left);

return `\\${escapeTemplatePlaceholders(firstToken.value)}\`${leftNodeText.slice(firstToken.value.length)} + \`${escapeTemplatePlaceholders(currentNode.right.raw)}'`;
}

return `\\${escapeTemplatePlaceholders(firstToken.value)}\`${currentNodeText.slice(firstToken.value.length)}`;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/prefer-template.js
Expand Up @@ -350,6 +350,21 @@ ruleTester.run("prefer-template", rule, {
output: "'a' + '`b`' + `\\`c\\`${ d}`",
errors
},
{
code: "a + ('b' + 'c') + d",
output: "`${a }b` + `c${ d}`",
errors
},
{
code: "a + ('b' + 'c') + (d + 'e')",
output: "`${a }b` + `c${ d }e`",
errors
},
{
code: "a + ('`b`' + '`c`') + d",
output: "`${a }\\`b\\`` + `\\`c\\`${ d}`",
errors
},
{
code: "var foo = \"Hello \" + \"world \" + \"another \" + test",
output: "var foo = \"Hello \" + \"world \" + `another ${ test}`",
Expand Down

0 comments on commit 875dfac

Please sign in to comment.