Repository: eslint/eslint (JavaScript)

Issue:
wrap-regex doesn't work in some expression
**Tell us about your environment**

* **ESLint Version:** 5.0.1
* **Node Version:** 8.11.3
* **npm Version:** 5.6.0

**What parser (default, Babel-ESLint, etc.) are you using?**

default

**Please show your full configuration:**

```js
module.exports = {
  rules: {
    'wrap-regex': 2,
  },
}
```

**What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.**

```js
// sample.1
const bar = /abc/.test(foo)

// sample.2
if (/abc/.test(foo)){
  baz()
}
```

```bash
eslint --fix test.js
```

**What did you expect to happen?**

ESLint should report errors in both samples, but in fact, sample 2 passed the lint, no error reports.

**What actually happened? Please include the actual, raw output from ESLint.**

```js
// sample.1 fixed correct
const bar = (/abc/).test(foo)

// sample.2 not fixed
if (/abc/.test(foo)){
  baz()
}
```

**Online demo**
[demo](https://eslint.org/demo/#eyJ0ZXh0IjoiY29uc3QgYmFyID0gL2FiYy8udGVzdChmb28pXG5cbmlmICgvYWJjLy50ZXN0KGZvbykpe1xuICBiYXooKVxufSIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6OSwic291cmNlVHlwZSI6Im1vZHVsZSIsImVjbWFGZWF0dXJlcyI6e319LCJydWxlcyI6eyJ3cmFwLXJlZ2V4IjoyfSwiZW52Ijp7fX19)


Discussion:

Patch:
diff --git a/lib/rules/wrap-regex.js b/lib/rules/wrap-regex.js
index e58a4fe53766..1816e0e9e64e 100644
--- a/lib/rules/wrap-regex.js
+++ b/lib/rules/wrap-regex.js
@@ -20,7 +20,10 @@ module.exports = {
 
         schema: [],
 
-        fixable: "code"
+        fixable: "code",
+        messages: {
+            requireParens: "Wrap the regexp literal in parens to disambiguate the slash."
+        }
     },
 
     create(context) {
@@ -33,15 +36,16 @@ module.exports = {
                     nodeType = token.type;
 
                 if (nodeType === "RegularExpression") {
-                    const source = sourceCode.getTokenBefore(node);
+                    const beforeToken = sourceCode.getTokenBefore(node);
+                    const afterToken = sourceCode.getTokenAfter(node);
                     const ancestors = context.getAncestors();
                     const grandparent = ancestors[ancestors.length - 1];
 
                     if (grandparent.type === "MemberExpression" && grandparent.object === node &&
-                        (!source || source.value !== "(")) {
+                        !(beforeToken && beforeToken.value === "(" && afterToken && afterToken.value === ")")) {
                         context.report({
                             node,
-                            message: "Wrap the regexp literal in parens to disambiguate the slash.",
+                            messageId: "requireParens",
                             fix: fixer => fixer.replaceText(node, `(${sourceCode.getText(node)})`)
                         });
                     }


Test patch:
diff --git a/tests/lib/rules/wrap-regex.js b/tests/lib/rules/wrap-regex.js
index 203f7852c54f..8e3fc2714066 100644
--- a/tests/lib/rules/wrap-regex.js
+++ b/tests/lib/rules/wrap-regex.js
@@ -37,6 +37,13 @@ ruleTester.run("wrap-regex", rule, {
             code: "/foo/ig.test(bar);",
             output: "(/foo/ig).test(bar);",
             errors: [{ message: "Wrap the regexp literal in parens to disambiguate the slash.", type: "Literal" }]
+        },
+
+        // https://github.com/eslint/eslint/issues/10573
+        {
+            code: "if(/foo/ig.test(bar));",
+            output: "if((/foo/ig).test(bar));",
+            errors: [{ message: "Wrap the regexp literal in parens to disambiguate the slash.", type: "Literal" }]
         }
     ]
 });