Skip to content
This repository was archived by the owner on Sep 26, 2024. It is now read-only.
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 @@ -661,13 +661,13 @@ private void WriteComponentAttributeInnards(CodeRenderingContext context, Compon
// An event callback can either be passed verbatim, or it can be created by the EventCallbackFactory.
// Since we don't look at the code the user typed inside the attribute value, this is always
// resolved via overloading.

var explicitType = (bool?)node.Annotations[ComponentMetadata.Component.ExplicitTypeNameKey];
var isInferred = (bool?)node.Annotations[ComponentMetadata.Component.OpenGenericKey];
if (canTypeCheck && NeedsTypeCheck(node))
{
context.CodeWriter.Write(ComponentsApi.RuntimeHelpers.TypeCheck);
context.CodeWriter.Write("<");
context.CodeWriter.Write("global::");
QualifyEventCallback(context.CodeWriter, node.TypeName);
QualifyEventCallback(context.CodeWriter, node.TypeName, explicitType);
context.CodeWriter.Write(">");
context.CodeWriter.Write("(");
}
Expand All @@ -680,10 +680,17 @@ private void WriteComponentAttributeInnards(CodeRenderingContext context, Compon
context.CodeWriter.Write(".");
context.CodeWriter.Write(ComponentsApi.EventCallbackFactory.CreateMethod);

if (node.TryParseEventCallbackTypeArgument(out StringSegment argument))
if (isInferred != true && node.TryParseEventCallbackTypeArgument(out StringSegment argument))
{
context.CodeWriter.Write("<");
TypeNameHelper.WriteGloballyQualifiedName(context.CodeWriter, argument);
if (explicitType == true)
{
context.CodeWriter.Write(argument);
}
else
{
TypeNameHelper.WriteGloballyQualifiedName(context.CodeWriter, argument);
}
context.CodeWriter.Write(">");
}

Expand Down Expand Up @@ -738,18 +745,26 @@ private void WriteComponentAttributeInnards(CodeRenderingContext context, Compon
}
}

static void QualifyEventCallback(CodeWriter codeWriter, string typeName)
static void QualifyEventCallback(CodeWriter codeWriter, string typeName, bool? explicitType)
{
if(ComponentAttributeIntermediateNode.TryGetEventCallbackArgument(typeName, out var argument))
if (ComponentAttributeIntermediateNode.TryGetEventCallbackArgument(typeName, out var argument))
{
codeWriter.Write("global::");
codeWriter.Write(ComponentsApi.EventCallback.FullTypeName);
codeWriter.Write("<");
TypeNameHelper.WriteGloballyQualifiedName(codeWriter, argument);
if (explicitType == true)
{
codeWriter.Write(argument);
}
else
{
TypeNameHelper.WriteGloballyQualifiedName(codeWriter, argument);
}
codeWriter.Write(">");
}
else
{
codeWriter.Write(typeName);
TypeNameHelper.WriteGloballyQualifiedName(codeWriter, typeName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void Process(ComponentIntermediateNode node)
if (ValidateTypeArguments(node, bindings))
{
var mappings = bindings.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Content);
RewriteTypeNames(_pass.TypeNameFeature.CreateGenericTypeRewriter(mappings), node);
RewriteTypeNames(_pass.TypeNameFeature.CreateGenericTypeRewriter(mappings), node, hasTypeArgumentSpecified);
}

return;
Expand All @@ -131,7 +131,7 @@ private void Process(ComponentIntermediateNode node)

// Since we're generating code in a different namespace, we need to 'global qualify' all of the types
// to avoid clashes with our generated code.
RewriteTypeNames(_pass.TypeNameFeature.CreateGlobalQualifiedTypeNameRewriter(bindings.Keys), node);
RewriteTypeNames(_pass.TypeNameFeature.CreateGlobalQualifiedTypeNameRewriter(bindings.Keys), node, hasTypeArgumentSpecified: false, bindings);

//
// We need to verify that an argument was provided that 'covers' each type parameter.
Expand Down Expand Up @@ -330,8 +330,10 @@ private static bool ValidateTypeArguments(ComponentIntermediateNode node, Dictio
return true;
}

private void RewriteTypeNames(TypeNameRewriter rewriter, ComponentIntermediateNode node)
private void RewriteTypeNames(TypeNameRewriter rewriter, ComponentIntermediateNode node, bool? hasTypeArgumentSpecified = null, IDictionary<string, Binding> bindings = null)
{
var typeNameFeature = _pass.TypeNameFeature;

// Rewrite the component type name
node.TypeName = rewriter.Rewrite(node.TypeName);

Expand All @@ -358,10 +360,18 @@ private void RewriteTypeNames(TypeNameRewriter rewriter, ComponentIntermediateNo
// type parameter. In which case, we mark it with an additional annotation to
// acount for that during code generation and avoid trying to fully qualify
// the type name.
if (!globallyQualifiedTypeName.StartsWith("global::", StringComparison.Ordinal))
if (hasTypeArgumentSpecified == true)
{
attribute.Annotations.Add(ComponentMetadata.Component.ExplicitTypeNameKey, true);
}
else if(attribute.BoundAttribute?.IsEventCallbackProperty() ?? false)
{
var callbackArgument = typeNameFeature.ParseTypeParameters(attribute.TypeName)[0];
if (bindings.ContainsKey(callbackArgument))
{
attribute.Annotations.Add(ComponentMetadata.Component.OpenGenericKey, true);
}
}
}
else if (attribute.TypeName == null && (attribute.BoundAttribute?.IsDelegateProperty() ?? false))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public static class Component

public const string ExplicitTypeNameKey = "Components.ExplicitTypeName";

public const string OpenGenericKey = "Components.OpenGeneric";

public const string TypeParameterKey = "Components.TypeParameter";

public const string TypeParameterIsCascadingKey = "Components.TypeParameterIsCascading";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,13 @@ private void WriteComponentAttributeInnards(CodeRenderingContext context, Compon
}
else if (node.BoundAttribute?.IsEventCallbackProperty() ?? false)
{
var explicitType = (bool?)node.Annotations[ComponentMetadata.Component.ExplicitTypeNameKey];
var isInferred = (bool?)node.Annotations[ComponentMetadata.Component.OpenGenericKey];
if (canTypeCheck && NeedsTypeCheck(node))
{
context.CodeWriter.Write(ComponentsApi.RuntimeHelpers.TypeCheck);
context.CodeWriter.Write("<");
context.CodeWriter.Write("global::");
QualifyEventCallback(context.CodeWriter, node.TypeName);
QualifyEventCallback(context.CodeWriter, node.TypeName, explicitType);
context.CodeWriter.Write(">");
context.CodeWriter.Write("(");
}
Expand All @@ -624,10 +625,17 @@ private void WriteComponentAttributeInnards(CodeRenderingContext context, Compon
context.CodeWriter.Write(".");
context.CodeWriter.Write(ComponentsApi.EventCallbackFactory.CreateMethod);

if (node.TryParseEventCallbackTypeArgument(out StringSegment argument))
if (isInferred != true && node.TryParseEventCallbackTypeArgument(out StringSegment argument))
{
context.CodeWriter.Write("<");
TypeNameHelper.WriteGloballyQualifiedName(context.CodeWriter, argument);
if (explicitType == true)
{
context.CodeWriter.Write(argument);
}
else
{
TypeNameHelper.WriteGloballyQualifiedName(context.CodeWriter, argument);
}
context.CodeWriter.Write(">");
}

Expand Down Expand Up @@ -678,18 +686,26 @@ private void WriteComponentAttributeInnards(CodeRenderingContext context, Compon

}

static void QualifyEventCallback(CodeWriter codeWriter, string typeName)
static void QualifyEventCallback(CodeWriter codeWriter, string typeName, bool? explicitType)
{
if (ComponentAttributeIntermediateNode.TryGetEventCallbackArgument(typeName, out var argument))
{
codeWriter.Write("global::");
codeWriter.Write(ComponentsApi.EventCallback.FullTypeName);
codeWriter.Write("<");
TypeNameHelper.WriteGloballyQualifiedName(codeWriter, argument);
if (explicitType == true)
{
codeWriter.Write(argument);
}
else
{
TypeNameHelper.WriteGloballyQualifiedName(codeWriter, argument);
}
codeWriter.Write(">");
}
else
{
codeWriter.Write(typeName);
TypeNameHelper.WriteGloballyQualifiedName(codeWriter, typeName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ internal bool TryParseEventCallbackTypeArgument(out StringSegment argument)
return TryGetEventCallbackArgument(TypeName, out argument);
}

internal static bool TryGetEventCallbackArgument(string typeName, out StringSegment argument)
internal static bool TryGetEventCallbackArgument(string candidate, out StringSegment argument)
{
if (string.Equals(typeName, ComponentsApi.EventCallback.FullTypeName, StringComparison.Ordinal))
var typeName = new StringSegment(candidate, candidate.StartsWith("global::", StringComparison.Ordinal) ? 8 : 0);
if (typeName.Equals(ComponentsApi.EventCallback.FullTypeName, StringComparison.Ordinal))
{
// Non-Generic
argument = default;
Expand All @@ -204,7 +205,7 @@ internal static bool TryGetEventCallbackArgument(string typeName, out StringSegm
// OK this is promising.
//
// Chop off leading `...EventCallback<` and let the length so the ending `>` is cut off as well.
argument = new StringSegment(typeName, ComponentsApi.EventCallback.FullTypeName.Length + 1, typeName.Length - (ComponentsApi.EventCallback.FullTypeName.Length + "<>".Length));
argument = typeName.Subsegment(ComponentsApi.EventCallback.FullTypeName.Length + 1, typeName.Length - (ComponentsApi.EventCallback.FullTypeName.Length + "<>".Length));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,80 @@ private void Increment() {
CompileToAssembly(generated);
}

[Fact]
public void EventCallbackOfT_GenericComponent_ExplicitType()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse(@"
using System;
using Microsoft.AspNetCore.Components;

namespace Test
{
public class MyComponent<T> : ComponentBase
{
[Parameter]
public EventCallback<T> OnClick { get; set; }
}

public class MyType
{
}
}
"));

// Act
var generated = CompileToCSharp(@"
<MyComponent T=""MyType"" OnClick=""@((MyType arg) => counter++)""/>

@code {
private int counter;
}");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void EventCallbackOfT_GenericComponent_ExplicitType_MethodGroup()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse(@"
using System;
using Microsoft.AspNetCore.Components;

namespace Test
{
public class MyComponent<T> : ComponentBase
{
[Parameter]
public EventCallback<T> OnClick { get; set; }
}

public class MyType
{
}
}
"));

// Act
var generated = CompileToCSharp(@"
<MyComponent T=""MyType"" OnClick=""Increment""/>

@code {
private int counter;

public void Increment(MyType type) => counter++;
}");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void EventCallback_CanPassEventCallbackOfT_Explicitly()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__o = typeof(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
MyType

#line default
#line hidden
#nullable disable
);
__o = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.EventCallback<MyType>>(global::Microsoft.AspNetCore.Components.EventCallback.Factory.Create<MyType>(this,
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
(MyType arg) => counter++

#line default
#line hidden
#nullable disable
));
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Test.MyComponent<>);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"

private int counter;

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [64] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentTypeArgument - (16:0,16 [6] x:\dir\subdir\Test\TestComponent.cshtml) - T
LazyIntermediateToken - (16:0,16 [6] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - MyType
ComponentAttribute - (33:0,33 [28] x:\dir\subdir\Test\TestComponent.cshtml) - OnClick - OnClick - AttributeStructure.DoubleQuotes
CSharpExpression - (34:0,34 [27] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (35:0,35 [25] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - (MyType arg) => counter++
HtmlContent - (64:0,64 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (64:0,64 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (75:2,7 [28] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (75:2,7 [28] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private int counter;\n
Loading