Skip to content

Commit 171ba41

Browse files
committed
Tweaks to the '[ObservableProperty]' code fixer
1 parent e2c33e7 commit 171ba41

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

src/CommunityToolkit.Mvvm.CodeFixers/UsePartialPropertyForObservablePropertyCodeFixer.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public sealed class UsePartialPropertyForObservablePropertyCodeFixer : CodeFixPr
5757
});
5858

5959
/// <inheritdoc/>
60-
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(UseObservablePropertyOnPartialPropertyId);
60+
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(
61+
UseObservablePropertyOnPartialPropertyId,
62+
WinRTObservablePropertyOnFieldsIsNotAotCompatibleId);
6163

6264
/// <inheritdoc/>
6365
public override FixAllProvider? GetFixAllProvider()
@@ -77,6 +79,14 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
7779
return;
7880
}
7981

82+
SemanticModel semanticModel = (await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false))!;
83+
84+
// If the language is not preview, we cannot apply this code fix (as it would generate invalid C# code)
85+
if (!semanticModel.Compilation.IsLanguageVersionPreview())
86+
{
87+
return;
88+
}
89+
8090
// Retrieve the properties passed by the analyzer
8191
if (diagnostic.Properties[FieldReferenceForObservablePropertyFieldAnalyzer.FieldNameKey] is not string fieldName ||
8292
diagnostic.Properties[FieldReferenceForObservablePropertyFieldAnalyzer.PropertyNameKey] is not string propertyName)
@@ -101,7 +111,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
101111
context.RegisterCodeFix(
102112
CodeAction.Create(
103113
title: "Use a partial property",
104-
createChangedDocument: token => ConvertToPartialProperty(context.Document, root, fieldDeclaration, fieldName, propertyName, context.CancellationToken),
114+
createChangedDocument: token => ConvertToPartialProperty(context.Document, root, fieldDeclaration, semanticModel, fieldName, propertyName, context.CancellationToken),
105115
equivalenceKey: "Use a partial property"),
106116
diagnostic);
107117
}
@@ -113,6 +123,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
113123
/// <param name="document">The original document being fixed.</param>
114124
/// <param name="root">The original tree root belonging to the current document.</param>
115125
/// <param name="fieldDeclaration">The <see cref="FieldDeclarationSyntax"/> for the field being updated.</param>
126+
/// <param name="semanticModel">The semantic model for <paramref name="document"/>.</param>
116127
/// <param name="fieldName">The name of the annotated field.</param>
117128
/// <param name="propertyName">The name of the generated property.</param>
118129
/// <param name="cancellationToken">The cancellation token for the operation.</param>
@@ -121,11 +132,12 @@ private static async Task<Document> ConvertToPartialProperty(
121132
Document document,
122133
SyntaxNode root,
123134
FieldDeclarationSyntax fieldDeclaration,
135+
SemanticModel semanticModel,
124136
string fieldName,
125137
string propertyName,
126138
CancellationToken cancellationToken)
127139
{
128-
SemanticModel semanticModel = (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false))!;
140+
await Task.CompletedTask;
129141

130142
// Try to get all necessary type symbols to process the attributes
131143
if (!semanticModel.Compilation.TryBuildNamedTypeSymbolMap(MvvmToolkitAttributeNamesToFullyQualifiedNamesMap, out ImmutableDictionary<string, INamedTypeSymbol>? toolkitTypeSymbols) ||

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer.cs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public override void Initialize(AnalysisContext context)
4141
return;
4242
}
4343

44-
bool isLanguageVersionPreview = context.Compilation.IsLanguageVersionPreview();
45-
4644
context.RegisterSymbolAction(context =>
4745
{
4846
// Ensure we do have a valid field
@@ -54,28 +52,14 @@ public override void Initialize(AnalysisContext context)
5452
// Emit a diagnostic if the field is using the [ObservableProperty] attribute
5553
if (fieldSymbol.TryGetAttributeWithType(observablePropertySymbol, out AttributeData? observablePropertyAttribute))
5654
{
57-
// If the C# version is preview, we can include the necessary information to trigger the
58-
// code fixer. If that is not the case, we shouldn't do that, to avoid the code fixer
59-
// changing the code to invalid C# (as without the preview version, it wouldn't compile).
60-
if (isLanguageVersionPreview)
61-
{
62-
context.ReportDiagnostic(Diagnostic.Create(
63-
WinRTObservablePropertyOnFieldsIsNotAotCompatible,
64-
observablePropertyAttribute.GetLocation(),
65-
ImmutableDictionary.Create<string, string?>()
66-
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.FieldNameKey, fieldSymbol.Name)
67-
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.PropertyNameKey, ObservablePropertyGenerator.Execute.GetGeneratedPropertyName(fieldSymbol)),
68-
fieldSymbol.ContainingType,
69-
fieldSymbol.Name));
70-
}
71-
else
72-
{
73-
context.ReportDiagnostic(Diagnostic.Create(
74-
WinRTObservablePropertyOnFieldsIsNotAotCompatible,
75-
observablePropertyAttribute.GetLocation(),
76-
fieldSymbol.ContainingType,
77-
fieldSymbol.Name));
78-
}
55+
context.ReportDiagnostic(Diagnostic.Create(
56+
WinRTObservablePropertyOnFieldsIsNotAotCompatible,
57+
observablePropertyAttribute.GetLocation(),
58+
ImmutableDictionary.Create<string, string?>()
59+
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.FieldNameKey, fieldSymbol.Name)
60+
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.PropertyNameKey, ObservablePropertyGenerator.Execute.GetGeneratedPropertyName(fieldSymbol)),
61+
fieldSymbol.ContainingType,
62+
fieldSymbol.Name));
7963
}
8064
}, SymbolKind.Field);
8165
});

0 commit comments

Comments
 (0)