|
| 1 | +using System.Collections.Immutable; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | + |
| 7 | +namespace Bunit.Analyzers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Analyzer that detects cast expressions from Find() and suggests using Find{T}() instead. |
| 11 | +/// </summary> |
| 12 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 13 | +public class PreferGenericFindAnalyzer : DiagnosticAnalyzer |
| 14 | +{ |
| 15 | + /// <inheritdoc/> |
| 16 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DiagnosticDescriptors.PreferGenericFind); |
| 17 | + |
| 18 | + /// <inheritdoc/> |
| 19 | + public override void Initialize(AnalysisContext context) |
| 20 | + { |
| 21 | + if (context is null) |
| 22 | + { |
| 23 | + throw new System.ArgumentNullException(nameof(context)); |
| 24 | + } |
| 25 | + |
| 26 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 27 | + context.EnableConcurrentExecution(); |
| 28 | + |
| 29 | + context.RegisterSyntaxNodeAction(AnalyzeCastExpression, SyntaxKind.CastExpression); |
| 30 | + } |
| 31 | + |
| 32 | + private static void AnalyzeCastExpression(SyntaxNodeAnalysisContext context) |
| 33 | + { |
| 34 | + var castExpression = (CastExpressionSyntax)context.Node; |
| 35 | + |
| 36 | + // Check if the cast is on a Find() invocation |
| 37 | + if (castExpression.Expression is not InvocationExpressionSyntax invocation) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Check if it's a member access expression (e.g., cut.Find(...)) |
| 43 | + if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Check if the method name is "Find" |
| 49 | + if (memberAccess.Name.Identifier.ValueText != "Find") |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Get the method symbol to verify it's the bUnit Find method |
| 55 | + var methodSymbol = context.SemanticModel.GetSymbolInfo(memberAccess, context.CancellationToken).Symbol as IMethodSymbol; |
| 56 | + if (methodSymbol is null) |
| 57 | + { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Check if the method is from IRenderedFragment or related types |
| 62 | + var containingType = methodSymbol.ContainingType; |
| 63 | + if (containingType is null || !IsRenderedFragmentType(containingType)) |
| 64 | + { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Get the selector argument if present |
| 69 | + var selector = invocation.ArgumentList.Arguments.FirstOrDefault()?.Expression.ToString() ?? "selector"; |
| 70 | + |
| 71 | + // Get the cast type |
| 72 | + var castType = castExpression.Type.ToString(); |
| 73 | + |
| 74 | + var diagnostic = Diagnostic.Create( |
| 75 | + DiagnosticDescriptors.PreferGenericFind, |
| 76 | + castExpression.GetLocation(), |
| 77 | + castType, |
| 78 | + selector); |
| 79 | + |
| 80 | + context.ReportDiagnostic(diagnostic); |
| 81 | + } |
| 82 | + |
| 83 | + private static bool IsRenderedFragmentType(INamedTypeSymbol type) |
| 84 | + { |
| 85 | + // Check if the type or any of its interfaces is IRenderedFragment |
| 86 | + var typeName = type.Name; |
| 87 | + if (typeName is "IRenderedFragment" or "IRenderedComponent" or "RenderedFragment" or "RenderedComponent") |
| 88 | + { |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + foreach (var @interface in type.AllInterfaces) |
| 93 | + { |
| 94 | + if (@interface.Name is "IRenderedFragment" or "IRenderedComponent") |
| 95 | + { |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return false; |
| 101 | + } |
| 102 | +} |
0 commit comments