diff --git a/docs/rules/no-div-regex.md b/docs/rules/no-div-regex.md index 8555536e547..211525b3ba3 100644 --- a/docs/rules/no-div-regex.md +++ b/docs/rules/no-div-regex.md @@ -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 diff --git a/lib/rules/no-div-regex.js b/lib/rules/no-div-regex.js index 408e006528b..0ccabdcc698 100644 --- a/lib/rules/no-div-regex.js +++ b/lib/rules/no-div-regex.js @@ -20,6 +20,8 @@ module.exports = { url: "https://eslint.org/docs/rules/no-div-regex" }, + fixable: "code", + schema: [], messages: { @@ -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], "[=]"); + } + }); } } }; diff --git a/tests/lib/rules/no-div-regex.js b/tests/lib/rules/no-div-regex.js index 531094c19d2..4627e20ef29 100644 --- a/tests/lib/rules/no-div-regex.js +++ b/tests/lib/rules/no-div-regex.js @@ -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" }] + } ] });