Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diagnostic for duplicate ICommand-s in base types #277

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,25 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
/// <returns>Whether or not <paramref name="methodSymbol"/> was unique within its containing type.</returns>
private static bool IsCommandDefinitionUnique(IMethodSymbol methodSymbol, ImmutableArray<Diagnostic>.Builder diagnostics)
{
// If a duplicate is present in any of the base types, always emit a diagnostic for the current method.
// That is, there is no need to check the order: we assume the priority is top-down in the type hierarchy.
// This check has to be done first, as otherwise there would always be a false positive for the current type.
foreach (ISymbol symbol in methodSymbol.ContainingType.BaseType?.GetAllMembers(methodSymbol.Name) ?? Enumerable.Empty<ISymbol>())
{
if (symbol is IMethodSymbol otherSymbol &&
otherSymbol.HasAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.Input.ICommandAttribute"))
{
diagnostics.Add(
MultipleICommandMethodOverloadsError,
methodSymbol,
methodSymbol.ContainingType,
methodSymbol);

return false;
}
}

// Check for duplicates in the containing type for the annotated method
foreach (ISymbol symbol in methodSymbol.ContainingType.GetMembers(methodSymbol.Name))
{
if (symbol is IMethodSymbol otherSymbol &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,34 @@ private void GreetUser(object value)
VerifyGeneratedDiagnostics<ICommandGenerator>(source, "MVVMTK0023");
}

[TestMethod]
public void MultipleICommandMethodOverloads_WithOverloadInBaseType()
{
string source = @"
using CommunityToolkit.Mvvm.Input;

namespace MyApp
{
public partial class BaseViewModel
{
[ICommand]
private void GreetUser()
{
}
}

public partial class SampleViewModel : BaseViewModel
{
[ICommand]
private void GreetUser(object value)
{
}
}
}";

VerifyGeneratedDiagnostics<ICommandGenerator>(source, "MVVMTK0023");
}

[TestMethod]
public void InvalidObservablePropertyError_Object()
{
Expand Down