|
| 1 | +namespace ReflectionAnalyzers |
| 2 | +{ |
| 3 | + using System.Collections.Immutable; |
| 4 | + using System.Linq; |
| 5 | + using Gu.Roslyn.AnalyzerExtensions; |
| 6 | + using Microsoft.CodeAnalysis; |
| 7 | + using Microsoft.CodeAnalysis.CSharp; |
| 8 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 9 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 10 | + |
| 11 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 12 | + internal class GetInterfaceAnalyzer : DiagnosticAnalyzer |
| 13 | + { |
| 14 | + /// <inheritdoc/> |
| 15 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create( |
| 16 | + REFL020AmbiguousMatchInterface.Descriptor); |
| 17 | + |
| 18 | + /// <inheritdoc/> |
| 19 | + public override void Initialize(AnalysisContext context) |
| 20 | + { |
| 21 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 22 | + context.EnableConcurrentExecution(); |
| 23 | + context.RegisterSyntaxNodeAction(Handle, SyntaxKind.InvocationExpression); |
| 24 | + } |
| 25 | + |
| 26 | + private static void Handle(SyntaxNodeAnalysisContext context) |
| 27 | + { |
| 28 | + if (!context.IsExcludedFromAnalysis() && |
| 29 | + context.Node is InvocationExpressionSyntax invocation && |
| 30 | + invocation.TryGetTarget(KnownSymbol.Type.GetInterface, context.SemanticModel, context.CancellationToken, out var getInterface) && |
| 31 | + getInterface.TryFindParameter(KnownSymbol.String, out var nameParameter) && |
| 32 | + invocation.TryFindArgument(nameParameter, out var nameArg) && |
| 33 | + TryGetName(nameArg, context, out var name) && |
| 34 | + GetX.TryGetType(invocation, context, out var type, out _)) |
| 35 | + { |
| 36 | + var count = type.AllInterfaces.Count(x => IsMatch(x)); |
| 37 | + if (count > 1) |
| 38 | + { |
| 39 | + context.ReportDiagnostic(Diagnostic.Create(REFL020AmbiguousMatchInterface.Descriptor, nameArg.GetLocation())); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + bool IsMatch(ITypeSymbol candidate) |
| 44 | + { |
| 45 | + if (candidate.MetadataName == name) |
| 46 | + { |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + return name.IsParts(candidate.ContainingNamespace.ToString(), ".", candidate.MetadataName); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static bool TryGetName(ArgumentSyntax nameArg, SyntaxNodeAnalysisContext context, out string name) |
| 55 | + { |
| 56 | + switch (nameArg.Expression) |
| 57 | + { |
| 58 | + case MemberAccessExpressionSyntax memberAccess: |
| 59 | + if (memberAccess.Expression is TypeOfExpressionSyntax typeOf && |
| 60 | + context.SemanticModel.TryGetType(typeOf.Type, context.CancellationToken, out var type)) |
| 61 | + { |
| 62 | + if (memberAccess.Name.Identifier.ValueText == "Name") |
| 63 | + { |
| 64 | + name = type.MetadataName; |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + if (memberAccess.Name.Identifier.ValueText == "FullName") |
| 69 | + { |
| 70 | + name = $"{type.ContainingNamespace}.{type.MetadataName}"; |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + name = null; |
| 76 | + return false; |
| 77 | + default: |
| 78 | + return context.SemanticModel.TryGetConstantValue(nameArg.Expression, context.CancellationToken, out name); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments