You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Make sure we can resolve the [ObservableProperty] attribute (as we want to add it in the fixed code)
62
+
if(semanticModel.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute")is not INamedTypeSymbolobservablePropertySymbol)
63
+
{
64
+
return;
65
+
}
66
+
51
67
// Register the code fix to update the semi-auto property to a partial property
@@ -64,14 +80,47 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
64
80
/// <param name="document">The original document being fixed.</param>
65
81
/// <param name="root">The original tree root belonging to the current document.</param>
66
82
/// <param name="propertyDeclaration">The <see cref="PropertyDeclarationSyntax"/> for the property being updated.</param>
83
+
/// <param name="observablePropertySymbol">The <see cref="INamedTypeSymbol"/> for <c>[ObservableProperty]</c>.</param>
67
84
/// <returns>An updated document with the applied code fix, and <paramref name="propertyDeclaration"/> being replaced with a partial property.</returns>
/// Applies the code fix to a target identifier and returns an updated document.
114
+
/// </summary>
115
+
/// <param name="propertyDeclaration">The <see cref="PropertyDeclarationSyntax"/> for the property being updated.</param>
116
+
/// <param name="observablePropertyAttributeList">The <see cref="AttributeListSyntax"/> with the attribute to add.</param>
117
+
/// <param name="syntaxEditor">The <see cref="SyntaxEditor"/> instance to use.</param>
118
+
/// <returns>An updated document with the applied code fix, and <paramref name="propertyDeclaration"/> being replaced with a partial property.</returns>
// Get the semantic model, as we need to resolve symbols
194
+
if(awaitdocument.GetSemanticModelAsync(fixAllContext.CancellationToken).ConfigureAwait(false)is not SemanticModelsemanticModel)
195
+
{
196
+
returndocument;
197
+
}
198
+
199
+
// Make sure we can resolve the [ObservableProperty] attribute here as well
200
+
if(semanticModel.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute")is not INamedTypeSymbolobservablePropertySymbol)
201
+
{
202
+
returndocument;
203
+
}
204
+
205
+
// Get the document root (this should always succeed)
206
+
if(awaitdocument.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false)is not SyntaxNoderoot)
Copy file name to clipboardExpand all lines: tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/Test_UseObservablePropertyOnSemiAutoPropertyCodeFixer.cs
+132Lines changed: 132 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -127,6 +127,57 @@ public partial class SampleViewModel : ObservableObject
// /0/Test0.cs(5,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.Name can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)
// /0/Test0.cs(5,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.FirstName can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)
// /0/Test0.cs(11,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.LastName can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)
// /0/Test0.cs(17,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.PhoneNumber can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)
0 commit comments