mirror of
https://github.com/godotengine/godot.git
synced 2025-11-01 06:01:14 +00:00
C#: Add a Roslyn analyzer for global classes
Co-Authored-By: Raul Santos <raulsntos@gmail.com>
This commit is contained in:
parent
cdd2313ba2
commit
8e56c807cc
3 changed files with 104 additions and 2 deletions
|
|
@ -0,0 +1,42 @@
|
|||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
|
||||
namespace Godot.SourceGenerators
|
||||
{
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public 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 void AnalyzeNode(SyntaxNodeAnalysisContext context)
|
||||
{
|
||||
var typeClassDecl = (ClassDeclarationSyntax)context.Node;
|
||||
|
||||
// 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)
|
||||
Common.ReportGlobalClassMustNotBeGeneric(context, typeClassDecl, typeSymbol);
|
||||
|
||||
if (!typeSymbol.InheritsFrom("GodotSharp", GodotClasses.GodotObject))
|
||||
Common.ReportGlobalClassMustDeriveFromGodotObject(context, typeClassDecl, typeSymbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue