|
| 1 | +// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using System.Collections.Immutable; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | +using ReactiveUI.SourceGenerators.Extensions; |
| 12 | +using ReactiveUI.SourceGenerators.Helpers; |
| 13 | +using static ReactiveUI.SourceGenerators.Diagnostics.SuppressionDescriptors; |
| 14 | + |
| 15 | +namespace ReactiveUI.SourceGenerators.Diagnostics.Suppressions |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// ReactiveCommand Attribute With Field Or Property Target Diagnostic Suppressor. |
| 19 | + /// </summary> |
| 20 | + /// <seealso cref="DiagnosticSuppressor" /> |
| 21 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 22 | + public sealed class ReactiveAttributeWithFieldTargetDiagnosticSuppressor : DiagnosticSuppressor |
| 23 | + { |
| 24 | + /// <inheritdoc/> |
| 25 | + public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(FieldOrPropertyAttributeListForReactiveProperty); |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + public override void ReportSuppressions(SuppressionAnalysisContext context) |
| 29 | + { |
| 30 | + foreach (var diagnostic in context.ReportedDiagnostics) |
| 31 | + { |
| 32 | + var syntaxNode = diagnostic.Location.SourceTree?.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan); |
| 33 | + |
| 34 | + // Check that the target is effectively [field:] or [property:] over a method declaration, which is the case we're looking for |
| 35 | + if (syntaxNode is AttributeTargetSpecifierSyntax { Parent.Parent: PropertyDeclarationSyntax propertyDeclaration, Identifier: SyntaxToken(SyntaxKind.FieldKeyword) }) |
| 36 | + { |
| 37 | + var semanticModel = context.GetSemanticModel(syntaxNode.SyntaxTree); |
| 38 | + |
| 39 | + // Get the method symbol from the first variable declaration |
| 40 | + ISymbol? declaredSymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); |
| 41 | + |
| 42 | + // Check if the method is using [Reactive], in which case we should suppress the warning |
| 43 | + if (declaredSymbol is IPropertySymbol propertySymbol && |
| 44 | + semanticModel.Compilation.GetTypeByMetadataName(AttributeDefinitions.ReactiveAttributeType) is INamedTypeSymbol reactiveSymbol && |
| 45 | + propertySymbol.HasAttributeWithType(reactiveSymbol)) |
| 46 | + { |
| 47 | + context.ReportSuppression(Suppression.Create(FieldOrPropertyAttributeListForReactiveProperty, diagnostic)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments