diff options
author | Stephen Toub <stoub@microsoft.com> | 2022-11-11 05:14:47 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-11 05:14:47 +0300 |
commit | e1dcc9c1914fb407ff92ecb3eaf44e7ba0c831bb (patch) | |
tree | 16aae37ce6f6e9645185a11b6a4d898c2bd50750 | |
parent | 7db5a577938d83c76f0654635d6ffe1598358097 (diff) |
Fix regex fixer to maintain string literal syntax (#78172)
* Fix regex fixer to maintain string literal syntax
* Simplify
* Still transform interpolated strings
2 files changed, 93 insertions, 1 deletions
diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs index bf98ce298ec..442976d648b 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs @@ -274,6 +274,10 @@ namespace System.Text.RegularExpressions.Generator string optionsLiteral = Literal(((RegexOptions)(int)argument.Value.ConstantValue.Value).ToString()); return SyntaxFactory.ParseExpression(optionsLiteral); } + else if (argument.Value is ILiteralOperation literalOperation) + { + return literalOperation.Syntax; + } else { return generator.LiteralExpression(argument.Value.ConstantValue.Value); diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs index 18c9e25b671..9dafdf41020 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs @@ -792,7 +792,7 @@ partial class Program static class Class { public static string CollapseWhitespace(this string text) => - [|Regex.Replace(text, @"" \s+"" , "" "")|]; + [|Regex.Replace(text, "" \\s+"" , "" "")|]; }"; string expectedFixedCode = @"using System.Text.RegularExpressions; @@ -809,6 +809,94 @@ static partial class Class } [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() { string test = @"using System.Text.RegularExpressions; |