Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: quotes autofix produces syntax error with octal escape sequences #12118

Merged
merged 1 commit into from Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/rules/quotes.js
Expand Up @@ -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));
}
});
Expand Down
90 changes: 90 additions & 0 deletions tests/lib/rules/quotes.js
Expand Up @@ -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" }
]
}
]
});