-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66257 from raulsntos/dotnet/suppress-CA1711
C#: Suppress EventHandler suffix diagnostic for signals
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/EventHandlerSuffixSuppressor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Godot.SourceGenerators | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class EventHandlerSuffixSuppressor : DiagnosticSuppressor | ||
{ | ||
private static readonly SuppressionDescriptor _descriptor = new( | ||
id: "GDSP0001", | ||
suppressedDiagnosticId: "CA1711", | ||
justification: "Signal delegates are used in events so the naming follows the guidelines."); | ||
|
||
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => | ||
ImmutableArray.Create(_descriptor); | ||
|
||
public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
{ | ||
foreach (var diagnostic in context.ReportedDiagnostics) | ||
{ | ||
AnalyzeDiagnostic(context, diagnostic, context.CancellationToken); | ||
} | ||
} | ||
|
||
private static void AnalyzeDiagnostic(SuppressionAnalysisContext context, Diagnostic diagnostic, CancellationToken cancellationToken = default) | ||
{ | ||
var location = diagnostic.Location; | ||
var root = location.SourceTree?.GetRoot(cancellationToken); | ||
var dds = root? | ||
.FindNode(location.SourceSpan) | ||
.DescendantNodesAndSelf() | ||
.OfType<DelegateDeclarationSyntax>() | ||
.FirstOrDefault(); | ||
|
||
if (dds == null) | ||
return; | ||
|
||
var semanticModel = context.GetSemanticModel(dds.SyntaxTree); | ||
var delegateSymbol = semanticModel.GetDeclaredSymbol(dds, cancellationToken); | ||
if (delegateSymbol == null) | ||
return; | ||
|
||
if (delegateSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotSignalAttribute() ?? false)) | ||
{ | ||
context.ReportSuppression(Suppression.Create(_descriptor, diagnostic)); | ||
} | ||
} | ||
} | ||
} |