Skip to content

Commit

Permalink
Fix: quotes autofix produces syntax error with octal escape sequences (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and platinumazure committed Aug 31, 2019
1 parent 329e295 commit e10eeba
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
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" }
]
}
]
});

0 comments on commit e10eeba

Please sign in to comment.