From 7d832d4ad55f3c987aca8d858809cbcb77697648 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 18 Dec 2021 03:42:08 +0530 Subject: [PATCH] fix: improve `prefer-template` fixer (#15230) Fixes #15083 --- lib/rules/prefer-template.js | 2 +- tests/lib/rules/prefer-template.js | 181 +++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 1 deletion(-) diff --git a/lib/rules/prefer-template.js b/lib/rules/prefer-template.js index 953155d8f98..e61eac1d65d 100644 --- a/lib/rules/prefer-template.js +++ b/lib/rules/prefer-template.js @@ -188,7 +188,7 @@ module.exports = { return sourceCode.getText(currentNode); } - if (isConcatenation(currentNode) && hasStringLiteral(currentNode) && hasNonStringLiteral(currentNode)) { + if (isConcatenation(currentNode) && hasStringLiteral(currentNode)) { const plusSign = sourceCode.getFirstTokenBetween(currentNode.left, currentNode.right, token => token.value === "+"); const textBeforePlus = getTextBetween(currentNode.left, plusSign); const textAfterPlus = getTextBetween(plusSign, currentNode.right); diff --git a/tests/lib/rules/prefer-template.js b/tests/lib/rules/prefer-template.js index eea0255f1ac..a89a36a9de2 100644 --- a/tests/lib/rules/prefer-template.js +++ b/tests/lib/rules/prefer-template.js @@ -223,6 +223,187 @@ ruleTester.run("prefer-template", rule, { code: "foo + '\\0'", output: "`${foo }\\0`", errors + }, + + // https://github.com/eslint/eslint/issues/15083 + { + code: `"default-src 'self' https://*.google.com;" + + "frame-ancestors 'none';" + + "report-to " + foo + ";"`, + output: `\`default-src 'self' https://*.google.com;\` + + \`frame-ancestors 'none';\` + + \`report-to \${ foo };\``, + errors + }, + { + code: "'a' + 'b' + foo", + output: "`a` + `b${ foo}`", + errors + }, + { + code: "'a' + 'b' + foo + 'c' + 'd'", + output: "`a` + `b${ foo }c` + `d`", + errors + }, + { + code: "'a' + 'b + c' + foo + 'd' + 'e'", + output: "`a` + `b + c${ foo }d` + `e`", + errors + }, + { + code: "'a' + 'b' + foo + ('c' + 'd')", + output: "`a` + `b${ foo }c` + `d`", + errors + }, + { + code: "'a' + 'b' + foo + ('a' + 'b')", + output: "`a` + `b${ foo }a` + `b`", + errors + }, + { + code: "'a' + 'b' + foo + ('c' + 'd') + ('e' + 'f')", + output: "`a` + `b${ foo }c` + `d` + `e` + `f`", + errors + }, + { + code: "foo + ('a' + 'b') + ('c' + 'd')", + output: "`${foo }a` + `b` + `c` + `d`", + errors + }, + { + code: "'a' + foo + ('b' + 'c') + ('d' + bar + 'e')", + output: "`a${ foo }b` + `c` + `d${ bar }e`", + errors + }, + { + code: "foo + ('b' + 'c') + ('d' + bar + 'e')", + output: "`${foo }b` + `c` + `d${ bar }e`", + errors + }, + { + code: "'a' + 'b' + foo + ('c' + 'd' + 'e')", + output: "`a` + `b${ foo }c` + `d` + `e`", + errors + }, + { + code: "'a' + 'b' + foo + ('c' + bar + 'd')", + output: "`a` + `b${ foo }c${ bar }d`", + errors + }, + { + code: "'a' + 'b' + foo + ('c' + bar + ('d' + 'e') + 'f')", + output: "`a` + `b${ foo }c${ bar }d` + `e` + `f`", + errors + }, + { + code: "'a' + 'b' + foo + ('c' + bar + 'e') + 'f' + test", + output: "`a` + `b${ foo }c${ bar }e` + `f${ test}`", + errors + }, + { + code: "'a' + foo + ('b' + bar + 'c') + ('d' + test)", + output: "`a${ foo }b${ bar }c` + `d${ test}`", + errors + }, + { + code: "'a' + foo + ('b' + 'c') + ('d' + bar)", + output: "`a${ foo }b` + `c` + `d${ bar}`", + errors + }, + { + code: "foo + ('a' + bar + 'b') + 'c' + test", + output: "`${foo }a${ bar }b` + `c${ test}`", + errors + }, + { + code: "'a' + '`b`' + c", + output: "`a` + `\\`b\\`${ c}`", + errors + }, + { + code: "'a' + '`b` + `c`' + d", + output: "`a` + `\\`b\\` + \\`c\\`${ d}`", + errors + }, + { + code: "'a' + b + ('`c`' + '`d`')", + output: "`a${ b }\\`c\\`` + `\\`d\\``", + errors + }, + { + code: "'`a`' + b + ('`c`' + '`d`')", + output: "`\\`a\\`${ b }\\`c\\`` + `\\`d\\``", + errors + }, + { + code: "foo + ('`a`' + bar + '`b`') + '`c`' + test", + output: "`${foo }\\`a\\`${ bar }\\`b\\`` + `\\`c\\`${ test}`", + errors + }, + { + code: "'a' + ('b' + 'c') + d", + output: "`a` + `b` + `c${ d}`", + errors + }, + { + code: "'a' + ('`b`' + '`c`') + d", + 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: "a + ('`b` + `c`' + '`d`') + e", + 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: "a + (('b' + 'c') + 'd')", + output: "`${a }b` + `c` + `d`", + errors + }, + { + code: "(a + 'b') + ('c' + 'd') + e", + output: "`${a }b` + `c` + `d${ e}`", + 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: "\"Hello \" + \"'world' \" + test", + output: "`Hello ` + `'world' ${ test}`", + errors } ] });