Convert to GeneratedRegexAttribute fixer should preserve references to const string...
authorBadre BSAILA <54767641+pedrobsaila@users.noreply.github.com>
Thu, 16 Mar 2023 23:35:23 +0000 (00:35 +0100)
committerGitHub <noreply@github.com>
Thu, 16 Mar 2023 23:35:23 +0000 (16:35 -0700)
* Convert to GeneratedRegexAttribute fixer should preserve references to const string fields

* add test to cover external const case

* fix double slashes

* simplify verbatime string generation

* delete unused quote

src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs

index 1eb360f..dc1e667 100644 (file)
@@ -287,6 +287,15 @@ namespace System.Text.RegularExpressions.Generator
                 {
                     return literalOperation.Syntax;
                 }
+                else if (argument.Value is IFieldReferenceOperation fieldReferenceOperation &&
+                    fieldReferenceOperation.Member is IFieldSymbol fieldSymbol && fieldSymbol.IsConst)
+                {
+                    return generator.Argument(fieldReferenceOperation.Syntax);
+                }
+                else if (argument.Value.ConstantValue.Value is string str && str.Contains('\\'))
+                {
+                    return SyntaxFactory.ParseExpression($"@\"{str}\"");
+                }
                 else
                 {
                     return generator.LiteralExpression(argument.Value.ConstantValue.Value);
index 79fc411..6627540 100644 (file)
@@ -972,7 +972,81 @@ partial class Program
         Regex regex = MyRegex();
     }
 
-    [GeneratedRegex(""a|b\\s\\n2"")]
+    [GeneratedRegex(@""a|b\s\n2"")]
+    private static partial Regex MyRegex();
+}";
+
+            await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode);
+        }
+
+        [Fact]
+        public async Task InterpolatedStringLiteralSyntaxFixedWhenStringLiteralIsConstantField()
+        {
+            string test = @"using System.Text.RegularExpressions;
+
+partial class Program
+{
+    const string pattern = @""a|b\s\n"";
+    const string pattern2 = $""{pattern}2"";
+
+    static void Main(string[] args)
+    {
+        Regex regex = [|new Regex(pattern2)|];
+    }
+}";
+
+            string expectedFixedCode = @"using System.Text.RegularExpressions;
+
+partial class Program
+{
+    const string pattern = @""a|b\s\n"";
+    const string pattern2 = $""{pattern}2"";
+
+    static void Main(string[] args)
+    {
+        Regex regex = MyRegex();
+    }
+
+    [GeneratedRegex(pattern2)]
+    private static partial Regex MyRegex();
+}";
+
+            await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode);
+        }
+
+        [Fact]
+        public async Task InterpolatedStringLiteralSyntaxFixedWhenStringLiteralIsExternalConstantField()
+        {
+            string test = @"using System.Text.RegularExpressions;
+
+internal class GlobalConstants
+{
+    const string pattern = @""a|b\s\n"";
+    internal const string pattern2 = $""{pattern}2"";
+}
+partial class Program
+{
+    static void Main(string[] args)
+    {
+        Regex regex = [|new Regex(GlobalConstants.pattern2)|];
+    }
+}";
+
+            string expectedFixedCode = @"using System.Text.RegularExpressions;
+
+internal class GlobalConstants
+{
+    const string pattern = @""a|b\s\n"";
+    internal const string pattern2 = $""{pattern}2"";
+}
+partial class Program
+{
+    static void Main(string[] args)
+    {
+        Regex regex = MyRegex();
+    }
+
+    [GeneratedRegex(GlobalConstants.pattern2)]
     private static partial Regex MyRegex();
 }";