| 1 |
using System.Collections.Immutable; |
| 2 |
using System.Linq; |
| 3 |
using System.Threading; |
| 4 |
using Microsoft.CodeAnalysis; |
| 5 |
using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 |
using Microsoft.CodeAnalysis.Diagnostics; |
| 7 |
|
| 8 |
namespace Godot.SourceGenerators |
| 9 |
{ |
| 10 |
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 11 |
public class EventHandlerSuffixSuppressor : DiagnosticSuppressor |
| 12 |
{ |
| 13 |
private static readonly SuppressionDescriptor _descriptor = new( |
| 14 |
id: "GDSP0001", |
| 15 |
suppressedDiagnosticId: "CA1711", |
| 16 |
justification: "Signal delegates are used in events so the naming follows the guidelines."); |
| 17 |
|
| 18 |
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => |
| 19 |
ImmutableArray.Create(_descriptor); |
| 20 |
|
| 21 |
public override void ReportSuppressions(SuppressionAnalysisContext context) |
| 22 |
{ |
| 23 |
foreach (var diagnostic in context.ReportedDiagnostics) |
| 24 |
{ |
| 25 |
AnalyzeDiagnostic(context, diagnostic, context.CancellationToken); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
private static void AnalyzeDiagnostic(SuppressionAnalysisContext context, Diagnostic diagnostic, CancellationToken cancellationToken = default) |
| 30 |
{ |
| 31 |
var location = diagnostic.Location; |
| 32 |
var root = location.SourceTree?.GetRoot(cancellationToken); |
| 33 |
var dds = root? |
| 34 |
.FindNode(location.SourceSpan) |
| 35 |
.DescendantNodesAndSelf() |
| 36 |
.OfType<DelegateDeclarationSyntax>() |
| 37 |
.FirstOrDefault(); |
| 38 |
|
| 39 |
if (dds == null) |
| 40 |
return; |
| 41 |
|
| 42 |
var semanticModel = context.GetSemanticModel(dds.SyntaxTree); |
| 43 |
var delegateSymbol = semanticModel.GetDeclaredSymbol(dds, cancellationToken); |
| 44 |
if (delegateSymbol == null) |
| 45 |
return; |
| 46 |
|
| 47 |
if (delegateSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotSignalAttribute() ?? false)) |
| 48 |
{ |
| 49 |
context.ReportSuppression(Suppression.Create(_descriptor, diagnostic)); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |