From 76deaa3365c1e8d317ba94708cfcf39c5df8bcf0 Mon Sep 17 00:00:00 2001 From: Hampus Kraft Date: Mon, 14 Nov 2022 11:28:00 +0100 Subject: [PATCH] `prefer-string-replace-all`: Ignore regex with pipe (#1962) --- rules/prefer-string-replace-all.js | 2 +- test/prefer-string-replace-all.mjs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index 995db0b70d..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) { diff --git a/test/prefer-string-replace-all.mjs b/test/prefer-string-replace-all.mjs index 56aabef02c..2bfc78e19d 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)', @@ -86,6 +87,11 @@ test({ output: 'foo.replaceAll(\'\\\\.\', bar)', errors: [error], }, + { + code: 'foo.replace(/\\|/g, bar)', + output: 'foo.replaceAll(\'|\', bar)', + errors: [error], + }, ], });