Skip to content

Commit 0d739ca

Browse files
committed
Stripped Async suffix to generated command names
1 parent 8c58a67 commit 0d739ca

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ private static void OnExecute(
126126
private static IEnumerable<MemberDeclarationSyntax> CreateCommandMembers(GeneratorExecutionContext context, SyntaxTriviaList leadingTrivia, IMethodSymbol methodSymbol)
127127
{
128128
// Get the command member names
129-
string
130-
propertyName = methodSymbol.Name + "Command",
131-
fieldName = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1)}";
129+
var (fieldName, propertyName) = GetGeneratedFieldAndPropertyNames(context, methodSymbol);
132130

133131
// Get the command type symbols
134132
if (!TryMapCommandTypesFromMethod(
@@ -190,6 +188,30 @@ private static IEnumerable<MemberDeclarationSyntax> CreateCommandMembers(Generat
190188
return new MemberDeclarationSyntax[] { fieldDeclaration, propertyDeclaration };
191189
}
192190

191+
/// <summary>
192+
/// Get the generated field and property names for the input method.
193+
/// </summary>
194+
/// <param name="context">The input <see cref="GeneratorExecutionContext"/> instance to use.</param>
195+
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
196+
/// <returns>The generated field and property names for <paramref name="methodSymbol"/>.</returns>
197+
[Pure]
198+
private static (string FieldName, string PropertyName) GetGeneratedFieldAndPropertyNames(GeneratorExecutionContext context, IMethodSymbol methodSymbol)
199+
{
200+
string propertyName = methodSymbol.Name;
201+
202+
if (SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, context.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task")!) &&
203+
methodSymbol.Name.EndsWith("Async"))
204+
{
205+
propertyName = propertyName.Substring(0, propertyName.Length - "Async".Length);
206+
}
207+
208+
propertyName += "Command";
209+
210+
string fieldName = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1)}";
211+
212+
return (fieldName, propertyName);
213+
}
214+
193215
/// <summary>
194216
/// Gets the type symbols for the input method, if supported.
195217
/// </summary>

UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class Test_ICommandAttribute
1616
{
1717
[TestCategory("Mvvm")]
1818
[TestMethod]
19-
public void Test_ICommandAttribute_RelayCommand()
19+
public async Task Test_ICommandAttribute_RelayCommand()
2020
{
2121
var model = new MyViewModel();
2222

@@ -30,19 +30,19 @@ public void Test_ICommandAttribute_RelayCommand()
3030

3131
Assert.AreEqual(model.Counter, 6);
3232

33-
model.IncrementCounterAsyncCommand.Execute(null);
33+
await model.DelayAndIncrementCounterCommand.ExecuteAsync(null);
3434

3535
Assert.AreEqual(model.Counter, 7);
3636

37-
model.IncrementCounterWithTokenAsyncCommand.Execute(null);
37+
await model.DelayAndIncrementCounterWithTokenCommand.ExecuteAsync(null);
3838

3939
Assert.AreEqual(model.Counter, 8);
4040

41-
model.IncrementCounterWithValueAsyncCommand.Execute(5);
41+
await model.DelayAndIncrementCounterWithValueCommand.ExecuteAsync(5);
4242

4343
Assert.AreEqual(model.Counter, 13);
4444

45-
model.IncrementCounterWithValueAndTokenAsyncCommand.Execute(5);
45+
await model.DelayAndIncrementCounterWithValueAndTokenCommand.ExecuteAsync(5);
4646

4747
Assert.AreEqual(model.Counter, 18);
4848
}
@@ -64,35 +64,35 @@ private void IncrementCounterWithValue(int count)
6464
}
6565

6666
[ICommand]
67-
private Task IncrementCounterAsync()
67+
private async Task DelayAndIncrementCounterAsync()
6868
{
69-
Counter += 1;
69+
await Task.Delay(50);
7070

71-
return Task.CompletedTask;
71+
Counter += 1;
7272
}
7373

7474
[ICommand]
75-
private Task IncrementCounterWithTokenAsync(CancellationToken token)
75+
private async Task DelayAndIncrementCounterWithTokenAsync(CancellationToken token)
7676
{
77-
Counter += 1;
77+
await Task.Delay(50);
7878

79-
return Task.CompletedTask;
79+
Counter += 1;
8080
}
8181

8282
[ICommand]
83-
private Task IncrementCounterWithValueAsync(int count)
83+
private async Task DelayAndIncrementCounterWithValueAsync(int count)
8484
{
85-
Counter += count;
85+
await Task.Delay(50);
8686

87-
return Task.CompletedTask;
87+
Counter += count;
8888
}
8989

9090
[ICommand]
91-
private Task IncrementCounterWithValueAndTokenAsync(int count, CancellationToken token)
91+
private async Task DelayAndIncrementCounterWithValueAndTokenAsync(int count, CancellationToken token)
9292
{
93-
Counter += count;
93+
await Task.Delay(50);
9494

95-
return Task.CompletedTask;
95+
Counter += count;
9696
}
9797
}
9898
}

UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public void Test_AlsoNotifyForAttribute_Events()
6060
{
6161
var model = new DependentPropertyModel();
6262

63-
(PropertyChangedEventArgs, int) changed = default;
6463
List<string> propertyNames = new();
6564

6665
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);

0 commit comments

Comments
 (0)