|
| 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 | +using System.Collections.Immutable; |
| 6 | +using CommunityToolkit.Mvvm.SourceGenerators.Extensions; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | +using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.SuppressionDescriptors; |
| 12 | + |
| 13 | +namespace CommunityToolkit.Mvvm.SourceGenerators; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// <para> |
| 17 | +/// A diagnostic suppressor to suppress CS0657 warnings for methods with [RelayCommand] using a [field:] or [property:] attribute list. |
| 18 | +/// </para> |
| 19 | +/// <para> |
| 20 | +/// That is, this diagnostic suppressor will suppress the following diagnostic: |
| 21 | +/// <code> |
| 22 | +/// public partial class MyViewModel : ObservableObject |
| 23 | +/// { |
| 24 | +/// [RelayCommand] |
| 25 | +/// [field: JsonIgnore] |
| 26 | +/// [property: SomeOtherAttribute] |
| 27 | +/// private void DoSomething() |
| 28 | +/// { |
| 29 | +/// } |
| 30 | +/// } |
| 31 | +/// </code> |
| 32 | +/// </para> |
| 33 | +/// </summary> |
| 34 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 35 | +public sealed class RelayCommandAttributeWithFieldOrPropertyTargetDiagnosticSuppressor : DiagnosticSuppressor |
| 36 | +{ |
| 37 | + /// <inheritdoc/> |
| 38 | + public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(FieldOrPropertyAttributeListForRelayCommandMethod); |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + public override void ReportSuppressions(SuppressionAnalysisContext context) |
| 42 | + { |
| 43 | + foreach (Diagnostic diagnostic in context.ReportedDiagnostics) |
| 44 | + { |
| 45 | + SyntaxNode? syntaxNode = diagnostic.Location.SourceTree?.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan); |
| 46 | + |
| 47 | + // Check that the target is effectively [field:] or [property:] over a method declaration, which is the case we're looking for |
| 48 | + if (syntaxNode is AttributeTargetSpecifierSyntax { Parent.Parent: MethodDeclarationSyntax methodDeclaration, Identifier: SyntaxToken(SyntaxKind.FieldKeyword or SyntaxKind.PropertyKeyword) }) |
| 49 | + { |
| 50 | + SemanticModel semanticModel = context.GetSemanticModel(syntaxNode.SyntaxTree); |
| 51 | + |
| 52 | + // Get the method symbol from the first variable declaration |
| 53 | + ISymbol? declaredSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken); |
| 54 | + |
| 55 | + // Check if the method is using [RelayCommand], in which case we should suppress the warning |
| 56 | + if (declaredSymbol is IMethodSymbol methodSymbol && |
| 57 | + semanticModel.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.Input.RelayCommandAttribute") is INamedTypeSymbol relayCommandSymbol && |
| 58 | + methodSymbol.HasAttributeWithType(relayCommandSymbol)) |
| 59 | + { |
| 60 | + context.ReportSuppression(Suppression.Create(FieldOrPropertyAttributeListForRelayCommandMethod, diagnostic)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments