godot/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GlobalClassAnalyzer.cs
Paul Joannon 139a5df821
Cleanup C# projects, code quality & style
New rules:
- Do not silence CA1805 any more
- Limit where we silence CA1707, CA1711, CA1720
- Enforce severity=warning for IDE0040
- Enforce Allman style braces
- Enforce naming conventions (IDE1006 is still severity=suggestion)

Fixes:
- Fix REFL045, CS1572, CS1573
- Suppress CS0618 when generating `InvokeGodotClassMethod`
- Fix indent when generating GD_constants.cs
- Temporarily silence CS1734 in generated code
- Fix a lot of naming rule violations

Misc.:
- Remove ReSharper comments for RedundantNameQualifier
- Remove suppression attributes for RedundantNameQualifier
- Remove severity=warnings for CA1716, CA1304 (already included in the level of analysis we run)
2024-02-27 20:11:24 +01:00

51 lines
1.9 KiB
C#

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Godot.SourceGenerators
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class GlobalClassAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(
Common.GlobalClassMustDeriveFromGodotObjectRule,
Common.GlobalClassMustNotBeGenericRule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration);
}
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
// Return if not a type symbol or the type is not a global class.
if (context.ContainingSymbol is not INamedTypeSymbol typeSymbol ||
!typeSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotGlobalClassAttribute() ?? false))
return;
if (typeSymbol.IsGenericType)
{
context.ReportDiagnostic(Diagnostic.Create(
Common.GlobalClassMustNotBeGenericRule,
typeSymbol.Locations.FirstLocationWithSourceTreeOrDefault(),
typeSymbol.ToDisplayString()
));
}
if (!typeSymbol.InheritsFrom("GodotSharp", GodotClasses.GodotObject))
{
context.ReportDiagnostic(Diagnostic.Create(
Common.GlobalClassMustDeriveFromGodotObjectRule,
typeSymbol.Locations.FirstLocationWithSourceTreeOrDefault(),
typeSymbol.ToDisplayString()
));
}
}
}
}