Refactor and fixes
MODIFY
WalletWasabi.Fluent.Generators/AutoNotifyGenerator.cs
WalletWasabi.Fluent.Generators/AutoNotifyGenerator.cs
@@ -41,34 +41,57 @@ namespace WalletWasabi.Fluent
return;
}
- CSharpParseOptions options = (context.Compilation as CSharpCompilation).SyntaxTrees[0].Options as CSharpParseOptions;
- Compilation compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
+ var options = (context.Compilation as CSharpCompilation)?.SyntaxTrees[0].Options as CSharpParseOptions;
+ var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
- INamedTypeSymbol attributeSymbol = compilation.GetTypeByMetadataName("WalletWasabi.Fluent.AutoNotifyAttribute");
- INamedTypeSymbol notifySymbol = compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged");
+ var attributeSymbol = compilation.GetTypeByMetadataName("WalletWasabi.Fluent.AutoNotifyAttribute");
+ if (attributeSymbol is null)
+ {
+ return;
+ }
+
+ var notifySymbol = compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged");
+ if (notifySymbol is null)
+ {
+ return;
+ }
+
+ List<IFieldSymbol> fieldSymbols = new ();
- List<IFieldSymbol> fieldSymbols = new List<IFieldSymbol>();
foreach (FieldDeclarationSyntax field in receiver.CandidateFields)
{
- SemanticModel model = compilation.GetSemanticModel(field.SyntaxTree);
+ var semanticModel = compilation.GetSemanticModel(field.SyntaxTree);
+
foreach (VariableDeclaratorSyntax variable in field.Declaration.Variables)
{
- IFieldSymbol fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol;
- if (fieldSymbol.GetAttributes().Any(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default)))
+ var fieldSymbol = semanticModel.GetDeclaredSymbol(variable) as IFieldSymbol;
+ if (fieldSymbol is null)
+ {
+ continue;
+ }
+
+ var attributes = fieldSymbol.GetAttributes();
+ if (attributes.Any(ad => ad?.AttributeClass?.Equals(attributeSymbol, SymbolEqualityComparer.Default) ?? false))
{
fieldSymbols.Add(fieldSymbol);
}
}
}
- foreach (IGrouping<INamedTypeSymbol, IFieldSymbol> group in fieldSymbols.GroupBy(f => f.ContainingType))
+ var groupedFields = fieldSymbols.GroupBy(f => f.ContainingType);
+
+ foreach (var group in groupedFields)
+ {
+ var classSource = ProcessClass(group.Key, group.ToList(), attributeSymbol, notifySymbol, context);
+ if (classSource is null)
{
- string classSource = ProcessClass(group.Key, group.ToList(), attributeSymbol, notifySymbol, context);
+ continue;
+ }
context.AddSource($"{group.Key.Name}_AutoNotify.cs", SourceText.From(classSource, Encoding.UTF8));
}
}
- private string ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fields, ISymbol attributeSymbol, ISymbol notifySymbol, GeneratorExecutionContext context)
+ private string? ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fields, ISymbol attributeSymbol, INamedTypeSymbol notifySymbol, GeneratorExecutionContext context)
{
if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
{
@@ -115,15 +138,15 @@ namespace {namespaceName}
private void ProcessField(StringBuilder source, IFieldSymbol fieldSymbol, ISymbol attributeSymbol)
{
- string fieldName = fieldSymbol.Name;
- ITypeSymbol fieldType = fieldSymbol.Type;
- AttributeData attributeData = fieldSymbol.GetAttributes().Single(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default));
- TypedConstant overridenNameOpt = attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == "PropertyName").Value;
+ var fieldName = fieldSymbol.Name;
+ var fieldType = fieldSymbol.Type;
+ var attributeData = fieldSymbol.GetAttributes().Single(ad => ad?.AttributeClass?.Equals(attributeSymbol, SymbolEqualityComparer.Default) ?? false);
+ var overridenNameOpt = attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == "PropertyName").Value;
+ var propertyName = ChooseName(fieldName, overridenNameOpt);
- string propertyName = ChooseName(fieldName, overridenNameOpt);
- if (propertyName.Length == 0 || propertyName == fieldName)
+ if (propertyName is null || propertyName.Length == 0 || propertyName == fieldName)
{
- //TODO: issue a diagnostic that we can't process this field
+ // Issue a diagnostic that we can't process this field.
return;
}
@@ -141,11 +164,11 @@ namespace {namespaceName}
}}
}}");
- static string ChooseName(string fieldName, TypedConstant overridenNameOpt)
+ static string? ChooseName(string fieldName, TypedConstant overridenNameOpt)
{
if (!overridenNameOpt.IsNull)
{
- return overridenNameOpt.Value.ToString();
+ return overridenNameOpt.Value?.ToString();
}
fieldName = fieldName.TrimStart('_');

MODIFY
WalletWasabi.Fluent.Generators/NavigationMetaDataGenerator.cs
WalletWasabi.Fluent.Generators/NavigationMetaDataGenerator.cs
@@ -82,25 +82,34 @@ namespace WalletWasabi.Fluent
return;
}
- CSharpParseOptions options =
- (context.Compilation as CSharpCompilation).SyntaxTrees[0].Options as CSharpParseOptions;
- Compilation compilation =
- context.Compilation.AddSyntaxTrees(
- CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
- INamedTypeSymbol attributeSymbol =
- compilation.GetTypeByMetadataName("WalletWasabi.Fluent.NavigationMetaDataAttribute");
- INamedTypeSymbol metadataSymbol =
- compilation.GetTypeByMetadataName("WalletWasabi.Fluent.NavigationMetaData");
+ var options = (context.Compilation as CSharpCompilation)?.SyntaxTrees[0].Options as CSharpParseOptions;
+ var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
+
+ var attributeSymbol = compilation.GetTypeByMetadataName("WalletWasabi.Fluent.NavigationMetaDataAttribute");
+ if (attributeSymbol is null)
+ {
+ return;
+ }
+
+ var metadataSymbol = compilation.GetTypeByMetadataName("WalletWasabi.Fluent.NavigationMetaData");
+ if (metadataSymbol is null)
+ {
+ return;
+ }
List<INamedTypeSymbol> namedTypeSymbols = new();
foreach (var candidateClass in receiver.CandidateClasses)
{
- SemanticModel model = compilation.GetSemanticModel(candidateClass.SyntaxTree);
- var namedTypeSymbol = model.GetDeclaredSymbol(candidateClass);
+ var semanticModel = compilation.GetSemanticModel(candidateClass.SyntaxTree);
+ var namedTypeSymbol = semanticModel.GetDeclaredSymbol(candidateClass);
+ if (namedTypeSymbol is null)
+ {
+ continue;
+ }
- if (namedTypeSymbol.GetAttributes().Any(ad =>
- ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default)))
+ var attributes = namedTypeSymbol.GetAttributes();
+ if (attributes.Any(ad => ad?.AttributeClass?.Equals(attributeSymbol, SymbolEqualityComparer.Default) ?? false))
{
namedTypeSymbols.Add(namedTypeSymbol);
}
@@ -108,14 +117,15 @@ namespace WalletWasabi.Fluent
foreach (var namedTypeSymbol in namedTypeSymbols)
{
- string classSource = ProcessClass(namedTypeSymbol, attributeSymbol, metadataSymbol, context);
- context.AddSource($"{namedTypeSymbol.Name}_NavigationMetaData.cs",
- SourceText.From(classSource, Encoding.UTF8));
+ var classSource = ProcessClass(namedTypeSymbol, attributeSymbol, metadataSymbol);
+ if (classSource is not null)
+ {
+ context.AddSource($"{namedTypeSymbol.Name}_NavigationMetaData.cs", SourceText.From(classSource, Encoding.UTF8));
+ }
}
}
- private string ProcessClass(INamedTypeSymbol namedTypeSymbol, ISymbol attributeSymbol, ISymbol metadataSymbol,
- GeneratorExecutionContext context)
+ private static string? ProcessClass(INamedTypeSymbol namedTypeSymbol, ISymbol attributeSymbol, ISymbol metadataSymbol)
{
if (!namedTypeSymbol.ContainingSymbol.Equals(namedTypeSymbol.ContainingNamespace,
SymbolEqualityComparer.Default))
@@ -136,8 +146,9 @@ namespace {namespaceName}
{{
");
- AttributeData attributeData = namedTypeSymbol.GetAttributes().Single(ad =>
- ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default));
+ var attributeData = namedTypeSymbol
+ .GetAttributes()
+ .Single(ad => ad?.AttributeClass?.Equals(attributeSymbol, SymbolEqualityComparer.Default) ?? false);
source.Append($@" public static {metadataSymbol.ToDisplayString()} MetaData {{ get; }} = new()
{{

MODIFY
WalletWasabi.Fluent/ViewModels/AboutViewModel.cs
WalletWasabi.Fluent/ViewModels/AboutViewModel.cs
@@ -13,7 +13,7 @@ using WalletWasabi.Logging;
namespace WalletWasabi.Fluent.ViewModels
{
- // TODO: for testing only
+ // TODO: For testing only.
[Property("IsVisible", typeof(bool))]
[Property("Title", typeof(string), false, PropertyModifier.Public, PropertyModifier.Internal)]
[Property("Items", typeof(List<string>), false, PropertyModifier.Public, PropertyModifier.Private)]
@@ -33,7 +33,7 @@ namespace WalletWasabi.Fluent.ViewModels
NavBarPosition = NavBarPosition.None)]
public partial class AboutViewModel : RoutableViewModel
{
- // TODO: for testing only
+ // TODO: For testing only.
[AutoNotify]
private bool _test;

MODIFY
WalletWasabi.Fluent/ViewModels/MainViewModel.cs
WalletWasabi.Fluent/ViewModels/MainViewModel.cs
@@ -153,7 +153,9 @@ namespace WalletWasabi.Fluent.ViewModels
private static void RegisterEntries(SearchPageViewModel searchPage, LegalDocuments legalDocuments)
{
- /*NavigationManager.RegisterRoutable<LegalDocumentsViewModel>(
+ // TODO:
+ /*
+ NavigationManager.RegisterRoutable<LegalDocumentsViewModel>(
LegalDocumentsViewModel.MetaData,
async () =>
{
@@ -162,8 +164,8 @@ namespace WalletWasabi.Fluent.ViewModels
var legalDocs = new LegalDocumentsViewModel(content);
return legalDocs;
- });*/
-
+ });
+ */
AboutViewModel.Register(async () => await Task.FromResult(new AboutViewModel()));
}

Refactor and fixes