diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs index bf98ce298ecff0..442976d648b834 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs @@ -274,6 +274,10 @@ static RegexOptions GetRegexOptionsFromArgument(ImmutableArray - [|Regex.Replace(text, @"" \s+"" , "" "")|]; + [|Regex.Replace(text, "" \\s+"" , "" "")|]; }"; string expectedFixedCode = @"using System.Text.RegularExpressions; @@ -808,6 +808,94 @@ public static string CollapseWhitespace(this string text) => await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode); } + [Fact] + public async Task VerbatimStringLiteralSyntaxPreservedByFixer() + { + string test = @"using System.Text.RegularExpressions; + +static class Class +{ + public static string CollapseWhitespace(this string text) => + [|Regex.Replace(text, @"" \s+"" , @"" "")|]; +}"; + + string expectedFixedCode = @"using System.Text.RegularExpressions; + +static partial class Class +{ + public static string CollapseWhitespace(this string text) => + MyRegex().Replace(text, @"" ""); + [GeneratedRegex(@"" \s+"")] + private static partial Regex MyRegex(); +}"; + + await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode); + } + + [Fact] + public async Task RawStringLiteralSyntaxPreservedByFixer() + { + string test = @"using System.Text.RegularExpressions; + +static class Class +{ + public static string CollapseWhitespace(this string text) => + [|Regex.Replace(text, """""" + \s+ + """""", + """""""" hello """""" world """""""")|]; +}"; + + string expectedFixedCode = @"using System.Text.RegularExpressions; + +static partial class Class +{ + public static string CollapseWhitespace(this string text) => + MyRegex().Replace(text, """""""" hello """""" world """"""""); + [GeneratedRegex("""""" + \s+ + """""")] + private static partial Regex MyRegex(); +}"; + + await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode); + } + + [Fact] + public async Task InterpolatedStringLiteralSyntaxPreservedByFixer() + { + string test = @"using System.Text.RegularExpressions; + +partial class Program +{ + static void Main(string[] args) + { + const string pattern = @""a|b\s\n""; + const string pattern2 = $""{pattern}2""; + + Regex regex = [|new Regex(pattern2)|]; + } +}"; + + string expectedFixedCode = @"using System.Text.RegularExpressions; + +partial class Program +{ + static void Main(string[] args) + { + const string pattern = @""a|b\s\n""; + const string pattern2 = $""{pattern}2""; + + Regex regex = MyRegex(); + } + + [GeneratedRegex(""a|b\\s\\n2"")] + private static partial Regex MyRegex(); +}"; + + await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode); + } + [Fact] public async Task TestAsArgument() {