File tree Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2525 IncrementalValuesProvider < ValidationInfo > validationInfo =
2626 context . SyntaxProvider
2727 . CreateSyntaxProvider (
28- static ( node , _ ) => node is ClassDeclarationSyntax ,
28+ static ( node , _ ) => node is ClassDeclarationSyntax classDeclaration && classDeclaration . HasOrPotentiallyHasBaseTypes ( ) ,
2929 static ( context , token ) =>
3030 {
3131 if ( ! context . SemanticModel . Compilation . HasLanguageVersionAtLeastEqualTo ( LanguageVersion . CSharp8 ) )
Original file line number Diff line number Diff line change 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+ using Microsoft . CodeAnalysis ;
6+ using Microsoft . CodeAnalysis . CSharp ;
7+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
8+
9+ namespace CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
10+
11+ /// <summary>
12+ /// Extension methods for the <see cref="SyntaxNode"/> type.
13+ /// </summary>
14+ internal static class TypeDeclarationSyntaxExtensions
15+ {
16+ /// <summary>
17+ /// Checks whether a given <see cref="TypeDeclarationSyntax"/> has or could possibly have any base types, using only syntax.
18+ /// </summary>
19+ /// <param name="typeDeclaration">The input <see cref="TypeDeclarationSyntax"/> instance to check.</param>
20+ /// <returns>Whether <paramref name="typeDeclaration"/> has or could possibly have any base types.</returns>
21+ public static bool HasOrPotentiallyHasBaseTypes ( this TypeDeclarationSyntax typeDeclaration )
22+ {
23+ // If the base types list is not empty, the type can definitely has implemented interfaces
24+ if ( typeDeclaration . BaseList is { Types . Count : > 0 } )
25+ {
26+ return true ;
27+ }
28+
29+ // If the base types list is empty, check if the type is partial. If it is, it means
30+ // that there could be another partial declaration with a non-empty base types list.
31+ foreach ( SyntaxToken modifier in typeDeclaration . Modifiers )
32+ {
33+ if ( modifier . IsKind ( SyntaxKind . PartialKeyword ) )
34+ {
35+ return true ;
36+ }
37+ }
38+
39+ return false ;
40+ }
41+ }
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2626 IncrementalValuesProvider < RecipientInfo > recipientInfo =
2727 context . SyntaxProvider
2828 . CreateSyntaxProvider (
29- static ( node , _ ) => node is ClassDeclarationSyntax ,
29+ static ( node , _ ) => node is ClassDeclarationSyntax classDeclaration && classDeclaration . HasOrPotentiallyHasBaseTypes ( ) ,
3030 static ( context , token ) =>
3131 {
3232 if ( ! context . SemanticModel . Compilation . HasLanguageVersionAtLeastEqualTo ( LanguageVersion . CSharp8 ) )
You can’t perform that action at this time.
0 commit comments