Skip to content

Commit 277663c

Browse files
Clarify [EventHandler] tag helper discovery logic and avoid exception (#10828)
This fixes an issue that @tmat pointed out to me over email. In a recent change (#10720), I added a call to `Assumed.Unreachable()` when `[EventHandler]` tag helper discovery encounters an attribute with invalid constructor attributes. However, throwing an exception during tag helper discovery is usually the wrong approach. Normally, if Roslyn symbols aren't in the proper shape during tag helper discovery, Razor will simply not produce a tag helper. (We _do_ support diagnostics for tag helpers, but those are usually reserved for warnings and errors that are related to a tag helper's data that would make it unusable, such as a name containing whitespace.) It turns out that the "unreachable" condition wasn't actually all that unreachable and @tmat was hitting it while working on hot reload tests. So, I've changed the code to make the success conditions clearer, i.e., the attribute data must match one of the two constructor calls. And, I changed the logic to simply skip `[EventHandler]` attributes that don't meet the success conditions.
2 parents 8bddfe9 + 277c780 commit 277663c

File tree

2 files changed

+290
-43
lines changed

2 files changed

+290
-43
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/EventHandlerTagHelperDescriptorProvider.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Collections.Immutable;
56
using Microsoft.AspNetCore.Razor;
67
using Microsoft.AspNetCore.Razor.Language;
78
using Microsoft.AspNetCore.Razor.Language.Components;
@@ -54,14 +55,14 @@ protected override void Collect(ISymbol symbol, ICollection<TagHelperDescriptor>
5455
{
5556
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, _eventHandlerAttribute))
5657
{
57-
if (!AttributeArgs.TryGet(attribute, out var values))
58+
if (!AttributeArgs.TryGet(attribute, out var args))
5859
{
59-
// If this occurs, the EventHandlerAttribute type has been broken.
60-
Assumed.Unreachable();
60+
// If this occurs, the [EventHandler] was defined incorrectly, so we can't create a tag helper.
61+
continue;
6162
}
6263

6364
var (typeName, namespaceName) = displayNames.GetNames();
64-
results.Add(CreateTagHelper(typeName, namespaceName, type.Name, values));
65+
results.Add(CreateTagHelper(typeName, namespaceName, type.Name, args));
6566
}
6667
}
6768
}
@@ -70,8 +71,8 @@ protected override void Collect(ISymbol symbol, ICollection<TagHelperDescriptor>
7071
private readonly record struct AttributeArgs(
7172
string Attribute,
7273
INamedTypeSymbol EventArgsType,
73-
bool EnableStopPropagation,
74-
bool EnablePreventDefault)
74+
bool EnableStopPropagation = false,
75+
bool EnablePreventDefault = false)
7576
{
7677
public static bool TryGet(AttributeData attribute, out AttributeArgs args)
7778
{
@@ -80,34 +81,49 @@ public static bool TryGet(AttributeData attribute, out AttributeArgs args)
8081
// - EventHandlerAttribute(string attributeName, Type eventArgsType);
8182
// - EventHandlerAttribute(string attributeName, Type eventArgsType, bool enableStopPropagation, bool enablePreventDefault);
8283

83-
args = default;
84-
8584
var arguments = attribute.ConstructorArguments;
8685

87-
if (arguments.Length is not (2 or 4))
88-
{
89-
return false;
90-
}
86+
return TryGetFromTwoArguments(arguments, out args) ||
87+
TryGetFromFourArguments(arguments, out args);
9188

92-
if (arguments[0] is not { Value: string attributeName } ||
93-
arguments[1] is not { Value: INamedTypeSymbol eventArgsType })
89+
static bool TryGetFromTwoArguments(ImmutableArray<TypedConstant> arguments, out AttributeArgs args)
9490
{
91+
// Ctor 1: EventHandlerAttribute(string attributeName, Type eventArgsType);
92+
93+
if (arguments is [
94+
{ Value: string attributeName },
95+
{ Value: INamedTypeSymbol eventArgsType }])
96+
{
97+
args = new(attributeName, eventArgsType);
98+
return true;
99+
}
100+
101+
args = default;
95102
return false;
96103
}
97104

98-
// TODO: The enablePreventDefault and enableStopPropagation arguments are incorrectly swapped!
99-
// However, they have been that way since the 4-argument constructor variant was introduced
100-
// in https://github.com/dotnet/razor/commit/7635bba6ef2d3e6798d0846ceb96da6d5908e1b0.
101-
// Fixing this is tracked be https://github.com/dotnet/razor/issues/10497
102-
103-
if (arguments is not [.., { Value: bool enablePreventDefault }, { Value: bool enableStopPropagation }])
105+
static bool TryGetFromFourArguments(ImmutableArray<TypedConstant> arguments, out AttributeArgs args)
104106
{
105-
enableStopPropagation = false;
106-
enablePreventDefault = false;
107-
}
107+
// Ctor 2: EventHandlerAttribute(string attributeName, Type eventArgsType, bool enableStopPropagation, bool enablePreventDefault);
108+
109+
// TODO: The enablePreventDefault and enableStopPropagation arguments are incorrectly swapped!
110+
// However, they have been that way since the 4-argument constructor variant was introduced
111+
// in https://github.com/dotnet/razor/commit/7635bba6ef2d3e6798d0846ceb96da6d5908e1b0.
112+
// Fixing this is tracked be https://github.com/dotnet/razor/issues/10497
113+
114+
if (arguments is [
115+
{ Value: string attributeName },
116+
{ Value: INamedTypeSymbol eventArgsType },
117+
{ Value: bool enablePreventDefault },
118+
{ Value: bool enableStopPropagation }])
119+
{
120+
args = new(attributeName, eventArgsType, enableStopPropagation, enablePreventDefault);
121+
return true;
122+
}
108123

109-
args = new(attributeName, eventArgsType, enableStopPropagation, enablePreventDefault);
110-
return true;
124+
args = default;
125+
return false;
126+
}
111127
}
112128
}
113129

0 commit comments

Comments
 (0)