Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: fix false negative of quotes with \n in template (fixes #7646
…) (#7647)

* Update: fix false negative of `quotes` with \n in template (fixes #7646)

* Fix incorrect comment
  • Loading branch information
not-an-aardvark authored and nzakas committed Nov 25, 2016
1 parent 474e444 commit faf5f56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rules/quotes.js
Expand Up @@ -258,7 +258,11 @@ module.exports = {
return;
}

const shouldWarn = node.quasis.length === 1 && (node.quasis[0].value.cooked.indexOf("\n") === -1);
/*
* A warning should be produced if the template literal only has one TemplateElement, and has no unescaped newlines.
* An unescaped newline is a newline preceded by an even number of backslashes.
*/
const shouldWarn = node.quasis.length === 1 && !/(^|[^\\])(\\\\)*[\r\n\u2028\u2029]/.test(node.quasis[0].value.raw);

if (shouldWarn) {
context.report({
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/quotes.js
Expand Up @@ -38,6 +38,16 @@ ruleTester.run("quotes", rule, {

// Backticks are only okay if they have substitutions, contain a line break, or are tagged
{ code: "var foo = `back\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 }},
{ code: "var foo = `back\rtick`;", options: ["single"], parserOptions: { ecmaVersion: 6 }},
{ code: "var foo = `back\u2028tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 }},
{ code: "var foo = `back\u2029tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 }},
{
code: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline
options: ["single"],
parserOptions: { ecmaVersion: 6 }
},
{ code: "var foo = `back\\\\\\\\\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 }},
{ code: "var foo = `\n`;", options: ["single"], parserOptions: { ecmaVersion: 6 }},
{ code: "var foo = `back${x}tick`;", options: ["double"], parserOptions: { ecmaVersion: 6 }},
{ code: "var foo = tag`backtick`;", options: ["double"], parserOptions: { ecmaVersion: 6 }},

Expand Down Expand Up @@ -272,6 +282,26 @@ ruleTester.run("quotes", rule, {
output: "foo(); \"use strict\";",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
},

// https://github.com/eslint/eslint/issues/7646
{
code: "var foo = `foo\\nbar`;",
output: "var foo = \"foo\\nbar\";",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
},
{
code: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline
output: "var foo = \"foo\\\nbar\";",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
},
{
code: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline
output: "var foo = \"foo\\\\\\\nbar\";",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
}
]
});

0 comments on commit faf5f56

Please sign in to comment.