From 77fe72313403505d214893fd51f5912d018e5c42 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Wed, 12 Jun 2024 16:35:46 -0700 Subject: [PATCH 1/2] Fix analyzer message formatting for RS1032 --- Source/Moq.Analyzers/Diagnostics.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Moq.Analyzers/Diagnostics.cs b/Source/Moq.Analyzers/Diagnostics.cs index 008f6f2c..666d0f98 100644 --- a/Source/Moq.Analyzers/Diagnostics.cs +++ b/Source/Moq.Analyzers/Diagnostics.cs @@ -6,27 +6,27 @@ internal static class Diagnostics internal const string NoSealedClassMocksId = "Moq1000"; internal const string NoSealedClassMocksTitle = "Moq: Sealed class mocked"; - internal const string NoSealedClassMocksMessage = "Sealed classes cannot be mocked."; + internal const string NoSealedClassMocksMessage = "Sealed classes cannot be mocked"; internal const string NoConstructorArgumentsForInterfaceMockId = "Moq1001"; internal const string NoConstructorArgumentsForInterfaceMockTitle = "Moq: Parameters specified for mocked interface"; - internal const string NoConstructorArgumentsForInterfaceMockMessage = "Mocked interfaces cannot have constructor parameters."; + internal const string NoConstructorArgumentsForInterfaceMockMessage = "Mocked interfaces cannot have constructor parameters"; internal const string ConstructorArgumentsShouldMatchId = "Moq1002"; internal const string ConstructorArgumentsShouldMatchTitle = "Moq: No matching constructor"; - internal const string ConstructorArgumentsShouldMatchMessage = "Parameters provided into mock do not match any existing constructors."; + internal const string ConstructorArgumentsShouldMatchMessage = "Parameters provided into mock do not match any existing constructors"; internal const string CallbackSignatureShouldMatchMockedMethodId = "Moq1100"; internal const string CallbackSignatureShouldMatchMockedMethodTitle = "Moq: Bad callback parameters"; - internal const string CallbackSignatureShouldMatchMockedMethodMessage = "Callback signature must match the signature of the mocked method."; + internal const string CallbackSignatureShouldMatchMockedMethodMessage = "Callback signature must match the signature of the mocked method"; internal const string NoMethodsInPropertySetupId = "Moq1101"; internal const string NoMethodsInPropertySetupTitle = "Moq: Property setup used for a method"; - internal const string NoMethodsInPropertySetupMessage = "SetupGet/SetupSet should be used for properties, not for methods."; + internal const string NoMethodsInPropertySetupMessage = "SetupGet/SetupSet should be used for properties, not for methods"; internal const string SetupShouldBeUsedOnlyForOverridableMembersId = "Moq1200"; internal const string SetupShouldBeUsedOnlyForOverridableMembersTitle = "Moq: Invalid setup parameter"; - internal const string SetupShouldBeUsedOnlyForOverridableMembersMessage = "Setup should be used only for overridable members."; + internal const string SetupShouldBeUsedOnlyForOverridableMembersMessage = "Setup should be used only for overridable members"; internal const string SetupShouldNotIncludeAsyncResultId = "Moq1201"; internal const string SetupShouldNotIncludeAsyncResultTitle = SetupShouldBeUsedOnlyForOverridableMembersTitle; From 7ead6c8eca7fdfa1dd72201046ce16abe537f9ce Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Wed, 12 Jun 2024 16:50:10 -0700 Subject: [PATCH 2/2] Inline Diagnostics constants into the analyzer classes --- .../AsShouldBeUsedOnlyForInterfaceAnalyzer.cs | 14 ++++--- ...ignatureShouldMatchMockedMethodAnalyzer.cs | 14 ++++--- ...SignatureShouldMatchMockedMethodCodeFix.cs | 2 +- ...ConstructorArgumentsShouldMatchAnalyzer.cs | 14 ++++--- Source/Moq.Analyzers/DiagnosticCategory.cs | 6 +++ Source/Moq.Analyzers/Diagnostics.cs | 38 ------------------- ...ructorArgumentsForInterfaceMockAnalyzer.cs | 14 ++++--- .../NoMethodsInPropertySetupAnalyzer.cs | 14 ++++--- .../NoSealedClassMocksAnalyzer.cs | 14 ++++--- ...BeUsedOnlyForOverridableMembersAnalyzer.cs | 14 ++++--- ...etupShouldNotIncludeAsyncResultAnalyzer.cs | 14 ++++--- 11 files changed, 79 insertions(+), 79 deletions(-) create mode 100644 Source/Moq.Analyzers/DiagnosticCategory.cs delete mode 100644 Source/Moq.Analyzers/Diagnostics.cs diff --git a/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs b/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs index a63be606..55c7bea4 100644 --- a/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs +++ b/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs @@ -3,16 +3,20 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class AsShouldBeUsedOnlyForInterfaceAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1300"; + private const string Title = "Moq: Invalid As type parameter"; + private const string Message = "Mock.As() should take interfaces only"; + private static readonly MoqMethodDescriptorBase MoqAsMethodDescriptor = new MoqAsMethodDescriptor(); private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.AsShouldBeUsedOnlyForInterfaceId, - Diagnostics.AsShouldBeUsedOnlyForInterfaceTitle, - Diagnostics.AsShouldBeUsedOnlyForInterfaceMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Error, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.AsShouldBeUsedOnlyForInterfaceId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); diff --git a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs index 5212b0ec..5adb9d67 100644 --- a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs +++ b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class CallbackSignatureShouldMatchMockedMethodAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1100"; + private const string Title = "Moq: Bad callback parameters"; + private const string Message = "Callback signature must match the signature of the mocked method"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.CallbackSignatureShouldMatchMockedMethodId, - Diagnostics.CallbackSignatureShouldMatchMockedMethodTitle, - Diagnostics.CallbackSignatureShouldMatchMockedMethodMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.CallbackSignatureShouldMatchMockedMethodId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs index 5e88c879..938cb6e9 100644 --- a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs +++ b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs @@ -12,7 +12,7 @@ public class CallbackSignatureShouldMatchMockedMethodCodeFix : CodeFixProvider { public sealed override ImmutableArray FixableDiagnosticIds { - get { return ImmutableArray.Create(Diagnostics.CallbackSignatureShouldMatchMockedMethodId); } + get { return ImmutableArray.Create(CallbackSignatureShouldMatchMockedMethodAnalyzer.RuleId); } } public sealed override FixAllProvider GetFixAllProvider() diff --git a/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs b/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs index 5e7fb836..8496b235 100644 --- a/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs +++ b/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs @@ -5,14 +5,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class ConstructorArgumentsShouldMatchAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1002"; + private const string Title = "Moq: No matching constructor"; + private const string Message = "Parameters provided into mock do not match any existing constructors"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.ConstructorArgumentsShouldMatchId, - Diagnostics.ConstructorArgumentsShouldMatchTitle, - Diagnostics.ConstructorArgumentsShouldMatchMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.ConstructorArgumentsShouldMatchId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/DiagnosticCategory.cs b/Source/Moq.Analyzers/DiagnosticCategory.cs new file mode 100644 index 00000000..b80e27bf --- /dev/null +++ b/Source/Moq.Analyzers/DiagnosticCategory.cs @@ -0,0 +1,6 @@ +namespace Moq.Analyzers; + +internal static class DiagnosticCategory +{ + public static string Moq { get; } = "Moq"; +} diff --git a/Source/Moq.Analyzers/Diagnostics.cs b/Source/Moq.Analyzers/Diagnostics.cs deleted file mode 100644 index 666d0f98..00000000 --- a/Source/Moq.Analyzers/Diagnostics.cs +++ /dev/null @@ -1,38 +0,0 @@ -namespace Moq.Analyzers; - -internal static class Diagnostics -{ - internal const string Category = "Moq"; - - internal const string NoSealedClassMocksId = "Moq1000"; - internal const string NoSealedClassMocksTitle = "Moq: Sealed class mocked"; - internal const string NoSealedClassMocksMessage = "Sealed classes cannot be mocked"; - - internal const string NoConstructorArgumentsForInterfaceMockId = "Moq1001"; - internal const string NoConstructorArgumentsForInterfaceMockTitle = "Moq: Parameters specified for mocked interface"; - internal const string NoConstructorArgumentsForInterfaceMockMessage = "Mocked interfaces cannot have constructor parameters"; - - internal const string ConstructorArgumentsShouldMatchId = "Moq1002"; - internal const string ConstructorArgumentsShouldMatchTitle = "Moq: No matching constructor"; - internal const string ConstructorArgumentsShouldMatchMessage = "Parameters provided into mock do not match any existing constructors"; - - internal const string CallbackSignatureShouldMatchMockedMethodId = "Moq1100"; - internal const string CallbackSignatureShouldMatchMockedMethodTitle = "Moq: Bad callback parameters"; - internal const string CallbackSignatureShouldMatchMockedMethodMessage = "Callback signature must match the signature of the mocked method"; - - internal const string NoMethodsInPropertySetupId = "Moq1101"; - internal const string NoMethodsInPropertySetupTitle = "Moq: Property setup used for a method"; - internal const string NoMethodsInPropertySetupMessage = "SetupGet/SetupSet should be used for properties, not for methods"; - - internal const string SetupShouldBeUsedOnlyForOverridableMembersId = "Moq1200"; - internal const string SetupShouldBeUsedOnlyForOverridableMembersTitle = "Moq: Invalid setup parameter"; - internal const string SetupShouldBeUsedOnlyForOverridableMembersMessage = "Setup should be used only for overridable members"; - - internal const string SetupShouldNotIncludeAsyncResultId = "Moq1201"; - internal const string SetupShouldNotIncludeAsyncResultTitle = SetupShouldBeUsedOnlyForOverridableMembersTitle; - internal const string SetupShouldNotIncludeAsyncResultMessage = "Setup of async methods should use ReturnsAsync instead of .Result"; - - internal const string AsShouldBeUsedOnlyForInterfaceId = "Moq1300"; - internal const string AsShouldBeUsedOnlyForInterfaceTitle = "Moq: Invalid As type parameter"; - internal const string AsShouldBeUsedOnlyForInterfaceMessage = "Mock.As() should take interfaces only"; -} diff --git a/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs b/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs index 46da7c41..591e3391 100644 --- a/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs +++ b/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs @@ -5,14 +5,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class NoConstructorArgumentsForInterfaceMockAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1001"; + private const string Title = "Moq: Parameters specified for mocked interface"; + private const string Message = "Mocked interfaces cannot have constructor parameters"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.NoConstructorArgumentsForInterfaceMockId, - Diagnostics.NoConstructorArgumentsForInterfaceMockTitle, - Diagnostics.NoConstructorArgumentsForInterfaceMockMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.NoConstructorArgumentsForInterfaceMockId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs b/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs index 1c870bee..871d4915 100644 --- a/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs +++ b/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class NoMethodsInPropertySetupAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1101"; + private const string Title = "Moq: Property setup used for a method"; + private const string Message = "SetupGet/SetupSet should be used for properties, not for methods"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.NoMethodsInPropertySetupId, - Diagnostics.NoMethodsInPropertySetupTitle, - Diagnostics.NoMethodsInPropertySetupMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.NoMethodsInPropertySetupId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs b/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs index 6f252d5d..4e3a74b8 100644 --- a/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs +++ b/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class NoSealedClassMocksAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1000"; + private const string Title = "Moq: Sealed class mocked"; + private const string Message = "Sealed classes cannot be mocked"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.NoSealedClassMocksId, - Diagnostics.NoSealedClassMocksTitle, - Diagnostics.NoSealedClassMocksMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.NoSealedClassMocksId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs b/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs index 8d24a676..f6bec1cd 100644 --- a/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs +++ b/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class SetupShouldBeUsedOnlyForOverridableMembersAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1200"; + private const string Title = "Moq: Invalid setup parameter"; + private const string Message = "Setup should be used only for overridable members"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersId, - Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersTitle, - Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Error, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); diff --git a/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs b/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs index 0cf4b111..e0c0606c 100644 --- a/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs +++ b/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs @@ -3,14 +3,18 @@ [DiagnosticAnalyzer(LanguageNames.CSharp)] public class SetupShouldNotIncludeAsyncResultAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1201"; + private const string Title = "Moq: Invalid setup parameter"; + private const string Message = "Setup of async methods should use ReturnsAsync instead of .Result"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.SetupShouldNotIncludeAsyncResultId, - Diagnostics.SetupShouldNotIncludeAsyncResultTitle, - Diagnostics.SetupShouldNotIncludeAsyncResultMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Error, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.SetupShouldNotIncludeAsyncResultId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule);