Skip to content
Permalink
Browse files
Fix: no-useless-escape wrong loc and fix with CRLF in template elemen…
…ts (#13953)
  • Loading branch information
mdjermanovic committed Dec 30, 2020
1 parent 19c69c0 commit f6e7e3231bc43c989f8c953de8e0d328bac5eea0
Showing with 92 additions and 2 deletions.
  1. +2 −2 lib/rules/no-useless-escape.js
  2. +90 −0 tests/lib/rules/no-useless-escape.js
@@ -172,7 +172,7 @@ module.exports = {
}

if (isUnnecessaryEscape && !isQuoteEscape) {
report(node, match.index + 1, match[0].slice(1));
report(node, match.index, match[0].slice(1));
}
}

@@ -206,7 +206,7 @@ module.exports = {
return;
}

const value = isTemplateElement ? node.value.raw : node.raw.slice(1, -1);
const value = isTemplateElement ? sourceCode.getText(node) : node.raw;
const pattern = /\\[^\d]/gu;
let match;

@@ -955,6 +955,96 @@ ruleTester.run("no-useless-escape", rule, {
}]
}]
},
{
code: "`multiline template\r\nliteral with useless \\escape`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 2,
column: 22,
endColumn: 23,
message: "Unnecessary escape character: \\e.",
type: "TemplateElement",
suggestions: [{
messageId: "removeEscape",
output: "`multiline template\r\nliteral with useless escape`"
}, {
messageId: "escapeBackslash",
output: "`multiline template\r\nliteral with useless \\\\escape`"
}]
}]
},
{
code: "`template literal with line continuation \\\nand useless \\escape`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 2,
column: 13,
endColumn: 14,
message: "Unnecessary escape character: \\e.",
type: "TemplateElement",
suggestions: [{
messageId: "removeEscape",
output: "`template literal with line continuation \\\nand useless escape`"
}, {
messageId: "escapeBackslash",
output: "`template literal with line continuation \\\nand useless \\\\escape`"
}]
}]
},
{
code: "`template literal with line continuation \\\r\nand useless \\escape`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 2,
column: 13,
endColumn: 14,
message: "Unnecessary escape character: \\e.",
type: "TemplateElement",
suggestions: [{
messageId: "removeEscape",
output: "`template literal with line continuation \\\r\nand useless escape`"
}, {
messageId: "escapeBackslash",
output: "`template literal with line continuation \\\r\nand useless \\\\escape`"
}]
}]
},
{
code: "`template literal with mixed linebreaks \r\r\n\n\\and useless escape`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 4,
column: 1,
endColumn: 2,
message: "Unnecessary escape character: \\a.",
type: "TemplateElement",
suggestions: [{
messageId: "removeEscape",
output: "`template literal with mixed linebreaks \r\r\n\nand useless escape`"
}, {
messageId: "escapeBackslash",
output: "`template literal with mixed linebreaks \r\r\n\n\\\\and useless escape`"
}]
}]
},
{
code: "`template literal with mixed linebreaks in line continuations \\\n\\\r\\\r\n\\and useless escape`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 4,
column: 1,
endColumn: 2,
message: "Unnecessary escape character: \\a.",
type: "TemplateElement",
suggestions: [{
messageId: "removeEscape",
output: "`template literal with mixed linebreaks in line continuations \\\n\\\r\\\r\nand useless escape`"
}, {
messageId: "escapeBackslash",
output: "`template literal with mixed linebreaks in line continuations \\\n\\\r\\\r\n\\\\and useless escape`"
}]
}]
},
{
code: "`\\a```",
parserOptions: { ecmaVersion: 6 },

0 comments on commit f6e7e32

Please sign in to comment.