|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.Operations; |
| 3 | + |
| 4 | +namespace Moq.Analyzers; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Mock should explicitly specify Strict behavior. |
| 8 | +/// </summary> |
| 9 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 10 | +public class SetStrictMockBehaviorAnalyzer : MockBehaviorDiagnosticAnalyzerBase |
| 11 | +{ |
| 12 | + private static readonly LocalizableString Title = "Moq: Set MockBehavior to Strict"; |
| 13 | + private static readonly LocalizableString Message = "Explicitly set the Strict mocking behavior"; |
| 14 | + |
| 15 | + private static readonly DiagnosticDescriptor Rule = new( |
| 16 | + DiagnosticIds.SetStrictMockBehavior, |
| 17 | + Title, |
| 18 | + Message, |
| 19 | + DiagnosticCategory.Moq, |
| 20 | + DiagnosticSeverity.Info, |
| 21 | + isEnabledByDefault: true, |
| 22 | + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/{ThisAssembly.GitCommitId}/docs/rules/{DiagnosticIds.SetStrictMockBehavior}.md"); |
| 23 | + |
| 24 | + /// <inheritdoc /> |
| 25 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); |
| 26 | + |
| 27 | + /// <inheritdoc /> |
| 28 | + [SuppressMessage("Design", "MA0051:Method is too long", Justification = "Should be fixed. Ignoring for now to avoid additional churn as part of larger refactor.")] |
| 29 | + internal override void AnalyzeCore(OperationAnalysisContext context, IMethodSymbol target, ImmutableArray<IArgumentOperation> arguments, MoqKnownSymbols knownSymbols) |
| 30 | + { |
| 31 | + // Check if the target method has a parameter of type MockBehavior |
| 32 | + IParameterSymbol? mockParameter = target.Parameters.DefaultIfNotSingle(parameter => parameter.Type.IsInstanceOf(knownSymbols.MockBehavior)); |
| 33 | + |
| 34 | + // If the target method doesn't have a MockBehavior parameter, check if there's an overload that does |
| 35 | + if (mockParameter is null && target.TryGetOverloadWithParameterOfType(knownSymbols.MockBehavior!, out IMethodSymbol? methodMatch, out _, cancellationToken: context.CancellationToken)) |
| 36 | + { |
| 37 | + if (!methodMatch.TryGetParameterOfType(knownSymbols.MockBehavior!, out IParameterSymbol? parameterMatch, cancellationToken: context.CancellationToken)) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + ImmutableDictionary<string, string?> properties = new DiagnosticEditProperties |
| 43 | + { |
| 44 | + TypeOfEdit = DiagnosticEditProperties.EditType.Insert, |
| 45 | + EditPosition = parameterMatch.Ordinal, |
| 46 | + }.ToImmutableDictionary(); |
| 47 | + |
| 48 | + // Using a method that doesn't accept a MockBehavior parameter, however there's an overload that does |
| 49 | + context.ReportDiagnostic(context.Operation.CreateDiagnostic(Rule, properties)); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + IArgumentOperation? mockArgument = arguments.DefaultIfNotSingle(argument => argument.Parameter.IsInstanceOf(mockParameter)); |
| 54 | + |
| 55 | + // Is the behavior set via a default value? |
| 56 | + if (mockArgument?.ArgumentKind == ArgumentKind.DefaultValue && mockArgument.Value.WalkDownConversion().ConstantValue.Value == knownSymbols.MockBehaviorDefault?.ConstantValue) |
| 57 | + { |
| 58 | + if (!target.TryGetParameterOfType(knownSymbols.MockBehavior!, out IParameterSymbol? parameterMatch, cancellationToken: context.CancellationToken)) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + ImmutableDictionary<string, string?> properties = new DiagnosticEditProperties |
| 64 | + { |
| 65 | + TypeOfEdit = DiagnosticEditProperties.EditType.Insert, |
| 66 | + EditPosition = parameterMatch.Ordinal, |
| 67 | + }.ToImmutableDictionary(); |
| 68 | + |
| 69 | + context.ReportDiagnostic(context.Operation.CreateDiagnostic(Rule, properties)); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // NOTE: This logic can't handle indirection (e.g. var x = MockBehavior.Default; new Mock(x);) |
| 74 | + // |
| 75 | + // The operation specifies a MockBehavior; is it MockBehavior.Strict? |
| 76 | + if (mockArgument?.Value.WalkDownConversion().ConstantValue.Value != knownSymbols.MockBehaviorStrict?.ConstantValue |
| 77 | + && mockArgument?.DescendantsAndSelf().OfType<IFieldReferenceOperation>().Any(argument => argument.Member.IsInstanceOf(knownSymbols.MockBehaviorStrict)) != true) |
| 78 | + { |
| 79 | + if (!target.TryGetParameterOfType(knownSymbols.MockBehavior!, out IParameterSymbol? parameterMatch, cancellationToken: context.CancellationToken)) |
| 80 | + { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + ImmutableDictionary<string, string?> properties = new DiagnosticEditProperties |
| 85 | + { |
| 86 | + TypeOfEdit = DiagnosticEditProperties.EditType.Replace, |
| 87 | + EditPosition = parameterMatch.Ordinal, |
| 88 | + }.ToImmutableDictionary(); |
| 89 | + |
| 90 | + context.ReportDiagnostic(context.Operation.CreateDiagnostic(Rule, properties)); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments