Skip to content

Commit de65cbc

Browse files
committed
Simplify
1 parent 1fa641d commit de65cbc

1 file changed

Lines changed: 42 additions & 24 deletions

File tree

WpfAnalyzers/Analyzers/RoutedEventCallbackAnalyzer.cs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,63 +26,81 @@ public override void Initialize(AnalysisContext context)
2626
private static void Handle(SyntaxNodeAnalysisContext context)
2727
{
2828
if (!context.IsExcludedFromAnalysis() &&
29-
context.Node is ArgumentSyntax { Expression: ObjectCreationExpressionSyntax { Parent: ArgumentSyntax handlerArgument } objectCreation } &&
30-
objectCreation.TrySingleArgument(out var callbackArg) &&
31-
callbackArg.Expression is IdentifierNameSyntax &&
32-
handlerArgument.FirstAncestor<InvocationExpressionSyntax>() is { } invocation)
29+
context.Node is ArgumentSyntax { Expression: ObjectCreationExpressionSyntax { } } argument &&
30+
argument.FirstAncestor<InvocationExpressionSyntax>() is { } invocation)
3331
{
34-
if (EventManager.RegisterClassHandler.Match(invocation, context.SemanticModel, context.CancellationToken) is { Target: { } target, EventArgument: { } eventArgument })
32+
if (EventManager.RegisterClassHandler.Match(invocation, context.SemanticModel, context.CancellationToken) is { } registerClassHandler)
3533
{
36-
if (Callback.SingleInvocation(target, invocation.FirstAncestorOrSelf<TypeDeclarationSyntax>(), context) is { } &&
37-
CheckName(eventArgument, callbackArg) is var (expectedName, properties))
34+
if (ShouldRename(registerClassHandler.Target, registerClassHandler.EventArgument, registerClassHandler.DelegateArgument) is var (location, properties, expectedName))
3835
{
3936
context.ReportDiagnostic(
4037
Diagnostic.Create(
4138
Descriptors.WPF0090RegisterClassHandlerCallbackNameShouldMatchEvent,
42-
callbackArg.GetLocation(),
39+
location,
4340
properties,
4441
expectedName));
4542
}
4643
}
47-
else if ((EventManager.AddHandler.Match(invocation, context.SemanticModel, context.CancellationToken) is { } ||
48-
EventManager.RemoveHandler.Match(invocation, context.SemanticModel, context.CancellationToken) is { }) &&
49-
invocation.TryGetArgumentAtIndex(0, out eventArgument))
44+
else if (EventManager.AddHandler.Match(invocation, context.SemanticModel, context.CancellationToken) is { } addHandler)
5045
{
51-
if (CheckName(eventArgument, callbackArg) is var (expectedName, properties))
46+
if (ShouldRename(addHandler.Target, addHandler.EventArgument, addHandler.DelegateArgument) is var (location, properties, expectedName))
5247
{
5348
context.ReportDiagnostic(
5449
Diagnostic.Create(
5550
Descriptors.WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent,
56-
callbackArg.GetLocation(),
51+
location,
52+
properties,
53+
expectedName));
54+
}
55+
}
56+
else if (EventManager.RemoveHandler.Match(invocation, context.SemanticModel, context.CancellationToken) is { } removeHandler)
57+
{
58+
if (ShouldRename(removeHandler.Target, removeHandler.EventArgument, removeHandler.DelegateArgument) is var (location, properties, expectedName))
59+
{
60+
context.ReportDiagnostic(
61+
Diagnostic.Create(
62+
Descriptors.WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent,
63+
location,
5764
properties,
5865
expectedName));
5966
}
6067
}
6168

62-
static (string ExpectedName, ImmutableDictionary<string, string?> Properties)? CheckName(ArgumentSyntax eventArgument, ArgumentSyntax callbackArg)
69+
(Location Location, ImmutableDictionary<string, string?> Properties, string ExpectedName)? ShouldRename(IMethodSymbol target, ArgumentSyntax eventArgument, ArgumentSyntax callbackArg)
6370
{
64-
if (callbackArg.Expression is IdentifierNameSyntax invokedHandler &&
65-
Identifier() is { } identifier)
71+
if (CallbackIdentifier() is { } handler &&
72+
Identifier(eventArgument.Expression) is { } eventField)
6673
{
67-
if (EventManager.IsMatch(invokedHandler.Identifier.ValueText, identifier.Identifier.ValueText) == false)
74+
if (EventManager.IsMatch(handler.Identifier.ValueText, eventField.Identifier.ValueText) == false &&
75+
Callback.SingleInvocation(target, invocation.FirstAncestorOrSelf<TypeDeclarationSyntax>(), context) is { })
6876
{
69-
if (EventManager.TryGetExpectedCallbackName(identifier.Identifier.ValueText, out var expectedName))
77+
if (EventManager.TryGetExpectedCallbackName(eventField.Identifier.ValueText, out var expectedName))
7078
{
71-
return (expectedName, ImmutableDictionary<string, string?>.Empty.Add("ExpectedName", expectedName));
79+
return (handler.GetLocation(), ImmutableDictionary<string, string?>.Empty.Add("ExpectedName", expectedName), expectedName);
7280
}
7381

74-
return ("On" + identifier.Identifier.ValueText, ImmutableDictionary<string, string?>.Empty);
82+
return (handler.GetLocation(), ImmutableDictionary<string, string?>.Empty, "On" + eventField.Identifier.ValueText);
7583
}
7684
}
7785

7886
return null;
7987

80-
IdentifierNameSyntax? Identifier()
88+
IdentifierNameSyntax? CallbackIdentifier()
89+
{
90+
return callbackArg switch
91+
{
92+
{ Expression: ObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: 1 } arguments } }
93+
=> Identifier(arguments[0].Expression),
94+
_ => Identifier(callbackArg.Expression),
95+
};
96+
}
97+
98+
IdentifierNameSyntax? Identifier(ExpressionSyntax expression)
8199
{
82-
return eventArgument switch
100+
return expression switch
83101
{
84-
{ Expression: IdentifierNameSyntax name } => name,
85-
{ Expression: MemberAccessExpressionSyntax { Name: IdentifierNameSyntax name } } => name,
102+
IdentifierNameSyntax name => name,
103+
MemberAccessExpressionSyntax { Name: IdentifierNameSyntax name } => name,
86104
_ => null,
87105
};
88106
}

0 commit comments

Comments
 (0)