From 71716eba3155266d777b994a38af524952e97696 Mon Sep 17 00:00:00 2001 From: joe-re Date: Sun, 26 May 2019 03:21:20 +0800 Subject: [PATCH] Update: add fixer for no-div-regex rule (fixes #11355) (#11744) Adding fixer for no-div-regex-rule and use [=] instead of \= as suggestion to avoid conflicting with no-useless-escape error. --- docs/rules/no-div-regex.md | 2 +- lib/rules/no-div-regex.js | 10 +++++++++- tests/lib/rules/no-div-regex.js | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) 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" }] + } ] });