| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ObservableObjectSetterAnalyzer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Collections.Immutable; |
| | | 8 | | using System.Linq; |
| | | 9 | | using Microsoft.CodeAnalysis; |
| | | 10 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 11 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 12 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Observable.Metadata.Generator; |
| | | 15 | | |
| | | 16 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 17 | | public sealed class ObservableObjectSetterAnalyzer : DiagnosticAnalyzer |
| | | 18 | | { |
| | | 19 | | private const string ObservableObjectMetadataName = "MyNet.Observable.ObservableObject"; |
| | | 20 | | |
| | 3 | 21 | | private static readonly DiagnosticDescriptor SetterMustUseSetPropertyDescriptor = new( |
| | 3 | 22 | | id: "MNETOBS004", |
| | 3 | 23 | | title: "Observable property setter must use SetProperty or the changing pipeline", |
| | 3 | 24 | | messageFormat: "Property setter for '{0}' should call SetProperty(ref field, value) or guard assignment with OnP |
| | 3 | 25 | | category: "MyNet.Observable.Usage", |
| | 3 | 26 | | defaultSeverity: DiagnosticSeverity.Warning, |
| | 3 | 27 | | isEnabledByDefault: true, |
| | 3 | 28 | | description: "Assigning a backing field without SetProperty bypasses cancellation, behaviors, and coalesced susp |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = |
| | 9 | 32 | | [SetterMustUseSetPropertyDescriptor]; |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public override void Initialize(AnalysisContext context) |
| | | 36 | | { |
| | 9 | 37 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 9 | 38 | | context.EnableConcurrentExecution(); |
| | 9 | 39 | | context.RegisterSyntaxNodeAction(AnalyzeSetter, SyntaxKind.SetAccessorDeclaration); |
| | 9 | 40 | | } |
| | | 41 | | |
| | | 42 | | private static void AnalyzeSetter(SyntaxNodeAnalysisContext context) |
| | | 43 | | { |
| | 9 | 44 | | if (context.Node is not AccessorDeclarationSyntax setter) |
| | 0 | 45 | | return; |
| | | 46 | | |
| | 9 | 47 | | if (setter.Body is null && setter.ExpressionBody is null) |
| | 0 | 48 | | return; |
| | | 49 | | |
| | 9 | 50 | | if (IsGeneratedFile(setter.SyntaxTree.FilePath)) |
| | 0 | 51 | | return; |
| | | 52 | | |
| | 9 | 53 | | var propertyDeclaration = setter.FirstAncestorOrSelf<PropertyDeclarationSyntax>(); |
| | 9 | 54 | | if (propertyDeclaration is null) |
| | 0 | 55 | | return; |
| | | 56 | | |
| | 9 | 57 | | if (context.SemanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken) is not { } propertyS |
| | 0 | 58 | | return; |
| | | 59 | | |
| | 9 | 60 | | if (propertySymbol.IsIndexer) |
| | 0 | 61 | | return; |
| | | 62 | | |
| | 9 | 63 | | var containingType = propertySymbol.ContainingType; |
| | 9 | 64 | | if (!InheritsFromObservableObject(containingType, context.Compilation)) |
| | 0 | 65 | | return; |
| | | 66 | | |
| | 9 | 67 | | if (UsesSetProperty(setter, context.SemanticModel)) |
| | 3 | 68 | | return; |
| | | 69 | | |
| | 6 | 70 | | if (UsesManualChangingPipeline(setter, context.SemanticModel)) |
| | 3 | 71 | | return; |
| | | 72 | | |
| | 3 | 73 | | if (!AssignsBackingMember(setter, propertySymbol, context.SemanticModel)) |
| | 0 | 74 | | return; |
| | | 75 | | |
| | 3 | 76 | | var location = setter.Body?.OpenBraceToken.GetLocation() ?? setter.ExpressionBody?.GetLocation() ?? setter.GetLo |
| | 3 | 77 | | context.ReportDiagnostic(Diagnostic.Create(SetterMustUseSetPropertyDescriptor, location, propertySymbol.Name)); |
| | 3 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static bool InheritsFromObservableObject(INamedTypeSymbol type, Compilation compilation) |
| | | 81 | | { |
| | 9 | 82 | | var observableObject = compilation.GetTypeByMetadataName(ObservableObjectMetadataName); |
| | 9 | 83 | | if (observableObject is null) |
| | 0 | 84 | | return false; |
| | | 85 | | |
| | 36 | 86 | | for (var current = type; current is not null; current = current.BaseType) |
| | | 87 | | { |
| | 18 | 88 | | if (SymbolEqualityComparer.Default.Equals(current, observableObject)) |
| | 9 | 89 | | return true; |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private static bool UsesSetProperty(AccessorDeclarationSyntax setter, SemanticModel semanticModel) |
| | 9 | 96 | | => setter.DescendantNodes() |
| | 9 | 97 | | .OfType<InvocationExpressionSyntax>() |
| | 9 | 98 | | .Any(invocation => IsSetPropertyInvocation(invocation, semanticModel)); |
| | | 99 | | |
| | | 100 | | private static bool IsSetPropertyInvocation(InvocationExpressionSyntax invocation, SemanticModel semanticModel) |
| | | 101 | | { |
| | 21 | 102 | | var symbol = semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol; |
| | 21 | 103 | | return symbol is { Name: "SetProperty" } |
| | 21 | 104 | | && symbol.ContainingType.ToDisplayString() == ObservableObjectMetadataName; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | private static bool UsesManualChangingPipeline(AccessorDeclarationSyntax setter, SemanticModel semanticModel) |
| | 6 | 108 | | => setter.DescendantNodes() |
| | 6 | 109 | | .OfType<InvocationExpressionSyntax>() |
| | 6 | 110 | | .Any(invocation => |
| | 6 | 111 | | { |
| | 6 | 112 | | var name = semanticModel.GetSymbolInfo(invocation).Symbol?.Name; |
| | 6 | 113 | | return name is "OnPropertyChanging" or "ProcessPropertyChanging"; |
| | 6 | 114 | | }); |
| | | 115 | | |
| | | 116 | | private static bool AssignsBackingMember(AccessorDeclarationSyntax setter, IPropertySymbol property, SemanticModel s |
| | | 117 | | { |
| | 9 | 118 | | foreach (var assignment in setter.DescendantNodes().OfType<AssignmentExpressionSyntax>()) |
| | | 119 | | { |
| | 3 | 120 | | if (assignment.Kind() != SyntaxKind.SimpleAssignmentExpression) |
| | | 121 | | continue; |
| | | 122 | | |
| | 3 | 123 | | var assignedSymbol = semanticModel.GetSymbolInfo(assignment.Left).Symbol; |
| | 3 | 124 | | switch (assignedSymbol) |
| | | 125 | | { |
| | 3 | 126 | | case IFieldSymbol field when IsBackingFieldForProperty(field, property): |
| | | 127 | | case IPropertySymbol assignedProperty |
| | 0 | 128 | | when SymbolEqualityComparer.Default.Equals(assignedProperty, property): |
| | 3 | 129 | | return true; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return false; |
| | 3 | 134 | | } |
| | | 135 | | |
| | | 136 | | private static bool IsBackingFieldForProperty(IFieldSymbol field, IPropertySymbol property) |
| | | 137 | | { |
| | 3 | 138 | | if (field.AssociatedSymbol is not null |
| | 3 | 139 | | && SymbolEqualityComparer.Default.Equals(field.AssociatedSymbol, property)) |
| | | 140 | | { |
| | 0 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | |
| | 3 | 144 | | var fieldName = field.Name.TrimStart('_'); |
| | 3 | 145 | | if (fieldName.StartsWith("m_", System.StringComparison.Ordinal)) |
| | 0 | 146 | | fieldName = fieldName.Substring(2); |
| | | 147 | | |
| | 3 | 148 | | return string.Equals(fieldName, property.Name, System.StringComparison.OrdinalIgnoreCase); |
| | | 149 | | } |
| | | 150 | | |
| | 9 | 151 | | private static bool IsGeneratedFile(string? filePath) => !string.IsNullOrEmpty(filePath) && (filePath!.EndsWith(".g. |
| | 9 | 152 | | || filePath.IndexOf("Ge |
| | | 153 | | } |
| | | 154 | | |