Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: allow escaped backreferences in no-useless-escape (fixes #7472) (
  • Loading branch information
not-an-aardvark authored and kaicataldo committed Oct 31, 2016
1 parent fffdf13 commit 56a662b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 54 deletions.
60 changes: 7 additions & 53 deletions lib/rules/no-useless-escape.js
Expand Up @@ -22,55 +22,10 @@ function union(setA, setB) {
}());
}

const VALID_STRING_ESCAPES = [
"\\",
"n",
"r",
"v",
"t",
"b",
"f",
"u",
"x",
"\n",
"\r"
];

const REGEX_GENERAL_ESCAPES = new Set([
"\\",
"b",
"c",
"d",
"D",
"f",
"n",
"r",
"s",
"S",
"t",
"v",
"w",
"W",
"x",
"u"
]);
const REGEX_CHARCLASS_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set(["]"]));
const REGEX_NON_CHARCLASS_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set([
"^",
"/",
".",
"$",
"*",
"+",
"?",
"[",
"{",
"}",
"|",
"(",
")",
"B"
]));
const VALID_STRING_ESCAPES = new Set("\\nrvtbfux\n\r");
const REGEX_GENERAL_ESCAPES = new Set("\\bcdDfnrsStvwWxu0123456789");
const REGEX_CHARCLASS_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set("]"));
const REGEX_NON_CHARCLASS_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set("^/.$*+?[{}|()B"));

/**
* Parses a regular expression into a list of characters with character class info.
Expand Down Expand Up @@ -150,15 +105,14 @@ module.exports = {
* Checks if the escape character in given string slice is unnecessary.
*
* @private
* @param {string[]} escapes - list of valid escapes
* @param {ASTNode} node - node to validate.
* @param {string} match - string slice to validate.
* @returns {void}
*/
function validateString(escapes, node, match) {
function validateString(node, match) {
const isTemplateElement = node.type === "TemplateElement";
const escapedChar = match[0][1];
let isUnnecessaryEscape = escapes.indexOf(escapedChar) === -1;
let isUnnecessaryEscape = !VALID_STRING_ESCAPES.has(escapedChar);
let isQuoteEscape;

if (isTemplateElement) {
Expand Down Expand Up @@ -215,7 +169,7 @@ module.exports = {
let match;

while ((match = pattern.exec(value))) {
validateString(VALID_STRING_ESCAPES, node, match);
validateString(node, match);
}
} else if (node.regex) {
parseRegExp(node.regex.pattern)
Expand Down
9 changes: 8 additions & 1 deletion tests/lib/rules/no-useless-escape.js
Expand Up @@ -93,7 +93,14 @@ ruleTester.run("no-useless-escape", rule, {
String.raw`var foo = /\[abc]/`, // Matches the literal string '[abc]'
String.raw`var foo = /\[foo\.bar]/`, // Matches the literal string '[foo.bar]'
String.raw`var foo = /vi/m`,
String.raw`var foo = /\B/`
String.raw`var foo = /\B/`,

// https://github.com/eslint/eslint/issues/7472
String.raw`var foo = /\0/`, // null character
"var foo = /\\1/", // \x01 character (octal literal)
"var foo = /(a)\\1/", // backreference
"var foo = /(a)\\12/", // backreference
"var foo = /[\\0]/" // null character in character class
],

invalid: [
Expand Down

0 comments on commit 56a662b

Please sign in to comment.