Skip to content

Commit

Permalink
Update: add fixer for no-div-regex rule (fixes #11355) (#11744)
Browse files Browse the repository at this point in the history
Adding fixer for no-div-regex-rule and use [=] instead of \= as suggestion to avoid conflicting with no-useless-escape error.
  • Loading branch information
joe-re authored and kaicataldo committed May 25, 2019
1 parent 53f7f4c commit 71716eb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/rules/no-div-regex.md
Expand Up @@ -23,7 +23,7 @@ Examples of **correct** code for this rule:
```js
/*eslint no-div-regex: "error"*/

function bar() { return /\=foo/; }
function bar() { return /[=]foo/; }
```

## Related Rules
Expand Down
10 changes: 9 additions & 1 deletion lib/rules/no-div-regex.js
Expand Up @@ -20,6 +20,8 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-div-regex"
},

fixable: "code",

schema: [],

messages: {
Expand All @@ -36,7 +38,13 @@ module.exports = {
const token = sourceCode.getFirstToken(node);

if (token.type === "RegularExpression" && token.value[1] === "=") {
context.report({ node, messageId: "unexpected" });
context.report({
node,
messageId: "unexpected",
fix(fixer) {
return fixer.replaceTextRange([token.range[0] + 1, token.range[0] + 2], "[=]");
}
});
}
}
};
Expand Down
6 changes: 5 additions & 1 deletion tests/lib/rules/no-div-regex.js
Expand Up @@ -24,6 +24,10 @@ ruleTester.run("no-div-regex", rule, {
"var f = function() { return /\\=foo/; };"
],
invalid: [
{ code: "var f = function() { return /=foo/; };", errors: [{ messageId: "unexpected", type: "Literal" }] }
{
code: "var f = function() { return /=foo/; };",
output: "var f = function() { return /[=]foo/; };",
errors: [{ messageId: "unexpected", type: "Literal" }]
}
]
});

0 comments on commit 71716eb

Please sign in to comment.