-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix [RelayCommand] attribute over methods with no attributes
- Loading branch information
1 parent
11c06e9
commit c1b5d73
Showing
4 changed files
with
113 additions
and
1 deletion.
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
src/CommunityToolkit.Mvvm.SourceGenerators/Extensions/MethodDeclarationSyntaxExtensions.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="MethodDeclarationSyntax"/> type. | ||
/// </summary> | ||
internal static class MethodDeclarationSyntaxExtensions | ||
{ | ||
/// <summary> | ||
/// Checks whether a given <see cref="MethodDeclarationSyntax"/> has or could potentially have any attribute lists. | ||
/// </summary> | ||
/// <param name="methodDeclaration">The input <see cref="MethodDeclarationSyntax"/> to check.</param> | ||
/// <returns>Whether <paramref name="methodDeclaration"/> has or potentially has any attribute lists.</returns> | ||
public static bool HasOrPotentiallyHasAttributeLists(this MethodDeclarationSyntax methodDeclaration) | ||
{ | ||
// If the declaration has any attribute lists, there's nothing left to do | ||
if (methodDeclaration.AttributeLists.Count > 0) | ||
{ | ||
return true; | ||
} | ||
|
||
// If there are no attributes, check whether the method declaration has the partial keyword. If it | ||
// does, there could potentially be attribute lists on the other partial definition/implementation. | ||
foreach (SyntaxToken modifier in methodDeclaration.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
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