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: autofix shouldn't produce template literals with \8 or \9 #13737

Merged
merged 2 commits into from Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 10 additions & 6 deletions lib/rules/utils/ast-utils.js
Expand Up @@ -38,7 +38,9 @@ const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]);
const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "SwitchCase"]);

const DECIMAL_INTEGER_PATTERN = /^(?:0|0[0-7]*[89]\d*|[1-9](?:_?\d)*)$/u;
const OCTAL_ESCAPE_PATTERN = /^(?:[^\\]|\\[^0-7]|\\0(?![0-9]))*\\(?:[1-7]|0[0-9])/u;

// Test for presence of at least one LegacyOctalEscapeSequence or NonOctalDecimalEscapeSequence in a raw string
const OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN = /^(?:[^\\]|\\.)*\\(?:[1-9]|0[0-9])/su;

const LOGICAL_ASSIGNMENT_OPERATORS = new Set(["&&=", "||=", "??="]);

Expand Down Expand Up @@ -1766,17 +1768,19 @@ module.exports = {
},

/**
* Determines whether the given raw string contains an octal escape sequence.
* Determines whether the given raw string contains an octal escape sequence
* or a non-octal decimal escape sequence ("\8", "\9").
*
* "\1", "\2" ... "\7"
* "\00", "\01" ... "\09"
* "\1", "\2" ... "\7", "\8", "\9"
* "\00", "\01" ... "\07", "\08", "\09"
*
* "\0", when not followed by a digit, is not an octal escape sequence.
* @param {string} rawString A string in its raw representation.
* @returns {boolean} `true` if the string contains at least one octal escape sequence.
* @returns {boolean} `true` if the string contains at least one octal escape sequence
* or at least one non-octal decimal escape sequence.
*/
hasOctalEscapeSequence(rawString) {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
return OCTAL_ESCAPE_PATTERN.test(rawString);
return OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN.test(rawString);
},

isLogicalExpression,
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/prefer-template.js
Expand Up @@ -194,6 +194,11 @@ ruleTester.run("prefer-template", rule, {
output: null,
errors
},
{
code: "foo + 'does not autofix non-octal decimal escape sequence' + '\\8'",
output: null,
errors
},
{
code: "foo + '\\n other text \\033'",
output: null,
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/quotes.js
Expand Up @@ -614,6 +614,19 @@ ruleTester.run("quotes", rule, {
type: "Literal"
}
]
},
{
code: "var nonOctalDecimalEscape = '\\8'",
output: null,
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "wrongQuotes",
data: { description: "backtick" },
type: "Literal"
}
]
}
]
});
25 changes: 16 additions & 9 deletions tests/lib/rules/utils/ast-utils.js
Expand Up @@ -1680,27 +1680,31 @@ describe("ast-utils", () => {
"\\\\\\1": true,
"\\\\\\01": true,
"\\\\\\08": true,
"\\8": true,
"\\9": true,
"a\\8a": true,
"\\0\\8": true,
"\\8\\0": true,
"\\80": true,
"\\81": true,
"\\\\\\8": true,
"\\\n\\1": true,
"foo\\\nbar\\2baz": true,
"\\\n\\8": true,
"foo\\\nbar\\9baz": true,

"\\0": false,
"\\8": false,
"\\9": false,
" \\0": false,
"\\0 ": false,
"a\\0": false,
"\\0a": false,
"a\\8a": false,
"\\0\\8": false,
"\\8\\0": false,
"\\80": false,
"\\81": false,
"\\\\": false,
"\\\\0": false,
"\\\\01": false,
"\\\\08": false,
"\\\\1": false,
"\\\\12": false,
"\\\\\\0": false,
"\\\\\\8": false,
"\\0\\\\": false,
"0": false,
"1": false,
Expand All @@ -1710,7 +1714,10 @@ describe("ast-utils", () => {
"80": false,
"12": false,
"\\a": false,
"\\n": false
"\\n": false,
"\\\n": false,
"foo\\\nbar": false,
"128\\\n349": false
};
/* eslint-enable quote-props */

Expand Down