-
Notifications
You must be signed in to change notification settings - Fork 468
/
RelaxTestNamingSuppressor.cs
60 lines (49 loc) · 2.71 KB
/
RelaxTestNamingSuppressor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System.Collections.Concurrent;
using System.Collections.Immutable;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Roslyn.Diagnostics.Analyzers
{
using static RoslynDiagnosticsAnalyzersResources;
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class RelaxTestNamingSuppressor : DiagnosticSuppressor
{
private const string Id = RoslynDiagnosticIds.RelaxTestNamingSuppressionRuleId;
// VSTHRD200: Use Async suffix for async methods
// https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD200.md
private const string SuppressedDiagnosticId = "VSTHRD200";
internal static readonly SuppressionDescriptor Rule =
new(Id, SuppressedDiagnosticId, CreateLocalizableResourceString(nameof(RelaxTestNamingSuppressorJustification)));
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions { get; } = ImmutableArray.Create(Rule);
public override void ReportSuppressions(SuppressionAnalysisContext context)
{
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.XunitFactAttribute, out var factAttribute);
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.BenchmarkDotNetAttributesBenchmarkAttribute, out var benchmarkAttribute);
if (factAttribute is null && benchmarkAttribute is null)
{
return;
}
var knownTestAttributes = new ConcurrentDictionary<INamedTypeSymbol, bool>();
foreach (var diagnostic in context.ReportedDiagnostics)
{
// The diagnostic is reported on the test method
if (diagnostic.Location.SourceTree is not { } tree)
{
continue;
}
var root = tree.GetRoot(context.CancellationToken);
var node = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
var semanticModel = context.GetSemanticModel(tree);
var declaredSymbol = semanticModel.GetDeclaredSymbol(node, context.CancellationToken);
if (declaredSymbol is IMethodSymbol method
&& method.IsBenchmarkOrXUnitTestMethod(knownTestAttributes, benchmarkAttribute, factAttribute))
{
context.ReportSuppression(Suppression.Create(Rule, diagnostic));
}
}
}
}
}