13
13
namespace MSTest . Analyzers ;
14
14
15
15
/// <summary>
16
- /// MSTEST0011 : <inheritdoc cref="Resources.ClassCleanupShouldBeValidTitle "/>.
16
+ /// MSTEST0034 : <inheritdoc cref="Resources.SetClassCleanupBehaviorTitle "/>.
17
17
/// </summary>
18
18
[ DiagnosticAnalyzer ( LanguageNames . CSharp , LanguageNames . VisualBasic ) ]
19
19
public sealed class SetClassCleanupBehaviorAnalyzer : DiagnosticAnalyzer
20
20
{
21
-
22
-
23
21
private static readonly LocalizableResourceString Title = new ( nameof ( Resources . SetClassCleanupBehaviorTitle ) , Resources . ResourceManager , typeof ( Resources ) ) ;
24
22
private static readonly LocalizableResourceString Description = new ( nameof ( Resources . SetClassCleanupBehaviorDescription ) , Resources . ResourceManager , typeof ( Resources ) ) ;
25
23
private static readonly LocalizableResourceString MessageFormat = new ( nameof ( Resources . SetClassCleanupBehaviorMessageFormat ) , Resources . ResourceManager , typeof ( Resources ) ) ;
@@ -44,16 +42,17 @@ public override void Initialize(AnalysisContext context)
44
42
context . RegisterCompilationStartAction ( context =>
45
43
{
46
44
if ( context . Compilation . TryGetOrCreateTypeByMetadataName ( WellKnownTypeNames . MicrosoftVisualStudioTestToolsUnitTestingClassCleanupAttribute , out INamedTypeSymbol ? classCleanupAttributeSymbol )
47
- && context . Compilation . TryGetOrCreateTypeByMetadataName ( WellKnownTypeNames . MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute , out INamedTypeSymbol ? testClassAttributeSymbol ) )
45
+ && context . Compilation . TryGetOrCreateTypeByMetadataName ( WellKnownTypeNames . MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute , out INamedTypeSymbol ? testClassAttributeSymbol )
46
+ && context . Compilation . TryGetOrCreateTypeByMetadataName ( WellKnownTypeNames . MicrosoftVisualStudioTestToolsUnitTestingClassCleanupBehavior , out INamedTypeSymbol ? classCleanupBehaviorSymbol ) )
48
47
{
49
48
context . RegisterSymbolAction (
50
- context => AnalyzeSymbol ( context , classCleanupAttributeSymbol , testClassAttributeSymbol ) ,
49
+ context => AnalyzeSymbol ( context , classCleanupAttributeSymbol , testClassAttributeSymbol , classCleanupBehaviorSymbol ) ,
51
50
SymbolKind . Method ) ;
52
51
}
53
52
} ) ;
54
53
}
55
54
56
- private static void AnalyzeSymbol ( SymbolAnalysisContext context , INamedTypeSymbol classCleanupAttributeSymbol , INamedTypeSymbol testClassAttributeSymbol )
55
+ private static void AnalyzeSymbol ( SymbolAnalysisContext context , INamedTypeSymbol classCleanupAttributeSymbol , INamedTypeSymbol testClassAttributeSymbol , INamedTypeSymbol classCleanupBehaviorSymbol )
57
56
{
58
57
var methodSymbol = ( IMethodSymbol ) context . Symbol ;
59
58
@@ -65,17 +64,19 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
65
64
66
65
ImmutableArray < AttributeData > methodAttributes = methodSymbol . GetAttributes ( ) ;
67
66
bool hasCleanupAttr = false ;
67
+ bool hasCleanupBehavior = false ;
68
68
foreach ( AttributeData methodAttribute in methodAttributes )
69
69
{
70
70
if ( SymbolEqualityComparer . Default . Equals ( methodAttribute . AttributeClass , classCleanupAttributeSymbol ) )
71
71
{
72
72
hasCleanupAttr = true ;
73
+ hasCleanupBehavior = methodAttribute . ConstructorArguments . Any ( arg => SymbolEqualityComparer . Default . Equals ( arg . Type , classCleanupBehaviorSymbol ) ) ;
73
74
}
74
75
}
75
76
76
- if ( hasCleanupAttr )
77
+ if ( hasCleanupAttr && ! hasCleanupBehavior )
77
78
{
78
- context . ReportDiagnostic ( methodSymbol . CreateDiagnostic ( SetClassCleanupBehaviorRule , methodSymbol . Name ) ) ;
79
+ context . ReportDiagnostic ( methodSymbol . CreateDiagnostic ( SetClassCleanupBehaviorRule ) ) ;
79
80
}
80
81
}
81
82
}
0 commit comments