From 184b1569e5d20c1cbb0ce01b858ac3b0ef49d370 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 10 Nov 2022 11:59:23 -0500 Subject: [PATCH 1/3] Fix regex fixer to maintain string literal syntax --- .../gen/UpgradeToGeneratedRegexCodeFixer.cs | 5 +- .../UpgradeToGeneratedRegexAnalyzerTests.cs | 55 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs index bf98ce298ecff0..20b07f4e744145 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs @@ -276,7 +276,10 @@ static RegexOptions GetRegexOptionsFromArgument(ImmutableArray - [|Regex.Replace(text, @"" \s+"" , "" "")|]; + [|Regex.Replace(text, "" \\s+"" , "" "")|]; }"; string expectedFixedCode = @"using System.Text.RegularExpressions; @@ -808,6 +808,59 @@ 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 TestAsArgument() { From bf2d3ddddf854279c94481967b4e1a8846a027bf Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 10 Nov 2022 13:13:27 -0500 Subject: [PATCH 2/3] Simplify --- .../gen/UpgradeToGeneratedRegexCodeFixer.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs index 20b07f4e744145..0025bf7f8edc23 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs @@ -276,10 +276,7 @@ static RegexOptions GetRegexOptionsFromArgument(ImmutableArray Date: Thu, 10 Nov 2022 14:02:32 -0500 Subject: [PATCH 3/3] Still transform interpolated strings --- .../gen/UpgradeToGeneratedRegexCodeFixer.cs | 6 +++- .../UpgradeToGeneratedRegexAnalyzerTests.cs | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs index 0025bf7f8edc23..442976d648b834 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs @@ -274,9 +274,13 @@ static RegexOptions GetRegexOptionsFromArgument(ImmutableArray 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() {