Add: ShouldBeNonSerialized DiagnosticDescriptor

This commit is contained in:
AnnulusGames 2024-02-16 23:38:29 +09:00
parent 0a74551727
commit fbde143664
3 changed files with 25 additions and 3 deletions

View File

@ -51,14 +51,27 @@ namespace Alchemy.SourceGenerator
foreach (var variable in field.Declaration.Variables) foreach (var variable in field.Declaration.Variables)
{ {
var fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol; var fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol;
var attribute = fieldSymbol.GetAttributes() var alchemySerializeAttribute = fieldSymbol.GetAttributes()
.FirstOrDefault(x => .FirstOrDefault(x =>
x.AttributeClass.Name is "AlchemySerializeField" x.AttributeClass.Name is "AlchemySerializeField"
or "AlchemySerializeFieldAttribute" or "AlchemySerializeFieldAttribute"
or "Alchemy.Serialization.AlchemySerializeField" or "Alchemy.Serialization.AlchemySerializeField"
or "Alchemy.Serialization.AlchemySerializeFieldAttribute"); or "Alchemy.Serialization.AlchemySerializeFieldAttribute");
if (attribute != null)
var nonSerializedAttribute = fieldSymbol.GetAttributes()
.FirstOrDefault(x =>
x.AttributeClass.Name is "NonSerialized"
or "NonSerializedAttribute"
or "System.NonSerialized"
or "System.NonSerializedAttribute");
if (alchemySerializeAttribute != null)
{ {
if (nonSerializedAttribute == null)
{
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.ShouldBeNonSerialized, variable.Identifier.GetLocation(), fieldSymbol.Name));
}
fieldSymbols.Add(fieldSymbol); fieldSymbols.Add(fieldSymbol);
} }
} }

View File

@ -21,7 +21,16 @@ namespace Alchemy.SourceGenerator
messageFormat: "The AlchemySerialize class '{0}' must be not nested type.", messageFormat: "The AlchemySerialize class '{0}' must be not nested type.",
category: Category, category: Category,
defaultSeverity: DiagnosticSeverity.Error, defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true); isEnabledByDefault: true
);
public static readonly DiagnosticDescriptor ShouldBeNonSerialized = new(
id: "ALCHEMY003",
title: "AlchemySerializeField should be NonSerialized to avoid duplicate serialization.",
messageFormat: "AlchemySerializeField '{0}' should be NonSerialized to avoid duplicate serialization.",
category: Category,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true
);
} }
} }