Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Report diagnostic for Node exports in a type that doesn't derive from Node #82918

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,31 @@ ISymbol exportedMemberSymbol
location?.SourceTree?.FilePath));
}

public static void ReportOnlyNodesShouldExportNodes(
GeneratorExecutionContext context,
ISymbol exportedMemberSymbol
)
{
var locations = exportedMemberSymbol.Locations;
var location = locations.FirstOrDefault(l => l.SourceTree != null) ?? locations.FirstOrDefault();
bool isField = exportedMemberSymbol is IFieldSymbol;

string message = $"Types not derived from Node should not export Node {(isField ? "fields" : "properties")}";

string description = $"{message}. Node export is only supported in Node-derived classes.";

context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(id: "GD0107",
title: message,
messageFormat: message,
category: "Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description),
location,
location?.SourceTree?.FilePath));
}

public static void ReportSignalDelegateMissingSuffix(
GeneratorExecutionContext context,
INamedTypeSymbol delegateSymbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static bool IsGodotSourceGeneratorDisabled(this GeneratorExecutionContext
disabledGenerators != null &&
disabledGenerators.Split(';').Contains(generatorName));

public static bool InheritsFrom(this INamedTypeSymbol? symbol, string assemblyName, string typeFullName)
public static bool InheritsFrom(this ITypeSymbol? symbol, string assemblyName, string typeFullName)
{
while (symbol != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ INamedTypeSymbol symbol
continue;
}

if (marshalType == MarshalType.GodotObjectOrDerived)
{
if (!symbol.InheritsFrom("GodotSharp", "Godot.Node") &&
propertyType.InheritsFrom("GodotSharp", "Godot.Node"))
{
Common.ReportOnlyNodesShouldExportNodes(context, property);
}
}

var propertyDeclarationSyntax = property.DeclaringSyntaxReferences
.Select(r => r.GetSyntax() as PropertyDeclarationSyntax).FirstOrDefault();

Expand Down Expand Up @@ -261,6 +270,15 @@ INamedTypeSymbol symbol
continue;
}

if (marshalType == MarshalType.GodotObjectOrDerived)
{
if (!symbol.InheritsFrom("GodotSharp", "Godot.Node") &&
fieldType.InheritsFrom("GodotSharp", "Godot.Node"))
{
Common.ReportOnlyNodesShouldExportNodes(context, field);
}
}

EqualsValueClauseSyntax? initializer = field.DeclaringSyntaxReferences
.Select(r => r.GetSyntax())
.OfType<VariableDeclaratorSyntax>()
Expand Down