2023-07-04 02:35:54 +00:00
|
|
|
using System.Collections.Immutable;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
|
|
|
|
|
|
namespace Godot.SourceGenerators
|
|
|
|
{
|
|
|
|
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-17 20:12:06 +00:00
|
|
|
public sealed class GlobalClassAnalyzer : DiagnosticAnalyzer
|
2023-07-04 02:35:54 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-02-19 21:15:37 +00:00
|
|
|
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
|
2023-07-04 02:35:54 +00:00
|
|
|
{
|
|
|
|
// 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)
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-17 20:12:06 +00:00
|
|
|
{
|
|
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
|
|
Common.GlobalClassMustNotBeGenericRule,
|
|
|
|
typeSymbol.Locations.FirstLocationWithSourceTreeOrDefault(),
|
|
|
|
typeSymbol.ToDisplayString()
|
|
|
|
));
|
|
|
|
}
|
2023-07-04 02:35:54 +00:00
|
|
|
|
|
|
|
if (!typeSymbol.InheritsFrom("GodotSharp", GodotClasses.GodotObject))
|
Clean diagnostic rules
Move the following diagnostics into static readonly fields: GD0101, GD0102, GD0103, GD0104, GD0105, GD0106, GD0107, GD0201, GD0202, GD0203, GD0301, GD0302, GD0303, GD0401, GD0402.
To be more consistent, the titles for the following diagnostics were modified: GD0101, GD0105, GD0106, GD0302, GD0303, GD0401, GD0402. A subsequent update of the documentation repo is needed.
Tests for the following diagnostics were created: GD0201, GD0202, GD0203.
2024-02-17 20:12:06 +00:00
|
|
|
{
|
|
|
|
context.ReportDiagnostic(Diagnostic.Create(
|
|
|
|
Common.GlobalClassMustDeriveFromGodotObjectRule,
|
|
|
|
typeSymbol.Locations.FirstLocationWithSourceTreeOrDefault(),
|
|
|
|
typeSymbol.ToDisplayString()
|
|
|
|
));
|
|
|
|
}
|
2023-07-04 02:35:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|