@@ -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 ) ||
0 commit comments