Skip to content

Commit a92bf8b

Browse files
committed
Allow command duffix in memmebr name.
Fix #355
1 parent b9fa589 commit a92bf8b

3 files changed

Lines changed: 46 additions & 22 deletions

File tree

ValidCode/Commands.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace ValidCode
66
public static class Commands
77
{
88
public static readonly RoutedCommand Command1 = new(nameof(Command1), typeof(Commands));
9+
public static readonly RoutedCommand ShowSystemMenuCommand = new("ShowSystemMenu", typeof(Commands));
10+
911
public static readonly RoutedUICommand Command2 = new("Some text", nameof(Command2), typeof(Commands));
1012
#pragma warning disable WPF0150 // Use nameof().
1113
public static readonly RoutedCommand Command3 = new("Command3", typeof(Commands));

WpfAnalyzers.Test/WPF0120RegisterContainingMemberAsNameForRoutedCommandTests/Valid.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,38 @@ public static class Valid
88
private static readonly RoutedCommandCreationAnalyzer Analyzer = new();
99

1010
[Test]
11-
public static void RoutedCommand()
11+
public static void RoutedCommandNameOf()
1212
{
1313
var code = @"
1414
namespace N
1515
{
1616
using System.Windows.Input;
1717
18-
public static class Foo
18+
public static class C
1919
{
20-
public static readonly RoutedCommand Bar = new RoutedCommand(nameof(Bar), typeof(Foo));
20+
public static readonly RoutedCommand F = new RoutedCommand(nameof(F), typeof(C));
2121
}
2222
}";
2323
RoslynAssert.Valid(Analyzer, code);
2424
}
2525

26+
[TestCase("F")]
27+
[TestCase("FCommand")]
28+
public static void RoutedCommandLiteralName(string fieldName)
29+
{
30+
var code = @"
31+
namespace N
32+
{
33+
using System.Windows.Input;
34+
35+
public static class C
36+
{
37+
public static readonly RoutedCommand F = new RoutedCommand(""F"", typeof(C));
38+
}
39+
}".AssertReplace("public static readonly RoutedCommand F", $"public static readonly RoutedCommand {fieldName}");
40+
RoslynAssert.Valid(Analyzer, Descriptors.WPF0120RegisterContainingMemberAsNameForRoutedCommand, code);
41+
}
42+
2643
[Test]
2744
public static void RoutedUICommand()
2845
{
@@ -31,9 +48,9 @@ namespace N
3148
{
3249
using System.Windows.Input;
3350
34-
public static class Foo
51+
public static class C
3552
{
36-
public static readonly RoutedUICommand Bar = new RoutedUICommand(""Some text"", nameof(Bar), typeof(Foo));
53+
public static readonly RoutedUICommand F = new RoutedUICommand(""Some text"", nameof(F), typeof(C));
3754
}
3855
}";
3956
RoslynAssert.Valid(Analyzer, code);

WpfAnalyzers/Analyzers/RoutedCommandCreationAnalyzer.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ context.ContainingSymbol is { } containingSymbol &&
5757
if (objectCreation.TryFindArgument(nameParameter, out var nameArg) &&
5858
nameArg.TryGetStringValue(context.SemanticModel, context.CancellationToken, out var registeredName))
5959
{
60-
if (registeredName != fieldOrProperty.Name)
60+
if (registeredName != fieldOrProperty.Name &&
61+
!fieldOrProperty.Name.IsParts(registeredName ?? string.Empty, "Command"))
6162
{
6263
context.ReportDiagnostic(
6364
Diagnostic.Create(
@@ -66,23 +67,27 @@ context.ContainingSymbol is { } containingSymbol &&
6667
ImmutableDictionary<string, string?>.Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name),
6768
fieldOrProperty.Name));
6869
}
69-
else if (nameArg.Expression.IsKind(SyntaxKind.StringLiteralExpression))
70-
{
71-
context.ReportDiagnostic(
72-
Diagnostic.Create(
73-
Descriptors.WPF0150UseNameofInsteadOfLiteral,
74-
nameArg.GetLocation(),
75-
ImmutableDictionary<string, string?>.Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name),
76-
fieldOrProperty.Name));
77-
}
78-
else if (!nameArg.Expression.IsNameof())
70+
71+
if (registeredName == fieldOrProperty.Name)
7972
{
80-
context.ReportDiagnostic(
81-
Diagnostic.Create(
82-
Descriptors.WPF0151UseNameofInsteadOfConstant,
83-
nameArg.GetLocation(),
84-
ImmutableDictionary<string, string?>.Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name),
85-
fieldOrProperty.Name));
73+
if (nameArg.Expression.IsKind(SyntaxKind.StringLiteralExpression))
74+
{
75+
context.ReportDiagnostic(
76+
Diagnostic.Create(
77+
Descriptors.WPF0150UseNameofInsteadOfLiteral,
78+
nameArg.GetLocation(),
79+
ImmutableDictionary<string, string?>.Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name),
80+
fieldOrProperty.Name));
81+
}
82+
else if (!nameArg.Expression.IsNameof())
83+
{
84+
context.ReportDiagnostic(
85+
Diagnostic.Create(
86+
Descriptors.WPF0151UseNameofInsteadOfConstant,
87+
nameArg.GetLocation(),
88+
ImmutableDictionary<string, string?>.Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name),
89+
fieldOrProperty.Name));
90+
}
8691
}
8792
}
8893
}

0 commit comments

Comments
 (0)