forked from CommunityToolkit/dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve syntactical filtering for messaging/validator generators
- Loading branch information
1 parent
27cd6b4
commit b6dd39b
Showing
3 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
CommunityToolkit.Mvvm.SourceGenerators/Extensions/TypeDeclarationSyntaxExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions; | ||
|
||
/// <summary> | ||
/// Extension methods for the <see cref="SyntaxNode"/> type. | ||
/// </summary> | ||
internal static class TypeDeclarationSyntaxExtensions | ||
{ | ||
/// <summary> | ||
/// Checks whether a given <see cref="TypeDeclarationSyntax"/> has or could possibly have any base types, using only syntax. | ||
/// </summary> | ||
/// <param name="typeDeclaration">The input <see cref="TypeDeclarationSyntax"/> instance to check.</param> | ||
/// <returns>Whether <paramref name="typeDeclaration"/> has or could possibly have any base types.</returns> | ||
public static bool HasOrPotentiallyHasBaseTypes(this TypeDeclarationSyntax typeDeclaration) | ||
{ | ||
// If the base types list is not empty, the type can definitely has implemented interfaces | ||
if (typeDeclaration.BaseList is { Types.Count: > 0 }) | ||
{ | ||
return true; | ||
} | ||
|
||
// If the base types list is empty, check if the type is partial. If it is, it means | ||
// that there could be another partial declaration with a non-empty base types list. | ||
foreach (SyntaxToken modifier in typeDeclaration.Modifiers) | ||
{ | ||
if (modifier.IsKind(SyntaxKind.PartialKeyword)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters