diff --git a/BindableProps/BindableProps.csproj b/BindableProps/BindableProps.csproj index 15779be..791a3c3 100644 --- a/BindableProps/BindableProps.csproj +++ b/BindableProps/BindableProps.csproj @@ -13,7 +13,7 @@ LICENSE.txt source-generator;maui;net-standard;net-6;net-7;net-8;utility;helper https://github.com/KafkaWannaFly/BindableProps - 1.4.1 + 1.4.2 https://github.com/KafkaWannaFly/BindableProps git README.md diff --git a/BindablePropsSG/Generators/AllBindablePropsSG.cs b/BindablePropsSG/Generators/AllBindablePropsSG.cs index 39197be..ec4e054 100644 --- a/BindablePropsSG/Generators/AllBindablePropsSG.cs +++ b/BindablePropsSG/Generators/AllBindablePropsSG.cs @@ -84,15 +84,15 @@ protected override void Execute(SourceProductionContext context, } } - protected override string ProcessClass(ClassDeclarationSyntax? classSyntax, + protected override string ProcessClass(TypeDeclarationSyntax? typeDeclarationSyntax, List<(SyntaxNode, ISymbol)> syntaxSymbols) { - if (classSyntax is null || !syntaxSymbols.Any()) + if (typeDeclarationSyntax is null || !syntaxSymbols.Any()) return string.Empty; - var usingDirectives = classSyntax.SyntaxTree.GetCompilationUnitRoot().Usings; + var usingDirectives = typeDeclarationSyntax.SyntaxTree.GetCompilationUnitRoot().Usings; - var namespaceSyntax = classSyntax.Parent as BaseNamespaceDeclarationSyntax; + var namespaceSyntax = typeDeclarationSyntax.Parent as BaseNamespaceDeclarationSyntax; var namespaceName = namespaceSyntax?.Name.ToString() ?? "global"; var source = new StringBuilder($@" @@ -103,7 +103,7 @@ protected override string ProcessClass(ClassDeclarationSyntax? classSyntax, namespace {namespaceName} {{ - public partial class {classSyntax.Identifier} + public partial class {typeDeclarationSyntax.Identifier} {{ "); @@ -111,7 +111,7 @@ public partial class {classSyntax.Identifier} { // variableDeclaratorSyntax --> variableDeclarationSyntax --> fieldDeclarationSyntax var fieldDeclarationSyntax = syntax.Parent?.Parent!; - ProcessField(source, classSyntax, fieldDeclarationSyntax, symbol); + ProcessField(source, typeDeclarationSyntax, fieldDeclarationSyntax, symbol); } source.Append(@" @@ -122,11 +122,11 @@ public partial class {classSyntax.Identifier} return source.ToString(); } - protected override void ProcessField(StringBuilder source, ClassDeclarationSyntax classDeclarationSyntax, + protected override void ProcessField(StringBuilder source, TypeDeclarationSyntax typeDeclarationSyntax, SyntaxNode syntaxNode, ISymbol fieldSymbol) { var bindablePropParam = SyntaxUtil.ExtractCreateBindablePropertyParam( - classDeclarationSyntax, + typeDeclarationSyntax, syntaxNode, fieldSymbol, "Dummy Name That I Don't Care" diff --git a/BindablePropsSG/Generators/AttachedPropSG.cs b/BindablePropsSG/Generators/AttachedPropSG.cs index 1f321fa..f13ad4b 100644 --- a/BindablePropsSG/Generators/AttachedPropSG.cs +++ b/BindablePropsSG/Generators/AttachedPropSG.cs @@ -17,12 +17,12 @@ public class AttachedPropSG : BaseGenerator "AttachedPropAttribute" }; - protected override void ProcessField(StringBuilder source, ClassDeclarationSyntax classSyntax, + protected override void ProcessField(StringBuilder source, TypeDeclarationSyntax typeDeclarationSyntax, SyntaxNode syntaxNode, ISymbol fieldSymbol) { var bindablePropParam = - SyntaxUtil.ExtractCreateBindablePropertyParam(classSyntax, syntaxNode, fieldSymbol, "AttachedProp"); + SyntaxUtil.ExtractCreateBindablePropertyParam(typeDeclarationSyntax, syntaxNode, fieldSymbol, "AttachedProp"); var defaultOnChangedDelegate = SyntaxUtil.GetDefaultOnChangedDelegate( bindablePropParam.ClassType!, diff --git a/BindablePropsSG/Generators/BaseGenerator.cs b/BindablePropsSG/Generators/BaseGenerator.cs index 16f9cad..f3372c9 100644 --- a/BindablePropsSG/Generators/BaseGenerator.cs +++ b/BindablePropsSG/Generators/BaseGenerator.cs @@ -59,9 +59,9 @@ protected virtual void Execute(SourceProductionContext context, { if (syntaxSymbols.IsDefaultOrEmpty) return; - - var groupList = syntaxSymbols.GroupBy<(SyntaxNode, ISymbol), ClassDeclarationSyntax>( - fieldGroup => (ClassDeclarationSyntax)fieldGroup.Item1!.Parent! + + var groupList = syntaxSymbols.GroupBy<(SyntaxNode, ISymbol), TypeDeclarationSyntax>( + fieldGroup => (TypeDeclarationSyntax)fieldGroup.Item1!.Parent! ); foreach (var group in groupList) @@ -73,17 +73,17 @@ protected virtual void Execute(SourceProductionContext context, } } - protected virtual string ProcessClass(ClassDeclarationSyntax? classSyntax, + protected virtual string ProcessClass(TypeDeclarationSyntax? typeDeclarationSyntax, List<(SyntaxNode, ISymbol)> syntaxSymbols) { - if (classSyntax is null) + if (typeDeclarationSyntax is null) { return string.Empty; } - var usingDirectives = classSyntax.SyntaxTree.GetCompilationUnitRoot().Usings; + var usingDirectives = typeDeclarationSyntax.SyntaxTree.GetCompilationUnitRoot().Usings; - var namespaceSyntax = classSyntax.Parent as BaseNamespaceDeclarationSyntax; + var namespaceSyntax = typeDeclarationSyntax.Parent as BaseNamespaceDeclarationSyntax; var namespaceName = namespaceSyntax?.Name.ToString() ?? "global"; var source = new StringBuilder($@" @@ -94,13 +94,13 @@ protected virtual string ProcessClass(ClassDeclarationSyntax? classSyntax, namespace {namespaceName} {{ - public partial class {classSyntax.Identifier} + public partial {typeDeclarationSyntax.Keyword.Text} {typeDeclarationSyntax.Identifier} {{ "); foreach (var (syntax, symbol) in syntaxSymbols) { - ProcessField(source, classSyntax, syntax, symbol); + ProcessField(source, typeDeclarationSyntax, syntax, symbol); } source.Append(@$" @@ -116,7 +116,7 @@ protected virtual string ProcessClass((SyntaxNode, ISymbol) group) return string.Empty; } - protected virtual void ProcessField(StringBuilder source, ClassDeclarationSyntax classSyntax, + protected virtual void ProcessField(StringBuilder source, TypeDeclarationSyntax typeDeclarationSyntax, SyntaxNode fieldSyntax, ISymbol fieldSymbol) { } diff --git a/BindablePropsSG/Generators/BindablePropSG.cs b/BindablePropsSG/Generators/BindablePropSG.cs index 973d65c..b79a6bf 100644 --- a/BindablePropsSG/Generators/BindablePropSG.cs +++ b/BindablePropsSG/Generators/BindablePropSG.cs @@ -17,12 +17,12 @@ public class BindablePropSG : BaseGenerator "BindablePropAttribute" }; - protected override void ProcessField(StringBuilder source, ClassDeclarationSyntax classSyntax, + protected override void ProcessField(StringBuilder source, TypeDeclarationSyntax typeDeclarationSyntax, SyntaxNode syntaxNode, ISymbol fieldSymbol) { var bindablePropParam = - SyntaxUtil.ExtractCreateBindablePropertyParam(classSyntax, syntaxNode, fieldSymbol, "BindableProp"); + SyntaxUtil.ExtractCreateBindablePropertyParam(typeDeclarationSyntax, syntaxNode, fieldSymbol, "BindableProp"); source.Append($@" public {bindablePropParam.NewKeyWord} static readonly BindableProperty {bindablePropParam.PropName}Property = BindableProperty.Create( diff --git a/BindablePropsSG/Generators/BindableReadOnlyPropSG.cs b/BindablePropsSG/Generators/BindableReadOnlyPropSG.cs index 5c4d3be..1dd449c 100644 --- a/BindablePropsSG/Generators/BindableReadOnlyPropSG.cs +++ b/BindablePropsSG/Generators/BindableReadOnlyPropSG.cs @@ -18,13 +18,13 @@ public class BindableReadOnlyPropSG : BaseGenerator protected override void ProcessField( StringBuilder source, - ClassDeclarationSyntax classSyntax, + TypeDeclarationSyntax typeDeclarationSyntax, SyntaxNode syntaxNode, ISymbol fieldSymbol ) { var bindablePropParam = - SyntaxUtil.ExtractCreateBindablePropertyParam(classSyntax, syntaxNode, fieldSymbol, "BindableReadOnlyProp"); + SyntaxUtil.ExtractCreateBindablePropertyParam(typeDeclarationSyntax, syntaxNode, fieldSymbol, "BindableReadOnlyProp"); var fieldSyntax = (FieldDeclarationSyntax)syntaxNode; var attributeSyntax = SyntaxUtil.GetAttributeByName(fieldSyntax, "BindableReadOnlyProp"); diff --git a/BindablePropsSG/Utils/SyntaxUtil.cs b/BindablePropsSG/Utils/SyntaxUtil.cs index 85a175c..7c3ceae 100644 --- a/BindablePropsSG/Utils/SyntaxUtil.cs +++ b/BindablePropsSG/Utils/SyntaxUtil.cs @@ -123,7 +123,7 @@ public static string GetDefaultOnChangedDelegate(string classType, string propNa } public static BindablePropertyParam ExtractCreateBindablePropertyParam( - ClassDeclarationSyntax classSyntax, + TypeDeclarationSyntax typeDeclarationSyntax, SyntaxNode syntaxNode, ISymbol fieldSymbol, string attributeName @@ -138,7 +138,7 @@ string attributeName var newKeyword = fieldSyntax.Modifiers .FirstOrDefault(keyword => keyword.Text.Equals("new")).ToString(); - var classType = classSyntax.Identifier.ToString(); + var classType = typeDeclarationSyntax.Identifier.ToString(); var defaultFieldValue = GetFieldDefaultValue(fieldSyntax) ?? "default"; diff --git a/UnitTest/BaseTest.cs b/UnitTest/BaseTest.cs index 3fbc393..925aaff 100644 --- a/UnitTest/BaseTest.cs +++ b/UnitTest/BaseTest.cs @@ -4,9 +4,9 @@ namespace UnitTest; public class BaseTest where TGenerator : IIncrementalGenerator, new() { - protected IIncrementalGenerator _generator = new TGenerator(); + private readonly IIncrementalGenerator _generator = new TGenerator(); - virtual protected string GetDataFolder() + protected virtual string GetDataFolder() { return string.Empty; }