diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/UseRegexSourceGeneratorFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/UseRegexSourceGeneratorFixer.cs index 352cd03d..c614b62f 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/UseRegexSourceGeneratorFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/UseRegexSourceGeneratorFixer.cs @@ -38,15 +38,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) isCSharp14OrAbove = parseOptions.LanguageVersion.IsCSharp14OrAbove(); } - // Always offer partial method - context.RegisterCodeFix( - CodeAction.Create( - "Use Regex Source Generator (partial method)", - cancellationToken => ConvertToSourceGenerator(context.Document, context.Diagnostics[0], usePartialProperty: false, cancellationToken), - equivalenceKey: "Use Regex Source Generator (partial method)"), - context.Diagnostics); - - // Offer partial property if C# 14 or later + // Offer partial property first if C# 14 or later if (isCSharp14OrAbove) { context.RegisterCodeFix( @@ -56,6 +48,14 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) equivalenceKey: "Use Regex Source Generator (partial property)"), context.Diagnostics); } + + // Always offer partial method + context.RegisterCodeFix( + CodeAction.Create( + "Use Regex Source Generator (partial method)", + cancellationToken => ConvertToSourceGenerator(context.Document, context.Diagnostics[0], usePartialProperty: false, cancellationToken), + equivalenceKey: "Use Regex Source Generator (partial method)"), + context.Diagnostics); } private static async Task ConvertToSourceGenerator(Document document, Diagnostic diagnostic, bool usePartialProperty, CancellationToken cancellationToken) diff --git a/tests/Meziantou.Analyzer.Test/Rules/UseRegexSourceGeneratorAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/UseRegexSourceGeneratorAnalyzerTests.cs index 1cd12170..aee6fff1 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/UseRegexSourceGeneratorAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/UseRegexSourceGeneratorAnalyzerTests.cs @@ -16,6 +16,7 @@ private static ProjectBuilder CreateProjectBuilder() .WithNoFixCompilation(); // requires the regex source generator } +#if CSHARP11_OR_GREATER [Fact] public async Task NewRegex_Options_Timeout() { @@ -43,11 +44,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task NewRegex_Options() { @@ -75,11 +80,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task NewRegex() { @@ -107,11 +116,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task RegexIsMatch_Options_Timeout() { @@ -139,11 +152,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task RegexIsMatch_Options() { @@ -171,11 +188,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task RegexIsMatch() { @@ -203,11 +224,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task RegexReplace_Options_Timeout() { @@ -235,11 +260,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task RegexReplace_Options() { @@ -267,11 +296,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task RegexReplace() { @@ -299,11 +332,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task RegexReplace_MatchEvaluator() { @@ -331,11 +368,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Theory] [InlineData("TimeSpan.FromMilliseconds(10)", "10")] [InlineData("TimeSpan.FromSeconds(10.5)", "10500")] @@ -373,11 +414,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(sourceCode) - .ShouldFixCodeWith(codeFix) + .ShouldFixCodeWith( + codeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Theory] [InlineData("System.Threading.Timeout.InfiniteTimeSpan")] [InlineData("Regex.InfiniteMatchTimeout")] @@ -407,11 +452,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(sourceCode) - .ShouldFixCodeWith(codeFix) + .ShouldFixCodeWith( + codeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Theory] [InlineData("System.Threading.Timeout.InfiniteTimeSpan")] [InlineData("Regex.InfiniteMatchTimeout")] @@ -441,11 +490,15 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(sourceCode) - .ShouldFixCodeWith(codeFix) + .ShouldFixCodeWith( + codeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task GenerateUniqueMethodName() { @@ -476,10 +529,13 @@ partial class Test "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(sourceCode) - .ShouldFixCodeWith(codeFix) + .ShouldFixCodeWith( + codeFix) .ValidateAsync(); } +#endif [Fact] public async Task NonConstantPattern() @@ -499,6 +555,7 @@ await CreateProjectBuilder() .ValidateAsync(); } +#if CSHARP11_OR_GREATER [Fact] public async Task NestedTypeShouldAddPartialToAllAncestorTypes() { @@ -538,10 +595,13 @@ partial class Inner "; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(sourceCode) - .ShouldFixCodeWith(codeFix) + .ShouldFixCodeWith( + codeFix) .ValidateAsync(); } +#endif #if CSHARP14_OR_GREATER [Fact] @@ -575,7 +635,7 @@ partial class Test .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -610,7 +670,7 @@ partial class Test .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -645,7 +705,7 @@ partial class Test .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -682,7 +742,7 @@ partial class Test .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -719,7 +779,7 @@ partial class Test .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -756,7 +816,7 @@ partial class Test .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -805,11 +865,12 @@ partial class Inner .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } #endif +#if CSHARP11_OR_GREATER [Fact] public async Task Field_SuggestFieldName() { @@ -847,11 +908,15 @@ void M() """; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task Field_SuggestFieldNameWithoutRegexSuffix() { @@ -889,11 +954,15 @@ void M() """; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task Variable_SuggestPascalCaseName() { @@ -929,11 +998,15 @@ void A() """; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task Variable_AlreadyPascalCase() { @@ -969,11 +1042,15 @@ void A() """; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif +#if CSHARP11_OR_GREATER [Fact] public async Task StaticMethod_UseDefaultName() { @@ -1001,10 +1078,13 @@ partial class Test """; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif #if CSHARP14_OR_GREATER [Fact] @@ -1049,7 +1129,7 @@ void M() .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -1093,7 +1173,7 @@ void A() .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -1149,11 +1229,12 @@ void M2() .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } #endif +#if CSHARP11_OR_GREATER [Fact] public async Task TopLevelStatement_NewRegex_PartialMethod() { @@ -1178,11 +1259,14 @@ partial class Program """; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif #if CSHARP14_OR_GREATER [Fact] @@ -1214,7 +1298,7 @@ partial class Program .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -1247,7 +1331,7 @@ partial class Program .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } @@ -1287,11 +1371,12 @@ private static void Helper() { } .WithCodeFixProvider() .WithNoFixCompilation() .WithSourceCode(SourceCode) - .ShouldFixCodeWith(index: 1, CodeFix) + .ShouldFixCodeWith(CodeFix) .ValidateAsync(); } #endif +#if CSHARP11_OR_GREATER [Fact] public async Task TopLevelStatement_StaticMethod_PartialMethod() { @@ -1314,9 +1399,12 @@ partial class Program """; await CreateProjectBuilder() + .WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11) .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication) .WithSourceCode(SourceCode) - .ShouldFixCodeWith(CodeFix) + .ShouldFixCodeWith( + CodeFix) .ValidateAsync(); } +#endif }