From e10eebab4abd193dee697c4de7fb2d95bbab2d8c Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 31 Aug 2019 16:51:14 +0200 Subject: [PATCH] Fix: quotes autofix produces syntax error with octal escape sequences (#12118) --- lib/rules/quotes.js | 6 +++ tests/lib/rules/quotes.js | 90 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/lib/rules/quotes.js b/lib/rules/quotes.js index 28b20af29f4..5f260e55f0a 100644 --- a/lib/rules/quotes.js +++ b/lib/rules/quotes.js @@ -279,6 +279,12 @@ module.exports = { description: settings.description }, fix(fixer) { + if (quoteOption === "backtick" && astUtils.hasOctalEscapeSequence(rawVal)) { + + // An octal escape sequence in a template literal would produce syntax error, even in non-strict mode. + return null; + } + return fixer.replaceText(node, settings.convert(node.raw)); } }); diff --git a/tests/lib/rules/quotes.js b/tests/lib/rules/quotes.js index 3245c745476..5a12ad0fbf3 100644 --- a/tests/lib/rules/quotes.js +++ b/tests/lib/rules/quotes.js @@ -328,6 +328,96 @@ ruleTester.run("quotes", rule, { output: "\"\"``", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral", line: 1, column: 1 }] + }, + + // Strings containing octal escape sequences. Don't autofix to backticks. + { + code: "var foo = \"\\1\"", + output: "var foo = '\\1'", + options: ["single"], + errors: [ + { message: "Strings must use singlequote.", type: "Literal" } + ] + }, + { + code: "var foo = '\\1'", + output: "var foo = \"\\1\"", + options: ["double"], + errors: [ + { message: "Strings must use doublequote.", type: "Literal" } + ] + }, + { + code: "var notoctal = '\\0'", + output: "var notoctal = `\\0`", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] + }, + { + code: "var foo = '\\1'", + output: null, + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] + }, + { + code: "var foo = \"\\1\"", + output: null, + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] + }, + { + code: "var foo = '\\01'", + output: null, + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] + }, + { + code: "var foo = '\\0\\1'", + output: null, + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] + }, + { + code: "var foo = '\\08'", + output: null, + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] + }, + { + code: "var foo = 'prefix \\33'", + output: null, + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] + }, + { + code: "var foo = 'prefix \\75 sufix'", + output: null, + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" } + ] } ] });