diff --git a/lib/rules/no-useless-escape.js b/lib/rules/no-useless-escape.js index 8057e44ddab..476aca06de7 100644 --- a/lib/rules/no-useless-escape.js +++ b/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; diff --git a/tests/lib/rules/no-useless-escape.js b/tests/lib/rules/no-useless-escape.js index 9321385b55b..ac3294b8c7f 100644 --- a/tests/lib/rules/no-useless-escape.js +++ b/tests/lib/rules/no-useless-escape.js @@ -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 },