|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | + |
| 6 | +using Analyzer.Utilities.Extensions; |
| 7 | + |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 10 | + |
| 11 | +using MSTest.Analyzers.Helpers; |
| 12 | + |
| 13 | +namespace MSTest.Analyzers; |
| 14 | + |
| 15 | +[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] |
| 16 | +public sealed class TestCleanupShouldBeValidAnalyzer : DiagnosticAnalyzer |
| 17 | +{ |
| 18 | + private static readonly LocalizableResourceString Title = new(nameof(Resources.TestCleanupShouldBeValidTitle), Resources.ResourceManager, typeof(Resources)); |
| 19 | + private static readonly LocalizableResourceString Description = new(nameof(Resources.TestCleanupShouldBeValidDescription), Resources.ResourceManager, typeof(Resources)); |
| 20 | + private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_Public), Resources.ResourceManager, typeof(Resources)); |
| 21 | + |
| 22 | + internal static readonly DiagnosticDescriptor PublicRule = DiagnosticDescriptorHelper.Create( |
| 23 | + DiagnosticIds.TestCleanupShouldBeValidRuleId, |
| 24 | + Title, |
| 25 | + MessageFormat, |
| 26 | + Description, |
| 27 | + Category.Usage, |
| 28 | + DiagnosticSeverity.Warning, |
| 29 | + isEnabledByDefault: true); |
| 30 | + |
| 31 | + internal static readonly DiagnosticDescriptor NotStaticRule = PublicRule.WithMessage(new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_NotStatic), Resources.ResourceManager, typeof(Resources))); |
| 32 | + internal static readonly DiagnosticDescriptor NoParametersRule = PublicRule.WithMessage(new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_NoParameters), Resources.ResourceManager, typeof(Resources))); |
| 33 | + internal static readonly DiagnosticDescriptor ReturnTypeRule = PublicRule.WithMessage(new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_ReturnType), Resources.ResourceManager, typeof(Resources))); |
| 34 | + internal static readonly DiagnosticDescriptor NotAsyncVoidRule = PublicRule.WithMessage(new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_NotAsyncVoid), Resources.ResourceManager, typeof(Resources))); |
| 35 | + internal static readonly DiagnosticDescriptor NotAbstractRule = PublicRule.WithMessage(new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_NotAbstract), Resources.ResourceManager, typeof(Resources))); |
| 36 | + internal static readonly DiagnosticDescriptor NotGenericRule = PublicRule.WithMessage(new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_NotGeneric), Resources.ResourceManager, typeof(Resources))); |
| 37 | + internal static readonly DiagnosticDescriptor OrdinaryRule = PublicRule.WithMessage(new(nameof(Resources.TestCleanupShouldBeValidMessageFormat_Ordinary), Resources.ResourceManager, typeof(Resources))); |
| 38 | + |
| 39 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(PublicRule); |
| 40 | + |
| 41 | + public override void Initialize(AnalysisContext context) |
| 42 | + { |
| 43 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 44 | + context.EnableConcurrentExecution(); |
| 45 | + |
| 46 | + context.RegisterCompilationStartAction(context => |
| 47 | + { |
| 48 | + if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestCleanupAttribute, out var testCleanupAttributeSymbol)) |
| 49 | + { |
| 50 | + var taskSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask); |
| 51 | + var valueTaskSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksValueTask); |
| 52 | + context.RegisterSymbolAction( |
| 53 | + context => AnalyzeSymbol(context, testCleanupAttributeSymbol, taskSymbol, valueTaskSymbol), |
| 54 | + SymbolKind.Method); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testCleanupAttributeSymbol, INamedTypeSymbol? taskSymbol, |
| 60 | + INamedTypeSymbol? valueTaskSymbol) |
| 61 | + { |
| 62 | + var methodSymbol = (IMethodSymbol)context.Symbol; |
| 63 | + if (!methodSymbol.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, testCleanupAttributeSymbol))) |
| 64 | + { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (methodSymbol.MethodKind != MethodKind.Ordinary) |
| 69 | + { |
| 70 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(OrdinaryRule, methodSymbol.Name)); |
| 71 | + |
| 72 | + // Do not check the other criteria, users should fix the method kind first. |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (methodSymbol.IsAbstract) |
| 77 | + { |
| 78 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(NotAbstractRule, methodSymbol.Name)); |
| 79 | + } |
| 80 | + |
| 81 | + if (methodSymbol.IsGenericMethod) |
| 82 | + { |
| 83 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(NotGenericRule, methodSymbol.Name)); |
| 84 | + } |
| 85 | + |
| 86 | + if (methodSymbol.Parameters.Length > 0) |
| 87 | + { |
| 88 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(NoParametersRule, methodSymbol.Name)); |
| 89 | + } |
| 90 | + |
| 91 | + if (methodSymbol.IsStatic) |
| 92 | + { |
| 93 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(NotStaticRule, methodSymbol.Name)); |
| 94 | + } |
| 95 | + |
| 96 | + if (methodSymbol.ReturnsVoid && methodSymbol.IsAsync) |
| 97 | + { |
| 98 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(NotAsyncVoidRule, methodSymbol.Name)); |
| 99 | + } |
| 100 | + |
| 101 | + if (methodSymbol.DeclaredAccessibility != Accessibility.Public || methodSymbol.GetResultantVisibility() != SymbolVisibility.Public) |
| 102 | + { |
| 103 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(PublicRule, methodSymbol.Name)); |
| 104 | + } |
| 105 | + |
| 106 | + if (!methodSymbol.ReturnsVoid |
| 107 | + && (taskSymbol is null || !SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, taskSymbol)) |
| 108 | + && (valueTaskSymbol is null || !SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, valueTaskSymbol))) |
| 109 | + { |
| 110 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(ReturnTypeRule, methodSymbol.Name)); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments