|  | 
|  | 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 PreferConstructorOverTestInitializeAnalyzer : DiagnosticAnalyzer | 
|  | 17 | +{ | 
|  | 18 | +    private static readonly LocalizableResourceString Title = new(nameof(Resources.PreferConstructorOverTestInitializeTitle), Resources.ResourceManager, typeof(Resources)); | 
|  | 19 | +    private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.PreferConstructorOverTestInitializeMessageFormat), Resources.ResourceManager, typeof(Resources)); | 
|  | 20 | + | 
|  | 21 | +    internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( | 
|  | 22 | +        DiagnosticIds.PreferConstructorOverTestInitializeRuleId, | 
|  | 23 | +        Title, | 
|  | 24 | +        MessageFormat, | 
|  | 25 | +        null, | 
|  | 26 | +        Category.Design, | 
|  | 27 | +        DiagnosticSeverity.Info, | 
|  | 28 | +        isEnabledByDefault: false); | 
|  | 29 | + | 
|  | 30 | +    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | 
|  | 31 | +        = ImmutableArray.Create(Rule); | 
|  | 32 | + | 
|  | 33 | +    public override void Initialize(AnalysisContext context) | 
|  | 34 | +    { | 
|  | 35 | +        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | 
|  | 36 | +        context.EnableConcurrentExecution(); | 
|  | 37 | + | 
|  | 38 | +        context.RegisterCompilationStartAction(context => | 
|  | 39 | +        { | 
|  | 40 | +            if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestInitializeAttribute, out var testInitAttributeSymbol)) | 
|  | 41 | +            { | 
|  | 42 | +                context.RegisterSymbolAction(context => AnalyzeSymbol(context, testInitAttributeSymbol), SymbolKind.Method); | 
|  | 43 | +            } | 
|  | 44 | +        }); | 
|  | 45 | +    } | 
|  | 46 | + | 
|  | 47 | +    private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testInitAttributeSymbol) | 
|  | 48 | +    { | 
|  | 49 | +        IMethodSymbol methodSymbol = (IMethodSymbol)context.Symbol; | 
|  | 50 | + | 
|  | 51 | +        if (methodSymbol.IsTestInitializeMethod(testInitAttributeSymbol) && methodSymbol.ReturnsVoid) | 
|  | 52 | +        { | 
|  | 53 | +            context.ReportDiagnostic(methodSymbol.CreateDiagnostic(Rule)); | 
|  | 54 | +        } | 
|  | 55 | +    } | 
|  | 56 | +} | 
0 commit comments