|
| 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>[GeneratedBindableCustomProperty]</c> is used on types with invalid generated base members. |
| 17 | +/// </summary> |
| 18 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 19 | +public sealed class WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer : DiagnosticAnalyzer |
| 20 | +{ |
| 21 | + /// <inheritdoc/> |
| 22 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create( |
| 23 | + WinRTGeneratedBindableCustomPropertyWithBaseObservablePropertyOnField, |
| 24 | + WinRTGeneratedBindableCustomPropertyWithBaseRelayCommand); |
| 25 | + |
| 26 | + /// <inheritdoc/> |
| 27 | + public override void Initialize(AnalysisContext context) |
| 28 | + { |
| 29 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 30 | + context.EnableConcurrentExecution(); |
| 31 | + |
| 32 | + context.RegisterCompilationStartAction(static context => |
| 33 | + { |
| 34 | + // This analyzer is only enabled when CsWinRT is also used |
| 35 | + if (!context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.IsUsingWindowsRuntimePack()) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Get the symbol for [ObservableProperty], [RelayCommand] and [GeneratedBindableCustomProperty] |
| 41 | + if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") is not INamedTypeSymbol observablePropertySymbol || |
| 42 | + context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.Input.RelayCommandAttribute") is not INamedTypeSymbol relayCommandSymbol || |
| 43 | + context.Compilation.GetTypeByMetadataName("WinRT.GeneratedBindableCustomPropertyAttribute") is not INamedTypeSymbol generatedBindableCustomPropertySymbol) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + bool isLanguageVersionPreview = context.Compilation.IsLanguageVersionPreview(); |
| 49 | + |
| 50 | + context.RegisterSymbolAction(context => |
| 51 | + { |
| 52 | + // Ensure we do have a valid field |
| 53 | + if (context.Symbol is not IFieldSymbol fieldSymbol) |
| 54 | + { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // Emit a diagnostic if the field is using the [ObservableProperty] attribute |
| 59 | + if (fieldSymbol.TryGetAttributeWithType(observablePropertySymbol, out AttributeData? observablePropertyAttribute)) |
| 60 | + { |
| 61 | + // If the C# version is preview, we can include the necessary information to trigger the |
| 62 | + // code fixer. If that is not the case, we shouldn't do that, to avoid the code fixer |
| 63 | + // changing the code to invalid C# (as without the preview version, it wouldn't compile). |
| 64 | + if (isLanguageVersionPreview) |
| 65 | + { |
| 66 | + context.ReportDiagnostic(Diagnostic.Create( |
| 67 | + WinRTObservablePropertyOnFieldsIsNotAotCompatible, |
| 68 | + observablePropertyAttribute.GetLocation(), |
| 69 | + ImmutableDictionary.Create<string, string?>() |
| 70 | + .Add(FieldReferenceForObservablePropertyFieldAnalyzer.FieldNameKey, fieldSymbol.Name) |
| 71 | + .Add(FieldReferenceForObservablePropertyFieldAnalyzer.PropertyNameKey, ObservablePropertyGenerator.Execute.GetGeneratedPropertyName(fieldSymbol)), |
| 72 | + fieldSymbol.ContainingType, |
| 73 | + fieldSymbol.Name)); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + context.ReportDiagnostic(Diagnostic.Create( |
| 78 | + WinRTObservablePropertyOnFieldsIsNotAotCompatible, |
| 79 | + observablePropertyAttribute.GetLocation(), |
| 80 | + fieldSymbol.ContainingType, |
| 81 | + fieldSymbol.Name)); |
| 82 | + } |
| 83 | + } |
| 84 | + }, SymbolKind.Field); |
| 85 | + }); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#endif |
0 commit comments