From 761a22cff56d09f02c391661c86d0ce4d2657711 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2024 16:05:06 -0700 Subject: [PATCH] Forward generator options to FAWMN --- .../ObservablePropertyGenerator.Execute.cs | 29 +++++++++++++++++++ .../ObservablePropertyGenerator.cs | 12 ++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs b/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs index c6bf051a4..6aefd581a 100644 --- a/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs +++ b/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs @@ -16,6 +16,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -35,6 +36,7 @@ internal static class Execute /// The instance to process. /// The input instance to process. /// The instance for the current run. + /// The options in use for the generator. /// The cancellation token for the current operation. /// The resulting value, if successfully retrieved. /// The resulting diagnostics from the processing operation. @@ -43,6 +45,7 @@ public static bool TryGetInfo( FieldDeclarationSyntax fieldSyntax, IFieldSymbol fieldSymbol, SemanticModel semanticModel, + AnalyzerConfigOptions options, CancellationToken token, [NotNullWhen(true)] out PropertyInfo? propertyInfo, out ImmutableArray diagnostics) @@ -66,6 +69,11 @@ public static bool TryGetInfo( token.ThrowIfCancellationRequested(); + // Override the property changing support if explicitly disabled + shouldInvokeOnPropertyChanging &= GetEnableINotifyPropertyChangingSupport(options); + + token.ThrowIfCancellationRequested(); + // Get the property type and name string typeNameWithNullabilityAnnotations = fieldSymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations(); string fieldName = fieldSymbol.Name; @@ -320,6 +328,27 @@ public static bool TryGetInfo( return true; } + /// + /// Gets the value for the "MvvmToolkitEnableINotifyPropertyChangingSupport" property. + /// + /// The options in use for the generator. + /// The value for the "MvvmToolkitEnableINotifyPropertyChangingSupport" property. + public static bool GetEnableINotifyPropertyChangingSupport(AnalyzerConfigOptions options) + { + if (options.TryGetValue("build_property.MvvmToolkitEnableINotifyPropertyChangingSupport", out string? propertyValue)) + { + if (bool.TryParse(propertyValue, out bool enableINotifyPropertyChangingSupport)) + { + return enableINotifyPropertyChangingSupport; + } + } + + // This setting is enabled by default, for backwards compatibility. + // Note that this path should never be reached, as the default + // value is also set in a .targets file bundled in the package. + return true; + } + /// /// Validates the containing type for a given field being annotated. /// diff --git a/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs b/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs index ad7e5fe03..68801d449 100644 --- a/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs +++ b/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs @@ -25,8 +25,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { // Gather info for all annotated fields IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Info)> propertyInfoWithErrors = - context.SyntaxProvider - .ForAttributeWithMetadataName( + context.ForAttributeWithMetadataNameAndOptions( "CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute", static (node, _) => node is VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: FieldDeclarationSyntax { Parent: ClassDeclarationSyntax or RecordDeclarationSyntax, AttributeLists.Count: > 0 } } }, static (context, token) => @@ -44,7 +43,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context) token.ThrowIfCancellationRequested(); - _ = Execute.TryGetInfo(fieldDeclaration, fieldSymbol, context.SemanticModel, token, out PropertyInfo? propertyInfo, out ImmutableArray diagnostics); + _ = Execute.TryGetInfo( + fieldDeclaration, + fieldSymbol, + context.SemanticModel, + context.GlobalOptions, + token, + out PropertyInfo? propertyInfo, + out ImmutableArray diagnostics); token.ThrowIfCancellationRequested();