Skip to content

Commit

Permalink
Fix: no-useless-escape wrong loc and fix with CRLF in template elemen…
Browse files Browse the repository at this point in the history
…ts (#13953)
  • Loading branch information
mdjermanovic committed Dec 30, 2020
1 parent 19c69c0 commit f6e7e32
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/no-useless-escape.js
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;

Expand Down
90 changes: 90 additions & 0 deletions tests/lib/rules/no-useless-escape.js
Expand Up @@ -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 },
Expand Down

0 comments on commit f6e7e32

Please sign in to comment.