|
| 1 | +namespace OpenStackNetAnalyzers |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Immutable; |
| 5 | + using System.Linq; |
| 6 | + using Microsoft.CodeAnalysis; |
| 7 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | + |
| 9 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 10 | + public class ServiceMethodPrepareAsyncAnalyzer : DiagnosticAnalyzer |
| 11 | + { |
| 12 | + public const string DiagnosticId = "ServiceMethodPrepareAsync"; |
| 13 | + internal const string Title = "Service methods should be named Prepare{Name}Async"; |
| 14 | + internal const string MessageFormat = "Service methods should be named Prepare{Name}Async"; |
| 15 | + internal const string Category = "OpenStack.Maintainability"; |
| 16 | + internal const string Description = "Service methods should be named Prepare{Name}Async"; |
| 17 | + |
| 18 | + private static DiagnosticDescriptor Descriptor = |
| 19 | + new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); |
| 20 | + |
| 21 | + private static readonly ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics = |
| 22 | + ImmutableArray.Create(Descriptor); |
| 23 | + |
| 24 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 25 | + { |
| 26 | + get |
| 27 | + { |
| 28 | + return _supportedDiagnostics; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public override void Initialize(AnalysisContext context) |
| 33 | + { |
| 34 | + context.RegisterSymbolAction(HandleNamedType, SymbolKind.NamedType); |
| 35 | + } |
| 36 | + |
| 37 | + private void HandleNamedType(SymbolAnalysisContext context) |
| 38 | + { |
| 39 | + INamedTypeSymbol symbol = (INamedTypeSymbol)context.Symbol; |
| 40 | + if (!symbol.IsHttpServiceInterface()) |
| 41 | + return; |
| 42 | + |
| 43 | + foreach (IMethodSymbol method in symbol.GetMembers().OfType<IMethodSymbol>()) |
| 44 | + { |
| 45 | + if (string.IsNullOrEmpty(method.Name)) |
| 46 | + continue; |
| 47 | + |
| 48 | + if (method.Name.StartsWith("Prepare", StringComparison.Ordinal) && method.Name.EndsWith("Async", StringComparison.Ordinal)) |
| 49 | + { |
| 50 | + // TODO check letter following 'Prepare' |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + ImmutableArray<Location> locations = method.Locations; |
| 55 | + context.ReportDiagnostic(Diagnostic.Create(Descriptor, locations.FirstOrDefault(), locations.Skip(1))); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments