From 35a68b52780d8707fb5ad3136bcab8811cd4b793 Mon Sep 17 00:00:00 2001 From: Hampus Kraft Date: Mon, 14 Nov 2022 02:22:12 +0100 Subject: [PATCH 1/3] `prefer-string-replace-all`: Ignore regex with pipe --- rules/prefer-string-replace-all.js | 2 +- test/prefer-string-replace-all.mjs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index 995db0b70d..99b19c0f6e 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -19,7 +19,7 @@ const isRegexWithGlobalFlag = node => function isLiteralCharactersOnly(node) { const searchPattern = node.regex.pattern; - return !/[$()*+.?[\\\]^{}]/.test(searchPattern.replace(/\\[$()*+.?[\\\]^{}]/g, '')); + return !/[$()*+.?[\\\]^{}|]/.test(searchPattern.replace(/\\[$()*+.?[\\\]^{}|]/g, '')); } function removeEscapeCharacters(regexString) { diff --git a/test/prefer-string-replace-all.mjs b/test/prefer-string-replace-all.mjs index 56aabef02c..4231091e9c 100644 --- a/test/prefer-string-replace-all.mjs +++ b/test/prefer-string-replace-all.mjs @@ -14,6 +14,7 @@ test({ 'foo.replace(/[a]/g, bar)', 'foo.replace(/a?/g, bar)', 'foo.replace(/.*/g, bar)', + 'foo.replace(/a|b/g, bar)', 'foo.replace(/\\W/g, bar)', 'foo.replace(/\\u{61}/g, bar)', 'foo.replace(/\\u{61}/gu, bar)', From fbbccddcec24f9d03b2330ba6ea4138f0738f1bc Mon Sep 17 00:00:00 2001 From: Hampus Kraft Date: Mon, 14 Nov 2022 08:15:13 +0100 Subject: [PATCH 2/3] `prefer-string-replace-all`: Add test for invalid --- test/prefer-string-replace-all.mjs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/prefer-string-replace-all.mjs b/test/prefer-string-replace-all.mjs index 4231091e9c..2bfc78e19d 100644 --- a/test/prefer-string-replace-all.mjs +++ b/test/prefer-string-replace-all.mjs @@ -87,6 +87,11 @@ test({ output: 'foo.replaceAll(\'\\\\.\', bar)', errors: [error], }, + { + code: 'foo.replace(/\\|/g, bar)', + output: 'foo.replaceAll(\'|\', bar)', + errors: [error], + }, ], }); From f67a9f2ca208be1763d27a2af09b0dfcf39ba279 Mon Sep 17 00:00:00 2001 From: Hampus Kraft Date: Mon, 14 Nov 2022 08:17:53 +0100 Subject: [PATCH 3/3] `prefer-string-replace-all`: Fix lint error --- rules/prefer-string-replace-all.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index 99b19c0f6e..5f0e8e032d 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -19,7 +19,7 @@ const isRegexWithGlobalFlag = node => function isLiteralCharactersOnly(node) { const searchPattern = node.regex.pattern; - return !/[$()*+.?[\\\]^{}|]/.test(searchPattern.replace(/\\[$()*+.?[\\\]^{}|]/g, '')); + return !/[$()*+.?[\\\]^{|}]/.test(searchPattern.replace(/\\[$()*+.?[\\\]^{|}]/g, '')); } function removeEscapeCharacters(regexString) {