|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#if ROSLYN_4_11_0_OR_GREATER |
| 6 | + |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Collections.Immutable; |
| 9 | +using CommunityToolkit.Mvvm.SourceGenerators.Extensions; |
| 10 | +using Microsoft.CodeAnalysis; |
| 11 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 12 | +using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors; |
| 13 | + |
| 14 | +namespace CommunityToolkit.Mvvm.SourceGenerators; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// A diagnostic analyzer that generates an error when <c>[GeneratedBindableCustomProperty]</c> is used on types with invalid generated base members. |
| 18 | +/// </summary> |
| 19 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 20 | +public sealed class WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer : DiagnosticAnalyzer |
| 21 | +{ |
| 22 | + /// <inheritdoc/> |
| 23 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create( |
| 24 | + WinRTGeneratedBindableCustomPropertyWithBaseObservablePropertyOnField, |
| 25 | + WinRTGeneratedBindableCustomPropertyWithBaseRelayCommand); |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + public override void Initialize(AnalysisContext context) |
| 29 | + { |
| 30 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 31 | + context.EnableConcurrentExecution(); |
| 32 | + |
| 33 | + context.RegisterCompilationStartAction(static context => |
| 34 | + { |
| 35 | + // This analyzer is only enabled when CsWinRT is also used |
| 36 | + if (!context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.IsUsingWindowsRuntimePack()) |
| 37 | + { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Get the symbol for [ObservableProperty], [RelayCommand] and [GeneratedBindableCustomProperty] |
| 42 | + if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") is not INamedTypeSymbol observablePropertySymbol || |
| 43 | + context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.Input.RelayCommandAttribute") is not INamedTypeSymbol relayCommandSymbol || |
| 44 | + context.Compilation.GetTypeByMetadataName("WinRT.GeneratedBindableCustomPropertyAttribute") is not INamedTypeSymbol generatedBindableCustomPropertySymbol) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + context.RegisterSymbolAction(context => |
| 50 | + { |
| 51 | + // Ensure we do have a valid type |
| 52 | + if (context.Symbol is not INamedTypeSymbol typeSymbol) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // We only care about it if it's using [GeneratedBindableCustomProperty] |
| 58 | + if (!typeSymbol.TryGetAttributeWithType(generatedBindableCustomPropertySymbol, out AttributeData? generatedBindableCustomPropertyAttribute)) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Warn on all [ObservableProperty] fields |
| 64 | + foreach (IFieldSymbol fieldSymbol in FindObservablePropertyFields(typeSymbol, relayCommandSymbol)) |
| 65 | + { |
| 66 | + context.ReportDiagnostic(Diagnostic.Create( |
| 67 | + WinRTGeneratedBindableCustomPropertyWithBaseObservablePropertyOnField, |
| 68 | + generatedBindableCustomPropertyAttribute.GetLocation(), |
| 69 | + typeSymbol, |
| 70 | + fieldSymbol.ContainingType, |
| 71 | + fieldSymbol.Name)); |
| 72 | + } |
| 73 | + |
| 74 | + // Warn on all [RelayCommand] methods |
| 75 | + foreach (IMethodSymbol methodSymbol in FindRelayCommandMethods(typeSymbol, relayCommandSymbol)) |
| 76 | + { |
| 77 | + context.ReportDiagnostic(Diagnostic.Create( |
| 78 | + WinRTGeneratedBindableCustomPropertyWithBaseRelayCommand, |
| 79 | + generatedBindableCustomPropertyAttribute.GetLocation(), |
| 80 | + typeSymbol, |
| 81 | + methodSymbol)); |
| 82 | + } |
| 83 | + }, SymbolKind.NamedType); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Finds all methods in the base types that have the <c>[RelayCommand]</c> attribute. |
| 89 | + /// </summary> |
| 90 | + /// <param name="typeSymbol">The <see cref="INamedTypeSymbol"/> instance to inspect.</param> |
| 91 | + /// <param name="relayCommandSymbol">The symbol for the <c>[RelayCommand]</c></param> |
| 92 | + /// <returns>All <see cref="IMethodSymbol"/> instances for matching members.</returns> |
| 93 | + private static IEnumerable<IMethodSymbol> FindRelayCommandMethods(INamedTypeSymbol typeSymbol, INamedTypeSymbol relayCommandSymbol) |
| 94 | + { |
| 95 | + // Check whether the base type (if any) is from the same assembly, and stop if it isn't. We do not |
| 96 | + // want to include methods from the same type, as those will already be caught by another analyzer. |
| 97 | + if (!SymbolEqualityComparer.Default.Equals(typeSymbol.ContainingAssembly, typeSymbol.BaseType)) |
| 98 | + { |
| 99 | + yield break; |
| 100 | + } |
| 101 | + |
| 102 | + foreach (ISymbol memberSymbol in typeSymbol.BaseType.GetAllMembersFromSameAssembly()) |
| 103 | + { |
| 104 | + if (memberSymbol is IMethodSymbol methodSymbol && |
| 105 | + methodSymbol.HasAttributeWithType(relayCommandSymbol)) |
| 106 | + { |
| 107 | + yield return methodSymbol; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Finds all fields in the base types that have the <c>[ObservableProperty]</c> attribute. |
| 114 | + /// </summary> |
| 115 | + /// <param name="typeSymbol">The <see cref="INamedTypeSymbol"/> instance to inspect.</param> |
| 116 | + /// <param name="observablePropertySymbol">The symbol for the <c>[ObservableProperty]</c></param> |
| 117 | + /// <returns>All <see cref="IFieldSymbol"/> instances for matching members.</returns> |
| 118 | + private static IEnumerable<IFieldSymbol> FindObservablePropertyFields(INamedTypeSymbol typeSymbol, INamedTypeSymbol observablePropertySymbol) |
| 119 | + { |
| 120 | + foreach (ISymbol memberSymbol in typeSymbol.GetAllMembersFromSameAssembly()) |
| 121 | + { |
| 122 | + if (memberSymbol is IFieldSymbol fieldSymbol && |
| 123 | + fieldSymbol.HasAttributeWithType(observablePropertySymbol)) |
| 124 | + { |
| 125 | + yield return fieldSymbol; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +#endif |
0 commit comments