|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="CallBackDelegateParametersShouldNotBeIgnoredAnalyzer.cs" company="P.O.S Informatique"> |
| 3 | +// Copyright (c) P.O.S Informatique. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//----------------------------------------------------------------------- |
| 6 | + |
| 7 | +namespace PosInformatique.Moq.Analyzers |
| 8 | +{ |
| 9 | + using System.Collections.Immutable; |
| 10 | + using Microsoft.CodeAnalysis; |
| 11 | + using Microsoft.CodeAnalysis.CSharp; |
| 12 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 14 | + |
| 15 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 16 | + public class CallBackDelegateParametersShouldNotBeIgnoredAnalyzer : DiagnosticAnalyzer |
| 17 | + { |
| 18 | + internal static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
| 19 | + "PosInfoMoq1004", |
| 20 | + "The Callback() parameter should not be ignored if it has been setup as an It.IsAny<T>() argument", |
| 21 | + "The '{0}' parameter should not be ignored if it has been setup as an It.IsAny<T>() argument", |
| 22 | + "Design", |
| 23 | + DiagnosticSeverity.Warning, |
| 24 | + isEnabledByDefault: true, |
| 25 | + description: "The Callback() parameter should not be ignored if it has been setup as an It.IsAny<T>() argument.", |
| 26 | + helpLinkUri: "https://posinformatique.github.io/PosInformatique.Moq.Analyzers/docs/Design/PosInfoMoq1004.html"); |
| 27 | + |
| 28 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 29 | + |
| 30 | + public override void Initialize(AnalysisContext context) |
| 31 | + { |
| 32 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 33 | + context.EnableConcurrentExecution(); |
| 34 | + |
| 35 | + context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.InvocationExpression); |
| 36 | + } |
| 37 | + |
| 38 | + private static void Analyze(SyntaxNodeAnalysisContext context) |
| 39 | + { |
| 40 | + var invocationExpression = (InvocationExpressionSyntax)context.Node; |
| 41 | + |
| 42 | + var moqSymbols = MoqSymbols.FromCompilation(context.Compilation); |
| 43 | + |
| 44 | + if (moqSymbols is null) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + var moqExpressionAnalyzer = new MoqExpressionAnalyzer(moqSymbols, context.SemanticModel); |
| 50 | + |
| 51 | + // Extracts the Setup() linked method. |
| 52 | + var setupMethod = moqExpressionAnalyzer.ExtractSetupMethod(invocationExpression, context.CancellationToken); |
| 53 | + |
| 54 | + if (setupMethod is null) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Check there is It.Any<T>() parameters. |
| 60 | + var itIsAnyArguments = new Dictionary<int, ChainInvocationArgument>(); |
| 61 | + |
| 62 | + for (var i = 0; i < setupMethod.InvocationArguments.Count; i++) |
| 63 | + { |
| 64 | + var argument = setupMethod.InvocationArguments[i]; |
| 65 | + |
| 66 | + if (moqSymbols.IsItIsAny(argument.Symbol)) |
| 67 | + { |
| 68 | + // The Callback() method is required for the argument, add in the list. |
| 69 | + itIsAnyArguments.Add(i, argument); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (itIsAnyArguments.Any()) |
| 74 | + { |
| 75 | + // Here, it is mean Setup() method has been defined with some It.IsAny<T>() parameters. |
| 76 | + // Extracts the Callback() method (if exists). |
| 77 | + var callbackMethod = moqExpressionAnalyzer.ExtractCallBackLambdaExpressionMethod(invocationExpression, out var lambdaExpression, context.CancellationToken); |
| 78 | + |
| 79 | + if (callbackMethod is null) |
| 80 | + { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Check each parameter of the Callback() method. |
| 85 | + for (var i = 0; i < callbackMethod.Parameters.Length; i++) |
| 86 | + { |
| 87 | + if (!itIsAnyArguments.TryGetValue(i, out var itIsAnyArgument)) |
| 88 | + { |
| 89 | + // The parameter in the Callback() method is not related to a It.IsAny<T>() expression. |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (callbackMethod.Parameters[i].Name == "_") |
| 94 | + { |
| 95 | + // Raise warning for the parameter which is not used. |
| 96 | + var parameterName = setupMethod.InvocationArguments[i].ParameterSymbol.Name; |
| 97 | + |
| 98 | + context.ReportDiagnostic(Rule, lambdaExpression!.ParameterList.Parameters[i].GetLocation(), parameterName); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments