|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.CSharp; |
| 6 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 7 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | +using System; |
| 9 | +using System.Collections.Immutable; |
| 10 | +using System.Linq; |
| 11 | + |
| 12 | +namespace Epi.Source.Updater |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Analyzer for identifying and removing obsolet types or methods. |
| 16 | + /// </summary> |
| 17 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 18 | + public sealed class EpiPartialRouterAnalyzer : EpiSubTypeAnalyzer |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// The diagnostic ID for diagnostics produced by this analyzer. |
| 22 | + /// </summary> |
| 23 | + public const string DiagnosticId = "EP0009"; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The diagnsotic category for diagnostics produced by this analyzer. |
| 27 | + /// </summary> |
| 28 | + private const string Category = "Upgrade"; |
| 29 | + |
| 30 | + |
| 31 | + private static readonly string MethodName = "RoutePartial"; |
| 32 | + private static readonly string RegistrationMethod = "RegisterPartialRouter"; |
| 33 | + private static readonly string[] BaseTypes = new[] { "IPartialRouter" }; |
| 34 | + |
| 35 | + private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.EpiPartialRouterTitle), Resources.ResourceManager, typeof(Resources)); |
| 36 | + private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.EpiPartialRouterMessageFormat), Resources.ResourceManager, typeof(Resources)); |
| 37 | + private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.EpiPartialRouterDescription), Resources.ResourceManager, typeof(Resources)); |
| 38 | + |
| 39 | + private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); |
| 40 | + |
| 41 | + public EpiPartialRouterAnalyzer() : base(BaseTypes) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 46 | + |
| 47 | + public override void Initialize(AnalysisContext context) |
| 48 | + { |
| 49 | + if (context is null) |
| 50 | + { |
| 51 | + throw new ArgumentNullException(nameof(context)); |
| 52 | + } |
| 53 | + |
| 54 | + context.EnableConcurrentExecution(); |
| 55 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); |
| 56 | + |
| 57 | + context.RegisterSyntaxNodeAction(AnalyzeIfInterfaceMethod, SyntaxKind.MethodDeclaration); |
| 58 | + context.RegisterCompilationStartAction(compilationContext => |
| 59 | + { |
| 60 | + compilationContext.RegisterSyntaxNodeAction(AnalyzePartialRouteRegistration, SyntaxKind.InvocationExpression); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private void AnalyzeIfInterfaceMethod(SyntaxNodeAnalysisContext context) |
| 65 | + { |
| 66 | + var methodDirective = (MethodDeclarationSyntax)context.Node; |
| 67 | + |
| 68 | + var namespaceName = methodDirective.Identifier.Text?.ToString(); |
| 69 | + |
| 70 | + if (namespaceName is null) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (namespaceName.Equals(MethodName, StringComparison.Ordinal)) |
| 76 | + { |
| 77 | + var parameters = methodDirective.ParameterList.Parameters; |
| 78 | + if (IsSubType(methodDirective.Parent as ClassDeclarationSyntax)) |
| 79 | + { |
| 80 | + var diagnostic = Diagnostic.Create(Rule, methodDirective.Parent.GetLocation(), methodDirective.ToFullString()); |
| 81 | + context.ReportDiagnostic(diagnostic); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void AnalyzePartialRouteRegistration(SyntaxNodeAnalysisContext context) |
| 87 | + { |
| 88 | + var memberExpression = (context.Node as InvocationExpressionSyntax)?.Expression as MemberAccessExpressionSyntax; |
| 89 | + if (memberExpression is null) |
| 90 | + { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // If the accessed member isn't named "RegisterPartialRouter" bail out |
| 95 | + if (!RegistrationMethod.Equals(memberExpression.Name.Identifier.Text)) |
| 96 | + { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + //If we have already acted by adding a comment then we should not report |
| 101 | + if (!memberExpression.GetLeadingTrivia().Any(t => t.Kind() == SyntaxKind.SingleLineCommentTrivia && t.ToString().StartsWith("//Partial router should be registered in ioc container"))) |
| 102 | + { |
| 103 | + var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation()); |
| 104 | + context.ReportDiagnostic(diagnostic); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments