From 570e9c23aee29467b1c1ee7e0df28267dbc260cd Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Thu, 30 Jun 2022 12:07:37 +0300 Subject: [PATCH] #2 Fix nullable --- ...aui.BindableProperty.Generator.Demo.csproj | 1 + .../ViewModels/MainViewModel.cs | 10 ++++----- .../AutoBindablePropertyGenerator.cs | 21 ++++++++++--------- .../Implementation/DefaultBindingMode.cs | 2 +- .../Implementation/DefaultValue.cs | 2 +- .../Interfaces/IImplementation.cs | 2 +- .../Implementation/PropertyChanged.cs | 2 +- .../Helpers/ExecutionContextExtensions.cs | 2 +- .../Helpers/SymbolExtensions.cs | 4 ++-- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Maui.BindableProperty.Generator.Demo/Maui.BindableProperty.Generator.Demo.csproj b/src/Maui.BindableProperty.Generator.Demo/Maui.BindableProperty.Generator.Demo.csproj index 95ce577..87f1cc3 100644 --- a/src/Maui.BindableProperty.Generator.Demo/Maui.BindableProperty.Generator.Demo.csproj +++ b/src/Maui.BindableProperty.Generator.Demo/Maui.BindableProperty.Generator.Demo.csproj @@ -11,6 +11,7 @@ true enable enable + Nullable Maui.BindableProperty.Generator.Demo diff --git a/src/Maui.BindableProperty.Generator.Demo/ViewModels/MainViewModel.cs b/src/Maui.BindableProperty.Generator.Demo/ViewModels/MainViewModel.cs index bcae55c..7f95749 100644 --- a/src/Maui.BindableProperty.Generator.Demo/ViewModels/MainViewModel.cs +++ b/src/Maui.BindableProperty.Generator.Demo/ViewModels/MainViewModel.cs @@ -6,18 +6,16 @@ namespace Maui.BindableProperty.Generator.Demo.ViewModels public partial class MainViewModel : ObservableObject { [ObservableProperty] - private string _firstName; + private string? _firstName; [ObservableProperty] - private string _lastName; + private string? _lastName; [ObservableProperty] private DateTime _birthDate; [ObservableProperty] - private string _country; - - public MainViewModel(){} + private string? _country; [RelayCommand] private void LogClicked() @@ -28,7 +26,7 @@ private void LogClicked() System.Diagnostics.Debug.WriteLine(@$"BirthDate -> {this.BirthDate}"); } - partial void OnFirstNameChanged(string value) + partial void OnFirstNameChanged(string? value) { System.Diagnostics.Debug.WriteLine("Method OnFirstNameChanged fired"); System.Diagnostics.Debug.WriteLine(value); diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs index edbd687..ce7465e 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs @@ -15,6 +15,7 @@ public class AutoBindablePropertyGenerator : ISourceGenerator private readonly List CustomImplementations = new() { new DefaultValue(), new PropertyChanged(), new DefaultBindingMode() }; private const string attributeText = @" + #nullable enable using System; namespace Maui.BindableProperty.Generator.Core { @@ -24,13 +25,13 @@ public sealed class AutoBindableAttribute : Attribute { public AutoBindableAttribute(){} - public string PropertyName { get; set; } + public string PropertyName { get; set; } = string.Empty; - public string OnChanged { get; set; } + public string? OnChanged { get; set; } - public string DefaultValue { get; set; } + public string? DefaultValue { get; set; } - public string DefaultBindingMode { get; set; } + public string? DefaultBindingMode { get; set; } public bool HidesUnderlyingProperty { get; set; } = false; } @@ -53,7 +54,7 @@ private string ProcessClass(INamedTypeSymbol classSymbol, List fie var namespaceName = classSymbol.ContainingNamespace.ToDisplayString(); var w = new CodeWriter(CodeWriterSettings.CSharpDefault); - using (w.B(@$"namespace {namespaceName}")) + using (w.B("#nullable enable", @$"namespace {namespaceName}")) { w._(AutoBindableConstants.AttrGeneratedCodeString); using (w.B(@$"public partial class {classSymbol.Name}")) @@ -111,7 +112,7 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy using (w.B(@$"set")) { w._($@"SetValue({bindablePropertyName}, value);"); - this.ProcessBodyStter(w); + this.ProcessBodySetter(w); } } else @@ -133,15 +134,15 @@ private string ProcessBindableParameters() { var parameters = this.CustomImplementations .Select(i => i.ProcessBindableParameters()) - .Where(x => !string.IsNullOrEmpty(x)); + .Where(x => !string.IsNullOrEmpty(x)).ToArray(); - return parameters.Any() ? $@",{ string.Join(",", parameters) }" : string.Empty; + return parameters.Length > 0 ? $@",{ string.Join(",", parameters) }" : string.Empty; } - private void ProcessBodyStter(CodeWriter w) + private void ProcessBodySetter(CodeWriter w) { this.CustomImplementations - .ForEach(i => i.ProcessBodyStter(w)); + .ForEach(i => i.ProcessBodySetter(w)); } private void ProcessImplementationLogic(CodeWriter w) diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultBindingMode.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultBindingMode.cs index 630b6e6..608f80c 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultBindingMode.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultBindingMode.cs @@ -41,7 +41,7 @@ public string ProcessBindableParameters() return defaultValue != default ? $"defaultBindingMode: {defaultValue}" : default; } - public void ProcessBodyStter(CodeWriter w) + public void ProcessBodySetter(CodeWriter w) { // Not implemented } diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultValue.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultValue.cs index eace92e..20f420f 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultValue.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/DefaultValue.cs @@ -50,7 +50,7 @@ public string ProcessBindableParameters() return defaultValue != null ? $"defaultValue: {defaultValue}" : $"defaultValue: default({fieldType})"; } - public void ProcessBodyStter(CodeWriter w) + public void ProcessBodySetter(CodeWriter w) { // Not implemented } diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/Interfaces/IImplementation.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/Interfaces/IImplementation.cs index 2da895c..d1a7787 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/Interfaces/IImplementation.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/Interfaces/IImplementation.cs @@ -7,7 +7,7 @@ public interface IImplementation void Initialize(TypedConstant nameProperty, IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol); bool SetterImplemented(); string ProcessBindableParameters(); - void ProcessBodyStter(CodeWriter w); + void ProcessBodySetter(CodeWriter w); void ProcessImplementationLogic(CodeWriter w); } } diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs index 20846cb..9150533 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs @@ -33,7 +33,7 @@ public string ProcessBindableParameters() }); } - public void ProcessBodyStter(CodeWriter w) + public void ProcessBodySetter(CodeWriter w) { // Not implemented } diff --git a/src/Maui.BindableProperty.Generator/Helpers/ExecutionContextExtensions.cs b/src/Maui.BindableProperty.Generator/Helpers/ExecutionContextExtensions.cs index 1304695..f6770ff 100644 --- a/src/Maui.BindableProperty.Generator/Helpers/ExecutionContextExtensions.cs +++ b/src/Maui.BindableProperty.Generator/Helpers/ExecutionContextExtensions.cs @@ -22,7 +22,7 @@ public static void EachField( // Group the fields by class, and generate the source #pragma warning disable RS1024 // Symbols should be compared for equality - foreach (IGrouping group in receiver.Fields?.GroupBy(f => f.ContainingType)) + foreach (IGrouping group in receiver.Fields?.GroupBy(f => f.ContainingType) ?? Enumerable.Empty>()) { action?.Invoke(attributeSymbol, group); } diff --git a/src/Maui.BindableProperty.Generator/Helpers/SymbolExtensions.cs b/src/Maui.BindableProperty.Generator/Helpers/SymbolExtensions.cs index 03f0ba4..549ff30 100644 --- a/src/Maui.BindableProperty.Generator/Helpers/SymbolExtensions.cs +++ b/src/Maui.BindableProperty.Generator/Helpers/SymbolExtensions.cs @@ -10,7 +10,7 @@ public static TypedConstant GetTypedConstant( string key) { // Get the AutoNotify attribute from the field, and any associated data - var attributeData = fieldSymbol.GetAttributes().Single(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default)); + var attributeData = fieldSymbol.GetAttributes().Single(ad => attributeSymbol.Equals(ad.AttributeClass, SymbolEqualityComparer.Default)); return attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == key).Value; } @@ -38,7 +38,7 @@ public static T GetValue( { var value = TypedConstant.Value; - if (value.GetType() == typeof(T)) + if (value?.GetType() == typeof(T)) { if (onSuccess != null) {