Fix GD0107 not applying to arrays and dictionaries containing nodes

This commit is contained in:
Juan Pablo Arce 2024-07-21 17:07:29 -03:00
parent 4e5ed0bbfb
commit 8aa444d212
3 changed files with 99 additions and 19 deletions

View file

@ -196,16 +196,13 @@ namespace Godot.SourceGenerators
continue;
}
if (marshalType == MarshalType.GodotObjectOrDerived)
if (!isNode && MemberHasNodeType(propertyType, marshalType.Value))
{
if (!isNode && propertyType.InheritsFrom("GodotSharp", GodotClasses.Node))
{
context.ReportDiagnostic(Diagnostic.Create(
Common.OnlyNodesShouldExportNodesRule,
property.Locations.FirstLocationWithSourceTreeOrDefault()
));
continue;
}
context.ReportDiagnostic(Diagnostic.Create(
Common.OnlyNodesShouldExportNodesRule,
property.Locations.FirstLocationWithSourceTreeOrDefault()
));
continue;
}
var propertyDeclarationSyntax = property.DeclaringSyntaxReferences
@ -315,16 +312,13 @@ namespace Godot.SourceGenerators
continue;
}
if (marshalType == MarshalType.GodotObjectOrDerived)
if (!isNode && MemberHasNodeType(fieldType, marshalType.Value))
{
if (!isNode && fieldType.InheritsFrom("GodotSharp", GodotClasses.Node))
{
context.ReportDiagnostic(Diagnostic.Create(
Common.OnlyNodesShouldExportNodesRule,
field.Locations.FirstLocationWithSourceTreeOrDefault()
));
continue;
}
context.ReportDiagnostic(Diagnostic.Create(
Common.OnlyNodesShouldExportNodesRule,
field.Locations.FirstLocationWithSourceTreeOrDefault()
));
continue;
}
EqualsValueClauseSyntax? initializer = field.DeclaringSyntaxReferences
@ -424,6 +418,27 @@ namespace Godot.SourceGenerators
context.AddSource(uniqueHint, SourceText.From(source.ToString(), Encoding.UTF8));
}
private static bool MemberHasNodeType(ITypeSymbol memberType, MarshalType marshalType)
{
if (marshalType == MarshalType.GodotObjectOrDerived)
{
return memberType.InheritsFrom("GodotSharp", GodotClasses.Node);
}
if (marshalType == MarshalType.GodotObjectOrDerivedArray)
{
var elementType = ((IArrayTypeSymbol)memberType).ElementType;
return elementType.InheritsFrom("GodotSharp", GodotClasses.Node);
}
if (memberType is INamedTypeSymbol { IsGenericType: true } genericType)
{
return genericType.TypeArguments
.Any(static typeArgument
=> typeArgument.InheritsFrom("GodotSharp", GodotClasses.Node));
}
return false;
}
private struct ExportedPropertyMetadata
{
public ExportedPropertyMetadata(string name, MarshalType type, ITypeSymbol typeSymbol, string? value)