|
| 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.Immutable; |
| 8 | +using CommunityToolkit.Mvvm.SourceGenerators.Extensions; |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | +using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors; |
| 12 | + |
| 13 | +namespace CommunityToolkit.Mvvm.SourceGenerators; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// A diagnostic analyzer that generates an error when <c>[RelayCommand]</c> is used on a method inside a type with <c>[GeneratedBindableCustomProperty]</c>. |
| 17 | +/// </summary> |
| 18 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 19 | +public sealed class WinRTRelayCommandIsNotGeneratedBindableCustomPropertyCompatibleAnalyzer : DiagnosticAnalyzer |
| 20 | +{ |
| 21 | + /// <inheritdoc/> |
| 22 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(WinRTRelayCommandIsNotGeneratedBindableCustomPropertyCompatible); |
| 23 | + |
| 24 | + /// <inheritdoc/> |
| 25 | + public override void Initialize(AnalysisContext context) |
| 26 | + { |
| 27 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 28 | + context.EnableConcurrentExecution(); |
| 29 | + |
| 30 | + context.RegisterCompilationStartAction(static context => |
| 31 | + { |
| 32 | + // This analyzer is only enabled when CsWinRT is also used |
| 33 | + if (!context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.IsUsingWindowsRuntimePack()) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Get the symbol for [RelayCommand] and [GeneratedBindableCustomProperty] |
| 39 | + if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.RelayCommandAttribute") is not INamedTypeSymbol relayCommandSymbol || |
| 40 | + context.Compilation.GetTypeByMetadataName("WinRT.GeneratedBindableCustomPropertyAttribute") is not INamedTypeSymbol generatedBindableCustomPropertySymbol) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + bool isLanguageVersionPreview = context.Compilation.IsLanguageVersionPreview(); |
| 46 | + |
| 47 | + context.RegisterSymbolAction(context => |
| 48 | + { |
| 49 | + // Ensure we do have a valid method with a containing type we can reference |
| 50 | + if (context.Symbol is not IMethodSymbol { ContainingType: INamedTypeSymbol typeSymbol } methodSymbol) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // If the method is not using [RelayCommand], we can skip it |
| 56 | + if (!methodSymbol.TryGetAttributeWithType(relayCommandSymbol, out AttributeData? relayCommandAttribute)) |
| 57 | + { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // If the containing type is using [GeneratedBindableCustomProperty], emit a warning |
| 62 | + if (typeSymbol.HasAttributeWithType(generatedBindableCustomPropertySymbol)) |
| 63 | + { |
| 64 | + context.ReportDiagnostic(Diagnostic.Create( |
| 65 | + WinRTRelayCommandIsNotGeneratedBindableCustomPropertyCompatible, |
| 66 | + relayCommandAttribute.GetLocation(), |
| 67 | + methodSymbol)); |
| 68 | + } |
| 69 | + }, SymbolKind.Method); |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#endif |
0 commit comments